I have a client interface to a hardware device that municates over a standard TCP socket. Is it possible to connect to this using WebSocket in a browser like Chrome? Here's the code I am using to connect:
var ws = new WebSocket("ws://127.0.0.1:13854");
ws.onopen = function() ...
The client just fails so far on the first line. Is the problem that WebSockets use their own protocol which a standard local socket would be unfamiliar with? I have tried specifying other protocols ('http://...' and 'tcp://...') but this has not worked.
I have a client interface to a hardware device that municates over a standard TCP socket. Is it possible to connect to this using WebSocket in a browser like Chrome? Here's the code I am using to connect:
var ws = new WebSocket("ws://127.0.0.1:13854");
ws.onopen = function() ...
The client just fails so far on the first line. Is the problem that WebSockets use their own protocol which a standard local socket would be unfamiliar with? I have tried specifying other protocols ('http://...' and 'tcp://...') but this has not worked.
Share Improve this question asked Mar 29, 2013 at 14:15 RonanODRonanOD 8851 gold badge10 silver badges19 bronze badges 1- 2 WebSocket is a special protocol that uses HTTP for handshake and use the underlaying socket to do the real munication afterwards. So, you cannot connect to a regular tcp socket with websocket because they are two different things. – Licson Commented Mar 29, 2013 at 14:20
2 Answers
Reset to default 11As thejh already explained, WebSockets don't transmit data as you provide it, but add framing and masking to the data stream. Thus it is not possible to use websockets to municate with applications which don't implement the WebSocket protocol.
But there is a workaround: A Websockify server can be used as a proxy to bridge between a websocket client and a non-websocket server. The client doesn't municate with the application directly, but with a websockify server instead. Websockify unpacks the Websocket data stream and relays it to the server. The response from the server is then packed into WebSocket frames and sent to the client.
Web Sockets use a special protocol. For example, any websocket connection starts as an HTTP connection and is changed to a websocket connection after both parties have agreed to change to the websocket protocol over HTTP. You can't talk to arbitrary TCP servers using websockets, sorry. If you want to do something like that, you'll probably have to use flash or a Java applet.