This commit is contained in:
yumoqing 2024-10-07 13:57:42 +08:00
parent 5f0e01941c
commit 7ff131e383

View File

@ -11,23 +11,37 @@ class Wss:
session_type: p2p, aba, meeting session_type: p2p, aba, meeting
signaling data format signaling data format
create a new session
{ {
type:callRequest, type:new_session,
sessionid:kkkk, session:{
sessiontype:... sessiontype:
sessionid
}
from: from:
to: to:
} }
(will boastcast to all peer online)login:
{
type:login,
from:
}
(will boastcast to all peer online)logout:
{
type:logout
from:
}
""" """
def __init__(self, app, wss_url, info=None, handler_name='sessionid'): def __init__(self, app, wss_url, info=None, opts={}):
self.wss_url = wss_url self.wss_url = wss_url
if info is None: if info is None:
info = { info = {
'id':getID() 'id':getID()
} }
self.ifno = info self.opts = opts
self.info = info
self.ws = None self.ws = None
self.handler_name = handler_name self.handler_name = 'sessionid'
self.app = app self.app = app
self.running = False self.running = False
self.app.wss = self self.app.wss = self
@ -58,14 +72,24 @@ class Wss:
handler = self.recvdata_handler handler = self.recvdata_handler
await handler(data) await handler(data)
async def recvdata_handler(self, data): def add_peer(self, info):
if data.type == 'info': self.peers = [ i for i in self.peers if i.id != info.id ]
self.peers.append(info)
def delete_peer(self, info):
self.peers = [ i for i in self.peers if i.id != info.id ]
async def recvdata_handler(self, data):
if data.type == 'login':
self.add_peer(data.msgfrom)
return return
if data.type == 'session' if data.type == 'logout':
h = self.sessionhandlers.get(data.sessiontype) self.delete_peer(data.msgfrom)
return
if data.type == 'new_session'
h = self.sessionhandlers.get(data.session.sessiontype)
i = h(data.from) i = h(data.from)
k = data.get(self.handler_name) k = data.session.sessionid
self.add_handler(k, i.recvdata_handler) self.add_handler(k, i.recvdata_handler)
return return
@ -81,7 +105,7 @@ class Wss:
await self.start() await self.start()
async def send(self, dic): async def send(self, dic):
dic['from'] = self.info dic.msgfrom = self.info
txt = json.dumps(dic) txt = json.dumps(dic)
try: try:
return await self.ws.send(txt) return await self.ws.send(txt)
@ -89,14 +113,30 @@ class Wss:
await self.restart() await self.restart()
return self.send(dic) return self.send(dic)
async def new_session(self, sessiontype, opts):
k = self.sessionhandlers.get(sessiontype)
if k is None:
e = Exception(f'Sessiontype({sessiontype}) not registed')
exception(f'new_session() exception {e}')
raise e
d = DictObject()
d.type = 'new_session'
d.msgfrom = self.info
sessionid = getID()
d.session = DictObject(sessiontype=sessiontype, sessionid=sessionid)
await self.send(d)
h = k(self, sessionid, opts)
self.add_sessionhandler(sessionid, h.recvdata_handler)
return h
async def login(self): async def login(self):
d = DictObject() d = DictObject()
d['from'] = self.info d.msgfrom = self.info
d.type = 'login' d.type = 'login'
await self.send(d) await self.send(d)
async def logout(self): async def logout(self):
d = DictObject() d = DictObject()
d['from'] = self.info d.msgfrom = self.info
d.type = 'logout' d.type = 'logout'
await self.send(d) await self.send(d)