Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP Socket to Websocket?

Tags:

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.

like image 890
Morty Kirk Avatar asked Feb 18 '13 01:02

Morty Kirk


1 Answers

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.

like image 190
kanaka Avatar answered Sep 21 '22 16:09

kanaka