首页 > 解决方案 > WebSocket Client Side Keeps reconnecting , without invoking on() events

问题描述

Hello i have a nodeJs/express Server !

my socket server side :

im using https by the way. We have Cloudflare linked too.

const server            = require("http").createServer(app);
const WebSocket         = require('ws');
const wss               = new WebSocket.Server({ server });

wss.on('connection', (ws) => {
    console.log('New connection', ws.url);

    //connection is up, let's add a simple simple event
    ws.on('message', (message) => {

        //log the received message and send it back to the client
        console.log('received: %s', message);
        ws.send(`Hello, you sent -> ${message}`);
    });

    //send immediatly a feedback to the incoming connection    
    ws.send('Hi there, I am a WebSocket server');
});

I tried connecting to the socket on : https://websocketking.com and it worked super fine !

enter image description here

now I made an express app that is working with : socket.io-client

app.listen(3001, '0.0.0.0', () => 
{
    console.log('App listening on 3001 !');
    socket = io('https://example.com/', {path: '/notify/' , transports:['websocket']  });
    
    socket.on("connect", (dt) => {
        console.log("Connected Successfully --> ", dt);
    });
    
    socket.on('message' , (dt) => {
        console.info("Got Data from Server ==> ",dt.toString());
    });

    socket.on('data' , (dt) => {
        console.info("Got Data from Server ==> ",dt.toString());
    });
});

the thing is it keeps sending New connection .. I do not know why ! Here a Screenshot from server logs :

enter image description here

it just keeps reconnecting ..

And none of the events are invoked !

I spent ~ 24h now with this websockets things , any help would be appreciated !

Thank you .

标签: node.jswebsocketsocket.iocloudflare

解决方案


推荐阅读