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

RPCServer 'upgradeAborted' event can fire after websocket upgrade is successful #64

Open
mikuso opened this issue Sep 15, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@mikuso
Copy link
Owner

mikuso commented Sep 15, 2023

As per title, the RPCServer's 'upgradeAborted' event can fire after websocket upgrade was successful.

@mikuso mikuso added the bug Something isn't working label Sep 15, 2023
@Mery042
Copy link

Mery042 commented Apr 5, 2024

Hello @mikuso,
I'm not sure if it is related to this bug but when I connect to the RPC Server with a subprotocol that is not allowed, the event 'client' is fired and some of my server logic is executed. Should it not be 'upgradeAborted' directly instead ?

Also, the server answers with a HTTP response that has no Sec-WebSocket-Protocol header but there is a Sec-WebSocket-Accept.

Server code :

const { RPCServer, createRPCError } = require('ocpp-rpc');
const express = require('express')

const app = express();
app.get("/charge_stations", getChargeStations);
const httpServer = app.listen(3000, 'localhost');

const rpcServer = new RPCServer({
    protocols: ['ocpp1.6', 'ocpp2.0.1'], // rpcServer accepts ocpp1.6 and ocpp2.0.1 subprotocols
    strictMode: true,       // enable strict validation of requests & responses
});

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

rpcServer.auth((accept, reject, handshake) => {
    if (handshake.endpoint === '/ocpp') {
        accept();
    } else {
        console.log("Wrong OCPP endpoint")
        reject(400, "Wrong OCPP endpoint");
    }
});

rpcServer.on('client', async (client) => {
    console.log(`${client.identity} connected!`);

    // create a specific handler for handling BootNotification requests
    client.handle('BootNotification', ({params}) => {
        console.log(`rpcServer got BootNotification from ${client.identity}:`, params);
        
        // respond to accept the client
        return {
            status: "Accepted",
            interval: 300,
            currentTime: new Date().toISOString()
        };
    });
    
    // create a specific handler for handling Heartbeat requests
    client.handle('Heartbeat', ({params}) => {
        console.log(`rpcServer got Heartbeat from ${client.identity}:`, params);

        // respond with the rpcServer's current time.
        return {
            currentTime: new Date().toISOString()
        };
    });
    
    // create a specific handler for handling StatusNotification requests
    client.handle('StatusNotification', ({params}) => {
        console.log(`rpcServer got StatusNotification from ${client.identity}:`, params);
        return {};
    });

    // create a wildcard handler to handle any RPC method
    client.handle(({method, params}) => {
        // This handler will be called if the incoming method cannot be handled elsewhere.
        console.log(`rpcServer got ${method} from ${client.identity}:`, params);

        // throw an RPC error to inform the rpcServer that we don't understand the request.
        throw createRPCError("NotImplemented");
    });
});


function getChargeStations(req, res){
    res.send('Hello !')
}

Postman request :

Here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants