我偶尔发现我的socket.io(2.1)服务器端有连接事件,因为我看到了日志,而在客户端我没有看到日志。从chrome网络窗格我看到客户端继续发送轮询请求(因为websocket失败)并且得到200 ok。
那么当客户端没有发送连接事件时,服务器怎样才能获得连接事件?我该如何调试问题
//server-side
io.on('connection', function (socket) {
logger.debug('a user connected ' + socket.id);
...
//client-side
socket.on('connect', () => {
console.log("socket connect")
...
//from network pane
request URL: http://localhost:5000/socket.io/?EIO=3&transport=polling&t=MCunKN5&sid=SmOL7JbUlSqw6MwZAAAL
Request Method: POST
在客户端,我使用 webpack-dev-server 委派我的快递服务器。
----更新------
我找到webpack-dev-serve use socketjs,而不是socket.io。不确定是否可能是原因。这是我在服务器端获得的日志:
GET /cs/refresh 200 - - 37.947 ms
socket.io:server incoming connection with id tTOIQou49WeZ-l92AAAL
socket.io:client connecting to namespace /
socket.io:namespace adding socket to nsp /
socket.io:socket socket connected - writing packet
socket.io:socket joining room tTOIQou49WeZ-l92AAAL
socket.io:socket packet already sent in initial handshake
socket.io:socket joined room tTOIQou49WeZ-l92AAAL
socket.io-parser decoded 0/5975b17697e60c5d2403a202, as {"type":0,"nsp":"/5975b17697e60c5d2403a202"}
socket.io:client connecting to namespace /5975b17697e60c5d2403a202
socket.io:namespace adding socket to nsp /5975b17697e60c5d2403a202
socket.io:socket socket connected - writing packet
socket.io:socket joining room /5975b17697e60c5d2403a202#tTOIQou49WeZ-l92AAAL
socket.io:client writing packet {"type":0,"nsp":"/5975b17697e60c5d2403a202"}
socket.io-parser encoding packet {"type":0,"nsp":"/5975b17697e60c5d2403a202"}
socket.io-parser encoded {"type":0,"nsp":"/5975b17697e60c5d2403a202"} as 0/5975b17697e60c5d2403a202,
- debug: a user connected /5975b17697e60c5d2403a202#tTOIQou49WeZ-l92AAAL
socket.io:socket joined room /5975b17697e60c5d2403a202#tTOIQou49WeZ-l92AAAL
在客户端