This commit is contained in:
yumoqing 2024-10-09 11:46:30 +08:00
parent 472cc66425
commit 4f97384455

View File

@ -96,29 +96,67 @@ async def ws_send(ws:web.WebSocketResponse, data):
print_exc() print_exc()
return False return False
class WsSession:
def __init__(self, session)
self.session = session
self.nodes = {}
def join(node):
self.nodes[node.id] = node
def leave(node):
self.nodes = {k:v for k,v in self.nodes.items() if k != node.id}
class WsData:
def __init__(self):
self.nodes = {}
self.sessions = {}
def add_node(self, node):
self.nodes[node.id] = node
def del_node(self, node):
self.nodes = {k,v for k,v in self.nodes.items() if k!=node.id}
def get_node(self, id):
return self.nodes.get(id)
def add_session(self, session):
self.sessions[session.sessionid] = session
def del_session(self, session):
self.sessions = {k:v for k,v in self.sessions.items() if k != session.sessionid}
def get_session(self, id):
return self.sessions.get(id)
class WsPool: class WsPool:
def __init__(self, ws, ws_path, app): def __init__(self, ws, ip, ws_path, app):
self.app = app self.app = app
self.ip = ip
self.id = None self.id = None
self.ws = ws self.ws = ws
self.ws_path = ws_path self.ws_path = ws_path
def get_data(self): def get_data(self):
return self.app.get_data(self.ws_path) r = self.app.get_data(self.ws_path)
if r is None:
r = WsData()
self.set_data(r)
return r
def set_data(self, data): def set_data(self, data):
self.app.set_data(self.ws_path, data) self.app.set_data(self.ws_path, data)
def is_online(self, userid): def is_online(self, userid):
data = self.get_data() data = self.get_data()
ws = data.get(userid) node = data.get_node(userid)
if ws is None: if node is None:
return False return False
return True return True
def register(self, id): def register(self, id):
iddata = DictObject() iddata = DictObject()
iddata.ws = self.ws
iddata.id = id iddata.id = id
self.add_me(iddata) self.add_me(iddata)
@ -127,32 +165,40 @@ class WsPool:
if data is None: if data is None:
data = {} data = {}
iddata.ws = self.ws iddata.ws = self.ws
iddata.ip = self.ip
self.id = iddata.id self.id = iddata.id
data.update({self.id:iddata}) data.add_node(ddata)
info(f'add_me() {data=}')
self.set_data(data) self.set_data(data)
def delete_id(self, id): def delete_id(self, id):
data = self.get_data() data = self.get_data()
if not data.get(id): node = data.get_node(id)
return if node:
data = {k:v for k,v in data.items() if k != id } data.del_node(node)
self.set_data(data) self.set_data(data)
def delete_me(self): def delete_me(self):
self.delete_id(self.id) self.delete_id(self.id)
def add_session(self, session):
data = self.get_data() data = self.get_data()
if not data.get(self.id): data.add_session(session)
return
data = {k:v for k,v in data.items() if k != self.id }
self.set_data(data) self.set_data(data)
def del_session(self, session):
data = self.get_data()
data.del_session(session)
self.set_data(data)
def get_session(self, sessionid):
data = self.get_data()
return data.get_session(sessionid)
async def sendto(self, data, id=None): async def sendto(self, data, id=None):
if id is None: if id is None:
return await ws_send(self.ws, data) return await ws_send(self.ws, data)
d = self.get_data() d = self.get_data()
iddata = d.get(id) iddata = d.get_node(id)
info(f'{id=} {d=}, {iddata=}')
ws = iddata.ws ws = iddata.ws
try: try:
return await ws_send(ws, data) return await ws_send(ws, data)
@ -179,7 +225,7 @@ class WebsocketProcessor(PythonScriptProcessor):
print('--------except:', e, type(e)) print('--------except:', e, type(e))
print_exc() print_exc()
raise e raise e
ws_pool = WsPool(ws, request.path, request.app) ws_pool = WsPool(ws, request['client_ip'], request.path, request.app)
async for msg in ws: async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'exit': if msg.data == 'exit':