Skip to content Skip to sidebar Skip to footer

Sending And Receiving Frames Over The Same Websocket Connection Without Blocking

Sorry for the long post but I've been poking at this for over a week so I've tried a lot of different stuff. I know Python well enough but I don't have any experience with asyncio

Solution 1:

I am in the exact same situation as u. I know that this is a very inelegant solution because it still isn't full-duplex but i can't seem to find any example on the internet or stackoverflow involving asyncio and the websockets module which i used.

I don't think i completely understand your websockets example (is that server-side or client-side code?) but i'm going to explain my situation and "solution" and maybe that would be usable for you too.

So i have a server main function that has a websocket listening for messages in a loop with recv(). When i send "start" it will start a function that will send data every second to the javascript client in the browser. But while the function is sending data i sometimes want to pause or stop the stream of data from my client be sending a stop message. The problem is that when i use recv() while the data sending has already begun the server stops sending data and only waits for a message. I tried threads,multiprocessing and some other stuff but eventually i came to the hopefully temporarily solution of sending a "pong" message to the server immediately after the client receives a piece of data so that the server continues sending data at the next loop iteration or stop sending data if the "pong" message is "stop" instead for example but yeah this is not real duplex just fast half-duplex...

code on my python "server"

asyncdefstart_server(self,websocket,webserver_path):
    self.websocket = websocket
    self.webserver_path = webserver_path
    whileTrue:
        command = await self.websocket.recv()
        print("received command")
        if command == "start":
            await self.analyze()
        asyncio.sleep(1)

in my analyze function:

for i,row inenumerate(data)
            await self.websocket.send(json.dumps(row))
            msg = await self.websocket.recv()
            if msg == "stop":
                self.stopFlag = Truereturnawait asyncio.sleep(1)

main

start_server = websockets.serve(t.start_server, "127.0.0.1", 5678)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

code on the javascript client

var ws = newWebSocket("ws://127.0.0.1:5678/");
        ws.onmessage = function (event) {
            var datapoint = JSON.parse(event.data);
            console.log(counter);
            counter++;
            data.push(datapoint);
            if (data.length > 40){
                var element = data.shift();
                render(data);
            }
            ws.send("pong");//sending dummy message to let server continue
        };

I know it is not THE solution and i hope somebody else provides a better one but since i have the same or very similar problem and there are no other answers i decided to post and i hope it helps.

Post a Comment for "Sending And Receiving Frames Over The Same Websocket Connection Without Blocking"