简体   繁体   English

使用WebSocket连接到标准套接字

[英]Connect to standard socket using WebSocket

I have a client interface to a hardware device that communicates over a standard TCP socket. 我有一个通过标准TCP套接字进行通信的硬件设备的客户端接口。 Is it possible to connect to this using WebSocket in a browser like Chrome? 是否可以在Chrome浏览器中使用WebSocket连接到此? 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? 问题是WebSockets使用自己的标准本地套接字不熟悉的协议吗? I have tried specifying other protocols ('http://...' and 'tcp://...') but this has not worked. 我已经尝试指定其他协议('http:// ...'和'tcp:// ...'),但这没有用。

As thejh already explained, WebSockets don't transmit data as you provide it, but add framing and masking to the data stream. 正如已经解释的那样,WebSockets不会像你提供的那样传输数据,而是为数据流添加成帧和屏蔽。 Thus it is not possible to use websockets to communicate with applications which don't implement the WebSocket protocol. 因此,不可能使用websockets与不实现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. 但是有一种解决方法: Websockify服务器可以用作代理,以桥接websocket客户端和非websocket服务器。 The client doesn't communicate with the application directly, but with a websockify server instead. 客户端不直接与应用程序通信,而是使用websockify服务器。 Websockify unpacks the Websocket data stream and relays it to the server. Websockify解压缩Websocket数据流并将其中继到服务器。 The response from the server is then packed into WebSocket frames and sent to the client. 然后将来自服务器的响应打包到WebSocket帧中并发送到客户端。

Web Sockets use a special protocol. Web套接字使用特殊协议。 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. 例如,任何websocket连接都作为HTTP连接启动,并在双方同意通过HTTP更改为websocket协议后更改为websocket连接。 You can't talk to arbitrary TCP servers using websockets, sorry. 抱歉,您无法使用websockets与任意TCP服务器通信。 If you want to do something like that, you'll probably have to use flash or a Java applet. 如果你想做类似的事情,你可能不得不使用flash或Java applet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM