Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

websocket client to socket.io

I need a server to be able to accept connections from both websocket and socket.io clients, is that possible? When I'm running just socket.io, also socket.io client works fine, but standard websocket client is unable to connect. On the other hand when I run websocket server alongside socket.io, websocket worsk well, but in browser, which is trying to connect via socket.io, I see error WebSocket connection to ... failed: Invalid frame header. Is it possible to get both connections working on single server instance?

I'm using express.io and websocket-node and it would be great to get it working only with express.oi.

like image 633
Peter Krejci Avatar asked Apr 13 '15 11:04

Peter Krejci


1 Answers

While @jfriend00 is right that socket.io is an additional protocol on top of webSocket, I found the additional protocol might be quite simple. I think [email protected] server implicitly runs a WebSocket server powered by [email protected], I managed to (with the help of F12/network tool) connect to it with a native WebSocket client.

socket.io client:

var socket = io('http://localhost');
socket.emit('hello', 'there');

websocket client:

var ws = new WebSocket('ws://localhost/socket.io/?EIO=3&transport=websocket');
ws.send('42' + JSON.stringify(['hello', 'there']));
// ws.onmessage will get a MessageEvent object with the data property being encoded in the similar way.

Socket.IO API is more abstract, it deals with event and args, while WebSocket API deals with string.

like image 58
Zhiyong Avatar answered Oct 12 '22 20:10

Zhiyong