TCP Socket to Websocket?

后端 未结 3 1956
一生所求
一生所求 2021-02-20 06:07

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

3条回答
  •  终归单人心
    2021-02-20 06:52

    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.

    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.

    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?

    Disclaimer: I made websockify.

提交回复
热议问题