Coder Social home page Coder Social logo

Comments (5)

kanaka avatar kanaka commented on May 17, 2024

Websockify is made up of three parts:

  • websocket.py: a general websocket server library/module to incorporate into other python programs to turn them into python servers
  • websockify: a runnable program that is a WebSocket to TCP proxy/bridge that uses websocket.py to communicate with the browser and makes regular TCP socket connections on the back-end to the server being bridged.
  • include/websock.js: a Javascript library that makes it easy to interface with websocket.py. It provides an interface that is different from normal WebSocket. The API is documented here: https://github.com/kanaka/websockify/wiki/websock.js

The WebSocket protocol is message based. When you send 1 message you will receive 1 message (i.e. onmessage will fire once). websock.js+websocket.py is designed to handle binary data that is either streaming or message based data. For that reason all data received is pushed onto a receive queue. If you want to process a message at a time then you will need to rQshiftBytes() or rQshiftStr() all the data off of the receive queue every time you receive a message (every time the message handler fires).

However, TCP is streaming and not message based. When you send TCP data, it is all guaranteed to arrive at the destination (and in order) but it may get fragmented into multiple chunks or multiple chunks may be combined into fewer chunks.

So when you use websockify as a proxy/bridge program to a TCP server, you will lose any notion of messages. In other words, you will need to implement your own protocol/message/framing handling in your client such that it understands the data coming from the target TCP server and is able to re-assemble the chunks into the proper pieces. For example, if the only thing that your server is sending is a series of PNG images, your client can look into the inital bytes of the PNG header and pull out the length. Then you can shift off that many bytes from the receive queue (if there are enough, otherwise you have to wait for more frames to arrive). Or if you have varying types of data you are sending, you may need to add your own header to each message that contains the length of the message so you know how long it is.

Alternately, since you have a python server, you could incorporate websocket.py into your python server. When you call send_frames() it takes a list of frames/messages to send. Each of those items from the list will arrive as a single message in the browser.

from websockify.

ntenisOT avatar ntenisOT commented on May 17, 2024

Thanks a lot for the comprehensive respond. Is it possible to provide a sample code on how to get the length of the PNG image and wait for more frames to arrive? I am only sending PNG images.

PS. What if I incorporate websocket.py into my python server and then use websockify to proxy the data through a second server. Will I receive all frames in one message?

from websockify.

kanaka avatar kanaka commented on May 17, 2024

Actually, I forgot, PNG doesn't have an overall length of the whole file. It only has a length for each chunk inside the file. That's a lot more work. I suggest adding a 4 byte length of the whole file before each image. More information about the PNG format:

If you use websockify, then part of your transport will be TCP. If you have TCP in the mix anywhere, and you want to handle messages instead of streams of bytes, then you need to handle this yourself. So unless you have WebSocket all the way to your server, you will need to handle messaging/framing yourself. You could use a WebSocket to WebSocket proxy if you are wanting to have a server in the middle, but this is not the purpose of websockify.

from websockify.

ntenisOT avatar ntenisOT commented on May 17, 2024

Thanks kanaka I will follow your suggestion. Thanks a lot once again for your help!

from websockify.

kanaka avatar kanaka commented on May 17, 2024

I'm closing this. Feel free to post here if you have any related questions (I'll see it in my feed).

from websockify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.