6

I have a client interface to a hardware device that communicates 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.

4

2 回答 2

9

正如thejh已经解释的那样,WebSockets 不会在您提供数据时传输数据,而是向数据流添加框架和掩码。因此,不可能使用 websocket 与未实现 WebSocket 协议的应用程序进行通信。

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 communicate 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.

于 2013-04-02T14:14:36.100 回答
4

Web Sockets 使用一种特殊的协议。例如,任何 websocket 连接都以 HTTP 连接开始,并在双方同意通过 HTTP 更改为 websocket 协议后更改为 websocket 连接。抱歉,您不能使用 websocket 与任意 TCP 服务器通信。如果您想做类似的事情,您可能必须使用 Flash 或 Java 小程序。

于 2013-03-29T14:19:35.963 回答