Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error While Disconnecting from Webscoket Client #73

Open
mardommah opened this issue Mar 18, 2024 · 5 comments
Open

Error While Disconnecting from Webscoket Client #73

mardommah opened this issue Mar 18, 2024 · 5 comments

Comments

@mardommah
Copy link

When trying to disconnect from websocket using postman or insomnia, always returning error like this
image

@mikuso
Copy link
Owner

mikuso commented Mar 18, 2024

It looks like you're not handling a certain condition.

Can you provide a small sample of code which I can use to replicate the issue?

@mardommah
Copy link
Author

Hello, here my code example, currently im integrating it with express js to create command initiated by central system -> charging station

rpc.js
image

api.js
image

server.js
image

@mikuso
Copy link
Owner

mikuso commented Mar 20, 2024

Hi @mardommah ,

Can you please copy + paste the code so that I can try running it? I'd rather not have to re-type it.

@mardommah
Copy link
Author

mardommah commented Mar 20, 2024

okay here's the code

server.js

`const express = require('express');
const serverRouter = require('./api')
const rpcServer = require("./rpc")

const app = express();
app.use(serverRouter)
const httpServer = app.listen(3002, 'localhost', () => {
console.log("server running on 3002")
});

httpServer.on('upgrade', rpcServer.handleUpgrade);

`

rpc.js

`
const { RPCServer } = require('ocpp-rpc');

const rpcServer = new RPCServer({
protocols: ['ocpp1.6'],
strictMode: false
});

rpcServer.on('client', client => {
// RPC client connected
client.call('Say', Hello, ${client.identity}!);

// create a specific handler for handling Heartbeat requests
client.handle('Heartbeat', ({ params }) => {
    console.log(`Server got Heartbeat from ${client.identity}:`, params);

    // respond with the server's current time.
    return {
        currentTime: new Date().toISOString()
    };

});

client.once('close', () => {
    // check that we're about to delete the correct client
    return "client disconnected"
});

});

module.exports = rpcServer `

api.js

`const express = require('express');
const rpcServer = require("./rpc")

const serverRouter = express.Router();

serverRouter.use(express.json())

// when clients connect (after auth), store them in a Map based on their identity
const clients = new Map();

rpcServer.on('client', client => {
clients.set(client.identity, client);

// when clients disconnect, remove them from our Map
client.once('close', () => {
    // check that we're about to delete the correct client
    if (clients.get(client.identity) === client) {
        clients.delete(client.identity);
    }
});

});

serverRouter.post('/reset/:cp', async (req, res, next) => {

const cp = req.params.cp;
const client = clients.get(cp);
console.log(cp)
const result = await client.call('Reset', { type: 'Hard' });
res.json({
    Status: "Success"
});

});

module.exports = serverRouter`

@mikuso
Copy link
Owner

mikuso commented Mar 21, 2024

Hi @mardommah ,

I wasn't able to reproduce this issue with the code you've provided. I have attempted to connect a simple client to localhost:3002 but it does not trigger the error.

Can you give me more details of how to reproduce the error you've reported, using this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants