使用WebSocket连接到标准套接字 [英] Connect to standard socket using WebSocket

查看:134
本文介绍了使用WebSocket连接到标准套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过标准TCP套接字进行通信的硬件设备的客户端接口。是否有可能在Chrome浏览器中使用WebSocket连接到这个?这里是我用来连接的代码:

  var ws = new WebSocket(ws://127.0.0.1:13854 ); 
ws.onopen = function()...

客户端到目前为止失败第一行。 WebSockets是否使用标准本地套接字不熟悉的协议?我已经尝试过指定其他协议('http:// ...'和'tcp:// ...'),但是这并没有奏效。

解决方案

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

但有一个解决方法:A Websockify服务器可以用作桥接websocket客户端和非websocket服务器的代理。客户端不直接与应用程序通信,而是通过websockify服务器进行通信。 Websockify解包Websocket数据流并将其中继到服务器。然后将来自服务器的响应打包到WebSocket框架中并发送给客户端。


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.

解决方案

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

这篇关于使用WebSocket连接到标准套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆