Refactoring and adding dockerfile.

master
Tomasz Półgrabia 2022-01-16 12:13:55 +01:00
parent b76257688b
commit a2ae9772cd
5 changed files with 83 additions and 75 deletions

View File

@ -1,3 +1,3 @@
.idea .idea
*.iml *.iml
env env

View File

@ -0,0 +1,6 @@
FROM python:3.8-slim-buster
COPY ./src /app
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN python3 -m pip install -r /app/requirements.txt
CMD [ "python3", "/app/server.py" ]

View File

@ -1 +1 @@
tornado tornado

View File

@ -1,45 +1,45 @@
from tornado.ioloop import IOLoop, PeriodicCallback from tornado.ioloop import IOLoop, PeriodicCallback
from tornado import gen from tornado import gen
from tornado.websocket import websocket_connect from tornado.websocket import websocket_connect
class Client(object): class Client(object):
def __init__(self, url, timeout): def __init__(self, url, timeout):
self.url = url self.url = url
self.timeout = timeout self.timeout = timeout
self.ioloop = IOLoop.instance() self.ioloop = IOLoop.instance()
self.ws = None self.ws = None
self.connect() self.connect()
PeriodicCallback(self.keep_alive, 20000).start() PeriodicCallback(self.keep_alive, 20000).start()
self.ioloop.start() self.ioloop.start()
@gen.coroutine @gen.coroutine
def connect(self): def connect(self):
print("trying to connect") print("trying to connect")
try: try:
self.ws = yield websocket_connect(self.url) self.ws = yield websocket_connect(self.url)
print("connected") print("connected")
self.run() self.run()
except Exception: except Exception:
print("connection error") print("connection error")
@gen.coroutine @gen.coroutine
def run(self): def run(self):
while True: while True:
msg = yield self.ws.read_message() msg = yield self.ws.read_message()
if msg is None: if msg is None:
print("connection closed") print("connection closed")
self.ws = None self.ws = None
break break
else: else:
print("Got a message: " + msg) print("Got a message: " + msg)
def keep_alive(self): def keep_alive(self):
if self.ws is None: if self.ws is None:
self.connect() self.connect()
else: else:
self.ws.write_message("keep alive") self.ws.write_message("keep alive")
if __name__ == "__main__": if __name__ == "__main__":
client = Client("ws://localhost:8888/socket", 5) client = Client("ws://localhost:8888/socket", 5)

View File

@ -1,26 +1,28 @@
import tornado.ioloop import tornado.ioloop
import tornado.web import tornado.web
import tornado.websocket import tornado.websocket
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
def get(self): def get(self):
self.write("Hello, world") self.write("Hello, world")
class WebsocketHandler(tornado.websocket.WebSocketHandler): class WebsocketHandler(tornado.websocket.WebSocketHandler):
def open(self): def open(self):
print("Opened...") print("Opened...")
def on_message(self, message): def on_message(self, message):
print("Got a message: " + message) print("Got a message: " + message)
self.write_message(u"You wrote: {0}".format(message)) self.write_message(u"You wrote: {0}".format(message))
def on_close(self): def on_close(self):
print("Closed...") print("Closed...")
if __name__ == "__main__": if __name__ == "__main__":
application = tornado.web.Application([ print("Start of the server...")
(r"/", MainHandler), application = tornado.web.Application([
(r"/socket", WebsocketHandler), (r"/", MainHandler),
]) (r"/socket", WebsocketHandler),
application.listen(8888) ])
tornado.ioloop.IOLoop.current().start() print("Starting to listen on port 8888")
application.listen(8888)
tornado.ioloop.IOLoop.current().start()