Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tornado import websocket, web, ioloop
- from modules import account
- import protocols
- import json
- import time
- import re
- import sys
- bannedip = {}
- clients = {}
- rooms = {}
- middlewares = ['module.log'] # not implemented yet
- class Server():
- @staticmethod
- def removeFromClients(socket):
- if socket in clients:
- print("We are removing %s from client list"%clients[socket].nickname);
- del clients[socket]
- print(clients)
- class WebsocketClient():
- logged_in = False
- nickname = None
- socket = None
- def __init__(self, socket):
- self.socket = socket;
- class SocketHandler(websocket.WebSocketHandler):
- def check_origin(self, origin):
- return True
- def open(self):
- if self not in clients:
- cl = WebsocketClient(self)
- clients[self]=cl
- def on_close(self):
- if self in clients:
- print("Client %s left chatroom"%(clients[self].nickname))
- Server.removeFromClients(self)
- def on_message(self,message):
- handled = False
- data = None
- try:
- data = json.loads(message)
- print(data)
- except json.JSONDecodeError as e:
- print(e.msg)
- self.close()
- return Server.removeFromClients(self)
- # .Data Classifer(int) .Function name(string) .All other required params
- # .required by protocol
- # data [0 , function , more params]
- print(data[0])
- dataClassification = protocols.DataClassifier[data[0]] \
- if data[0] in protocols.DataClassifier else None
- print(dataClassification)
- if self in clients:
- for name, module in sys.modules.items():
- if not name.startswith("modules."):
- continue
- fn = getattr(module, dataClassification)
- if(callable(fn)):
- tempHandle = fn(self, data[1::], globals())
- if(tempHandle == True):
- return
- # module.globals()['']
- # self.write_message(mssage)
- if handled == False:
- self.close()
- Server.removeFromClients(self)
- app = web.Application([
- (r'/', SocketHandler),
- ])
- if __name__ == '__main__':
- print(protocols.DataClassifier)
- app.listen(5000)
- ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement