6

Is it possible to have a TCP Socket server running which accepts incoming connections from WebSocket clients? I have a TCP Socket server and I'd like to be able to test this in a browser. Is this possible?

jaffa
  • 26,770
  • 50
  • 178
  • 289

2 Answers2

9

Absolutely! That is the purpose of websockify.

Of course your WebSocket client application will need to be able to implement the protocol of the TCP server. For example, noVNC is a browser VNC client that implements the RFB protocol and by using websockify it can connect to a normal TCP based VNC server.

Disclaimer: I created both websockify and noVNC

kanaka
  • 70,845
  • 23
  • 144
  • 140
  • 1
    @jaffa, yes, that's the main use case. – kanaka Mar 01 '13 at 19:13
  • But this is like a proxy? Just like http://artemyankov.com/tcp-client-for-browsers/? – HackNone Mar 20 '15 at 00:32
  • @HackNone technically a "bridge" (different protocols on each side), but yes it's similar to WebTCP (linked in that article). Just a few differences: WebTCP is both younger and hasn't had updates in the past year. WebTCP is implemented in JS/node, websockify has implementations in several languages (including JS). websockify is lower level (raw websock data, no JSON message wrappers). WebTCP has a huge security issue because it allows browser to connect to arbitrary remote destinations; see https://github.com/kanaka/websockify/issues/3 websockify has large deployments (e.g. OpenStack). – kanaka Mar 20 '15 at 13:19
  • So you're saying I can speak to a program using TCP, just through WebSockets? Does that mean my Server doesn't even need WebSockets (just my browser)? – Cameron Apr 09 '18 at 06:00
  • @Cameron your server either needs to support WebSockets or you need to use something like websockify to bridge between WebSockets and TCP. – kanaka Apr 09 '18 at 18:08
  • Yes. I think I was just confused initially. It sounded like I could talk directly to a socket using a websocket client. But that’s not the case. However, with the websockify bridge it’s actually not much extra work at all. I ended up demoing it in one my network engineering classes and it seemed like everyone was really impressed so thanks for making this software – Cameron Apr 09 '18 at 18:16
  • Websockify is broken and it doesn't work. Maybe some alternative should be offered. – Predrag Manojlovic Apr 05 '21 at 22:35
  • @PredragManojlovic websockify works and has been used in production for years. Can you please be more specific or ask the upstream project maintainers (I no longer am) if you have a specific context where you are having trouble with it? – kanaka Apr 06 '21 at 15:15
  • @kanaka Latest release 0.9.0 doesn't work in any context on windows. Even the simplest example will drop the "Client disconnect" error. Before you ask, I've been using a binary frame. On the other side, the 0.8.0 version won't run on python 3 as it is complaining about the missing module SocketServer. On Linux it does work. That is why I'm looking for a more stable solution. – Predrag Manojlovic Apr 06 '21 at 19:16
  • @PredragManojlovic Websockify implements a very simple protocol. There are implementations of websockify in just about every common language. Some of them are under the official project on github at https://github.com/noVNC, but many are in language specific package repositories. Some of them definitely work on Windows. I think the default python one can be made to run on Windows: https://github.com/novnc/websockify/blob/master/Windows/Windows%20Service%20Readme.md However, I have not followed the current state of that version in years. – kanaka Apr 08 '21 at 12:30
7

TCP and WebSocket are not the same protocol or framing, so wiring them up blindly isn't going to work. Well ... technically, websocket is an upgrade of http which is layered on ssl (optionally) which in turn is layered on tcp.

TCP can be thought of as a stream of bytes, while WebSocket is a set frames.

WebSocket frames can be any of the following:

  • TEXT - consists of 1 or more frames making up a single UTF8 message
  • BINARY - consists of 1 or more frames making up a byte array message
  • CONTINUATION - used by TEXT and BINARY to piece together 2 or more frames.
  • PING - obvious
  • PONG - obvious
  • CLOSE - request a close/disconnect of the protocol.

In short, you'd need to implement the websocket protocol framing in order to have TCP wired up to websocket. And you'll need to implement basic HTTP UPGRADE in order to reach that point.

Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • So in short, no its not possible? – jaffa Mar 01 '13 at 16:07
  • 1
    It is possible, but with a lot of extra work. It will not work "out of the box" so to speak. – Joakim Erdfelt Mar 01 '13 at 16:12
  • To clarify, it is not stream vs frames, it's stream vs messages. The TCP protocol carries a stream of bytes that must be re-assembled by the application into the original messages. The WebSocket protocol carries messages so each message that is sent is received as a single whole message. The term "framing" is a concept that applies to both TCP and WebSockets (any wire protocol actually). – kanaka Mar 01 '13 at 16:51
  • To websocket endpoint, a message is a series of frames. To the websocket protocol, it's just frames. In fact the spec even separates the behaviors to "Opening Handshake", "Data Framing", and "Closing the Connection". – Joakim Erdfelt Nov 17 '22 at 22:12