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
signaling data format
create a new session
{
type:callRequest,
sessionid:kkkk,
sessiontype:...
type:new_session,
session:{
sessiontype:
sessionid
}
from:
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
if info is None:
info = {
'id':getID()
}
self.ifno = info
self.opts = opts
self.info = info
self.ws = None
self.handler_name = handler_name
self.handler_name = 'sessionid'
self.app = app
self.running = False
self.app.wss = self
@ -58,14 +72,24 @@ class Wss:
handler = self.recvdata_handler
await handler(data)
async def recvdata_handler(self, data):
if data.type == 'info':
def add_peer(self, 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
if data.type == 'session'
h = self.sessionhandlers.get(data.sessiontype)
if data.type == 'logout':
self.delete_peer(data.msgfrom)
return
if data.type == 'new_session'
h = self.sessionhandlers.get(data.session.sessiontype)
i = h(data.from)
k = data.get(self.handler_name)
k = data.session.sessionid
self.add_handler(k, i.recvdata_handler)
return
@ -81,7 +105,7 @@ class Wss:
await self.start()
async def send(self, dic):
dic['from'] = self.info
dic.msgfrom = self.info
txt = json.dumps(dic)
try:
return await self.ws.send(txt)
@ -89,14 +113,30 @@ class Wss:
await self.restart()
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):
d = DictObject()
d['from'] = self.info
d.msgfrom = self.info
d.type = 'login'
await self.send(d)
async def logout(self):
d = DictObject()
d['from'] = self.info
d.msgfrom = self.info
d.type = 'logout'
await self.send(d)