forked from silvercondor/basic_socket
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket_host.py
More file actions
29 lines (20 loc) · 721 Bytes
/
socket_host.py
File metadata and controls
29 lines (20 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from tornado import websocket
import tornado.ioloop
PORT = 66666
class EchoWebSocket(websocket.WebSocketHandler):
clients = set()
def open(self):
print('Websocket opened')
self.clients.add(self)
def on_message(self, message):
# Echo message to client
# self.write_message(f'Incoming message {message}')
# Broadcast message to all clients
[c.write_message(f'{message}') for c in self.clients]
def on_close(self):
print("websocket closed")
self.clients.remove(self)
application = tornado.web.Application([(r"/", EchoWebSocket), ])
if __name__ == "__main__":
application.listen(66666)
tornado.ioloop.IOLoop.instance().start()