Socket.IOとWebSocketは通信できない
タイトルの通りです。
Node.jsでWebSocketサーバを作る機会がありまして、有名なSocket.IOを採用しました。
私のSocket.IOの認識は以下でした。
- WebSocketをラップしたライブラリで、接続できないときはポーリングなどに自動で切り替えてくれる
- 普通のWebSocketサーバとも通信できる
しかし、実際にはSocket.IOはSocket.IOとしか通信できません。
改めて公式を見ると、ちゃんと書いてあります。
WebSocketクライアントとSocket.IOサーバの通信、あるいはその逆は、不可と読めますね。
What Socket.IO is not
Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server either. Please see the protocol specification here.
常識なのかもしれませんが・・・
他に間違える人が少しでも減るよう、記事として残しておきます。