在node安裝了Socket.IO,做demo時報錯
WebSocket connection to 'ws://localhost:8999/socket.io/?EIO=3&transport=websocket&sid=D0fxLuPwjHWgYSI7AAAB' failed: Error during WebSocket handshake: Unexpected response code: 400
WebSocket 握手時發生錯誤,詭異的是我前端頁面沒有關閉,重啟服務器node app.js后socket重新連上了
經多次測試驗證,只有登陸進入才會報錯,但是之后Socket 是處在連接狀態的如圖,連接不是很穩定,cookie也可以獲取到。重啟之后可以socket自動連上。
這就奇怪了。。折騰了許久,另外寫了個更簡單的demo, 只要必要的express和socket.io, 發現沒問題。
然后排查,最后找到了這貨
express-status-monitor
基於socket.io和chat.js的服務器監控器
可能會存在沖突,把這個注釋之后,可以了!
然后在express-status-monitor, 找到了原因:
If you're using socket.io in your project, this module could break your project because this module by default will spawn its own socket.io instance. To mitigate that, fill websocket parameter with your main socket.io instance as well as port parameter.