0

Iam trying to do OCPP protocol (client side) development using JavaScript. So i tried websocket based client server communication. It is not connecting with the server. when i try to connect client with the server the following error occurs on the server side

Connection has been closed for <ChargeBoxID> close reason code [1006] reason [connection reset by peer]. 

I tried to solve it using WS.onclose event code 1006.but i unable to solve it.

Here is my client side code i got the following error

async function webSockConnect() {

    let PingTimer;
    let pingTimeout;

    delete WS;

    const url = ChargepointConfig.HostAddress + '/' + ChargepointConfig.ChargeBoxID;

    WS = new WebSocket(url, "<protocol name>", { perMessageDeflate: false });


    console.log('Host Address-->', url);


    WS.onopen = () => {

        console.log('WS connected-->');

        CMSConnectionState = true;

       


    };

    WS.on('pong', (pingData) => {

        CMSConnectionState = true;

        // console.log('pingData-->', pingData.toString());

        clearTimeout(pingTimeout);

        pingTimeout = setTimeout(() => {

            console.log('CMS Offline');

            WS.terminate();

        }, 3 * 1000);

    });


    WS.onmessage = (WSDataframe) => {


        let WSData = new PDU(JSON.parse(WSDataframe.data));


        try {

            console.log('525', WSData.PayLoad);

        } catch (error) {

            console.log('527', WSData);

        }

        switch (WSData.MessageTypeId) {

            case 2:

                let schema = SchemaList[WSData.Action];

                let valid = ajv.validate(schema, WSData.PayLoad);


                if (!valid) {

                    let errMsg = ajv.errors;
                    let data = [
                        4,
                        WSData.UniqueId,
                        'FormationViolation',
                        'The payload for action could not be deserialized',
                        { "errorMsg": errMsg }
                    ];

                    // console.log('errMsg-->',errMsg);

                    WS.send(JSON.stringify(data));

                } else {

                    requestHandler(WSData);

                }
                break;

            case 3:

                responseHandler(WSData);

                break;

            default:
                console.log('CALLERROR')
                break;
        }

    };


    WS.onerror = (error) => {
    
        // console.log(`WebSocket error: ${error}`)
        // console.log('WebSocket error--->', error)

        console.log('WSerror-->', CMSConnectionState);

        CMSConnectionState = false;

        WS.close();

    };


    WS.onclose = (evt) => {
         
         console.log('event code -->', evt.code);
         
       /* if(evt.code == 1006){
            
                console.log('event code -->', evt.code);
                setTimeout(webSockConnect,60000);
              /*  setTimeout(() => {

                webSockConnect();

                }, 60 * 1000);    */           

     //   }
     //   else
     //   {
        console.log('WSClosed-->', CMSConnectionState);

        CMSConnectionState = false;

        clearTimeout(pingTimeout);
        clearInterval(PingTimer);

        setTimeout(() => {

            webSockConnect();

        }, 60 * 1000);  

    };
V_J viji
  • 17
  • 6
  • when you did research before asking here, did you happen to stumble on [this](https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006) – Bravo Jul 28 '21 at 12:18

0 Answers0