Python Forum
Sending data from a websocket client to another server
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending data from a websocket client to another server
#2
I worked out a solution to my problem concerning step 2 and found a new problem! Dance Wall
Now I can send messages from the websocket client to the tornado server. (I have included the updated code below.)
I just transfer a set of websocket connections to the client.
tornado_connections = set()
def __on_message(self, msg):
        [client.write_message(msg) for client in self.connections]
Unfortunately, as soon as I start the websocket client (WsHandler) I can only have 1 client connected to the tornado server. I assume this has something to do with threading/asynchrony. Huh
start_client(connections=tornado_connections)
Has anyone a proposal how to overcome that problem?

Thanks!



Python websocket client

import websocket
 
 
class WebsocketClient:
    def __init__(self, connections):
        self.connections = connections
     
    def __on_open(self):
        print("Websocket opened")
        self.ws.send("Websocket rocks!")
 
    def __on_message(self, msg):
        [client.write_message(msg) for client in self.connections]
 
    def __on_close(self):
        print("Websocket closed")
 
    def connect(self):
        self.ws = websocket.WebSocketApp(
            "ws://echo.websocket.org",
            on_message=self.__on_message,
            on_close=self.__on_close,
            on_open=self.__on_open,
        )
        self.ws.run_forever()

    def disconnect(self):
        self.ws.close()
        print("PYTHON: Timer end")

 
def start_client(connections):
    ws = WebsocketClient(connections=connections)    
    ws.connect()
    print("Finished")
Python tornado websocket server

import os.path
import tornado.web
import tornado.websocket
import tornado.httpserver
from ws_client_test import start_client

tornado_connections = set()

class Application(tornado.web.Application): 
    def __init__(self):
        handlers = [
            (r"/", IndexHandler),
            (r"/ws", WsHandler)
            ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "template"), 
            static_path=os.path.join(os.path.dirname(__file__), "static"), 
        )
        tornado.web.Application.__init__(self, handlers, **settings)
 
class IndexHandler(tornado.web.RequestHandler): 
    def get(self):
        self.render("index_test.html")
 
class WsHandler(tornado.websocket.WebSocketHandler): 
    def open(self):
        if self not in tornado_connections:
            tornado_connections.add(self)
            print(f'TORNADO: {str(len(tornado_connections))} clients connected')
            self.write_message(f'TORNADO: {str(len(tornado_connections))} clients connected')
        
        if len(tornado_connections) == 1:
            start_client(connections=tornado_connections)
            print('Echo client connected')
        
 
    def on_message(self, message): 
        print(message)
     
    def on_close(self):
        if self in tornado_connections:
            tornado_connections.remove(self)
            print('TORNADO: server disconnected.')


if __name__ == "__main__": 
    app = Application()
    server = tornado.httpserver.HTTPServer(app) 
    server.listen(8000) 
    tornado.ioloop.IOLoop.instance().start()
JavaScript websocket client
var ws = new WebSocket("ws://127.0.0.1:8000/ws");

ws.onopen = function () {
ws.send("Client CONNECTED");
};

ws.onmessage = function (evt) {
document.getElementById("p1").innerHTML = evt.data;
};

ws.onclose = function () {
console.log("Client DISCONNECTED");
};
Reply


Messages In This Thread
RE: Sending data from a websocket client to another server - by lemon - May-25-2020, 02:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending data to php page ebolisa 0 1,927 Mar-18-2020, 05:34 PM
Last Post: ebolisa
  Sending data post to wsgiref JohnnyCoffee 0 1,808 Oct-24-2019, 09:04 PM
Last Post: JohnnyCoffee
  Client closing websocket connection - proof of concept rmattos 0 1,955 Jan-16-2019, 03:21 PM
Last Post: rmattos
  Django - Passing data from view for use client side with JavaScript in a template bountyhuntr 0 3,660 Jun-11-2018, 06:04 AM
Last Post: bountyhuntr
  Can't connect to websocket laas 2 8,414 Jul-27-2017, 02:11 PM
Last Post: garuna
  data push from logger to server asdfasdf 2 3,715 May-02-2017, 11:32 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020