Possible for WebSocket client on browser to talk to TCP Socket server?
Asked Answered
R

2

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?

Riker answered 1/3, 2013 at 15:3 Comment(0)
A
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.

Ackerman answered 1/3, 2013 at 15:14 Comment(4)
So in short, no its not possible?Riker
It is possible, but with a lot of extra work. It will not work "out of the box" so to speak.Ackerman
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).Puebla
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".Ackerman
P
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

Puebla answered 1/3, 2013 at 16:47 Comment(10)
@jaffa, yes, that's the main use case.Puebla
But this is like a proxy? Just like artemyankov.com/tcp-client-for-browsers?Tennies
@Tennies 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 github.com/kanaka/websockify/issues/3 websockify has large deployments (e.g. OpenStack).Puebla
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)?Lawn
@Lawn your server either needs to support WebSockets or you need to use something like websockify to bridge between WebSockets and TCP.Puebla
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 softwareLawn
Websockify is broken and it doesn't work. Maybe some alternative should be offered.Disservice
@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?Puebla
@Puebla 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.Disservice
@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 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: github.com/novnc/websockify/blob/master/Windows/… However, I have not followed the current state of that version in years.Puebla
A
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.

Ackerman answered 1/3, 2013 at 15:14 Comment(4)
So in short, no its not possible?Riker
It is possible, but with a lot of extra work. It will not work "out of the box" so to speak.Ackerman
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).Puebla
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".Ackerman

© 2022 - 2024 — McMap. All rights reserved.