TCP 套接字到 Websocket? [英] TCP Socket to Websocket?

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

问题描述

那里有很多 websocket -> 套接字包装器(例如 websockify)但是否有相反的可用吗?具体来说,我希望能够使用应用程序连接到 TCP 套接字,并将代理转换为 websocket 并转移到另一个 websocket 服务器.

There are plenty of websocket -> socket wrappers around there (like websockify) but is there an opposite available out there? Specifically, I want to be able to connect to a TCP socket with an application, and have the proxy translate to websocket and over to another websocket server.

推荐答案

从 WebSocket 客户端到 TCP 服务器的桥接是一种通用解决方案,它将较低级别的 TCP 协议封装到较高级别的 WebSocket 协议中.它允许浏览器(或其他 websocket 客户端)与任意 TCP 服务器通信.请注意,客户端 (JavaScript) 应用程序仍必须能够解码/编码 TCP 服务器所说的协议.

Bridging from WebSocket client to TCP server is a generic solution that encapsulates the lower level TCP protocol into the higher level WebSocket protocol. It allows browsers (or other websocket clients) to communicate with arbitrary TCP servers. Note that the the client (JavaScript) application must still be able to decode/encode the protocol that the TCP server speaks.

反向操作不是通用的,需要对从 TCP 客户端应用程序到网桥的消息进行特殊的帧处理,以便网桥知道如何将 WebSocket 消息编码到 TCP 服务器.WebSockets 是基于消息的传输,而 TCP 是较低级别的流传输.TCP 传输层在协议本身中没有消息的概念,因此必须在高层进行处理.换句话说,要使 TCP 客户端应用程序能够与桥接应用程序进行通信,您将不得不做几乎与直接在应用程序中实现 WebSocket 客户端所需的工作一样多的工作.实际上,直接实现可能更少,因为已经有适用于大多数流行语言的 WebSocket 客户端库.

The reverse operation is not generic and would require a special framing of the messages from the TCP client application to the bridge so that the bridge would know how to encode the WebSocket messages to the TCP server. WebSockets is a message based transport while TCP is a lower-level streaming transport. The TCP transport layer does not have a concept of messages in the protocol itself so this has to be handled at a high layer. In other words, you will have to do almost as much work to enable your TCP client application to communicate with the bridge application as it would take to implementing the a WebSocket client directly in your application. Actually, it's probably less to implement directly, because there are already WebSocket client libraries available for most popular languages.

如果不更改客户端和桥接器(以添加消息边界和操作码信息),您将无法通过桥接器将预先存在的 TCP 客户端连接到现有的 WebSocket 服务器,否则您将需要一个特殊的 WebSocket 服务器忽略 WebSiocket 消息边界并将传入数据视为流(在更高层处理消息解析).

You won't be able to connect a pre-existing TCP client via the bridge to an existing WebSocket server without changing either the client and bridge (to add message boundary and opcode information) or you will need a special WebSocket server that ignores the WebSiocket message boundaries and treats the incoming data as a stream (with message parsing handled at a higher layer).

也许您可以给出一个您认为这可能有用的用例?

Perhaps you could give an use-case where you think this might be useful?

免责声明:我制作了 websockify.

Disclaimer: I made websockify.

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

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