Error during WebSocket handshake: 'Connection' header is missing

前端 未结 1 812
萌比男神i
萌比男神i 2021-01-27 04:26

I\'m completely new to Node, and even newer to Socket.io. I just got my first chat application online at spaiglas.com, which works despite an error in the developer console that

相关标签:
1条回答
  • 2021-01-27 04:36

    You need to initialize socket.io before you start your HTTP server that is listening on port 80. In the docs it is...

    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    

    And then later, you call http.listen(3000, ...). This will start the socket.

    On the client, your path is also incorrect. Websockets is a protocol (ws://somedomain.com), just like HTTP. When you connect via http:// that is a different protocol. I would either not pass it anything, or use namespaces which you can find in the socket.io documentation.

    Socket.io reverts to long polling and sometimes doesn't even use websockets (which is partly why it is generally slower than libraries like ws on npm). Because of that, passing http:// into the io(...) on the client may not be wrong with their library, but I would remove it anyway and do my above suggestion.

    0 讨论(0)
提交回复
热议问题