使用纯HTML5的WebSocket API去访问Socket.io的WebSocket服务器怎么总是报错呢?
发布于 10 年前 作者 piginzoo 24808 次浏览 最后一次编辑是 8 年前

我们都知道socket.io标榜支持WebSocket,于是乎我就做了一个Socket.io服务器,然后我用标准的HTML5的WebSocket访问,结果就抱错: ** WebSocket connection to ‘ws://localhost:8080/’ failed: Connection closed before receiving a handshake response ** 可是用Socket.io的客户端就一点问题都没有哇,唉,怎么回事啊?哪位大拿帮着看看。

测试代码在https://github.com/piginzoo/socketiotest

问题类似于这篇:http://cnodejs.org/topic/538d9bbcc3ee0b58204d6dea @ 514366607,你找到解决方法了么?

3 回复

WebSocket协议大拿和Nodejs/Socket.io大拿请跳出啦噢~

WebSocket客户端访问服务器时候的服务器端的debug信息

engine:ws received "2" +24s
  engine:socket packet +24s
  engine:socket got ping +0ms
  engine:socket sending packet "pong" (undefined) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "3" +0ms
  socket.io:client client close with reason transport close +25s
  socket.io:socket closing socket - reason transport close +25s
  socket.io:client ignoring remove for xlXY3VlzrBGRdOLpAAAB +0ms
  socket.io:server serve client source +25s

Socket.io的标准客户端访问的时候,服务器侧的debug信息

socket.io:server serve client source +1.5m
  engine intercepting request for path "/socket.io/" +1.9m
  engine handling "GET" http request "/socket.io/?EIO=2&transport=polling&t=1407139373806-0" +0ms
  engine handshaking client "L-etOWnlnNNmuOxiAAAC" +0ms
  engine:socket sending packet "open" ({"sid":"L-etOWnlnNNmuOxiAAAC","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +1.5m
  engine:polling setting request +1.9m
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "	�0{"sid":"L-etOWnlnNNmuOxiAAAC","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +0ms
  engine:socket executing batch send callback +0ms
  socket.io:server incoming connection with id L-etOWnlnNNmuOxiAAAC +27ms
  socket.io:client connecting to namespace / +1.5m
  socket.io:namespace adding socket to nsp / +1.9m
  socket.io:socket socket connected - writing packet +1.5m
  socket.io:socket joining room L-etOWnlnNNmuOxiAAAC +0ms
  socket.io:client writing packet {"type":0,"nsp":"/"} +0ms
  socket.io-parser encoding packet {"type":0,"nsp":"/"} +1.9m
  socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +1ms
  engine:socket sending packet "message" (0) +4ms
Client connected the SocketIO server!!!
  socket.io:socket joined room L-etOWnlnNNmuOxiAAAC +4ms
  engine upgrading existing transport +44ms
  engine:socket might upgrade socket transport from "polling" to "websocket" +40ms
  engine intercepting request for path "/socket.io/" +1ms
  engine handling "GET" http request "/socket.io/?EIO=2&transport=polling&t=1407139373849-1&sid=L-etOWnlnNNmuOxiAAAC" +0ms
  engine setting new request for existing client +0ms
  engine:polling setting request +45ms
  engine:socket flushing buffer to transport +1ms
  engine:polling writing "�40" +0ms
  engine:socket executing batch send callback +1ms
  engine:ws received "2probe" +1.5m
  engine:ws writing "3probe" +0ms
  engine intercepting request for path "/socket.io/" +88ms
  engine handling "GET" http request "/socket.io/?EIO=2&transport=polling&t=1407139373939-2&sid=L-etOWnlnNNmuOxiAAAC" +0ms
  engine setting new request for existing client +0ms
  engine:polling setting request +88ms
  engine:socket writing a noop packet to polling for fast upgrade +181ms
  engine:polling writing "�6" +95ms
  engine:ws received "5" +104ms
  engine:socket got upgrade packet - upgrading +5ms

貌似直接用WebSocket确实没有handshake,看上面的trace,也看到这问题,可是为什么呢?难道是WebSocket建立的有问题,可是Websocket的API就是这么简单啊

请问楼主解决这个问题没有啊?

回到顶部