I am building a simple application for both a server and a client to communicate data in advance that I build more complex application. Purpose of this problem is very simple. In here, the client creates data over each seconds to the server and server also send back some data if the data received from client is text message like "send". I almost to create this similar application. But It didn't work so that I spent almost a week for this wasting my weekend.
The problem is that after the server received the message which means request for message to client, the server seemed to have sent data as per log but the client has no response. As my understanding, I think the callback function called cb_receive() should have responsed for this.
I created this problem with simple application below. Please let me know if you are good at asyncio and tornado libraries. Thanks again!
Server side
import tornado.ioloop
import tornado.web
import tornado.websocket
import os
from tornado import gen
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
self.write_message('hello')
@gen.coroutine
def on_message(self, message):
print(message)
yield self.write_message('notification : ', message)
def on_close(self):
print("A client disconnected!!")
if __name__ == "__main__":
app = tornado.web.Application([(r"/", EchoWebSocket)])
app.listen(os.getenv('PORT', 8344))
tornado.ioloop.IOLoop.instance().start()
Client side
from tornado.ioloop import IOLoop, PeriodicCallback
from tornado import gen
from tornado.websocket import websocket_connect
@gen.coroutine
def cb_receive(msg):
print('msg----------> {}'.format(msg))
class Client(object):
def __init__(self, url, timeout):
self.url = url
self.timeout = timeout
self.ioloop = IOLoop.instance()
self.ws = None
self.connect()
self.ioloop.start()
@gen.coroutine
def connect(self):
print("trying to connect")
try:
self.ws = yield websocket_connect(self.url,on_message_callback=cb_receive)
except Exception as e:
print("connection error")
else:
print("connected")
self.run()
@gen.coroutine
def run(self):
while True:
print('please input')
msg = input()
yield self.ws.write_message(msg)
print('trying to send msg {}'.format(msg))
if __name__ == "__main__":
client = Client("ws://localhost:8344", 5)
Please help me! I tried not only this tornado library above but also websockets and others. But it didn't work.