Add NodeWS class.

This commit is contained in:
Tipx-L 2023-10-10 03:08:04 -07:00
parent 8cd11aff03
commit bc14608256
1 changed files with 26 additions and 21 deletions

View File

@ -7484,6 +7484,9 @@
'<li>激昂:一名角色发动“昂扬技”标签技能后,此技能失效,直至从此刻至满足此技能“激昂”条件后。'+ '<li>激昂:一名角色发动“昂扬技”标签技能后,此技能失效,直至从此刻至满足此技能“激昂”条件后。'+
'' ''
}, },
/**
* @type {import('path')}
*/
path:{}, path:{},
getErrorTip:msg=>{ getErrorTip:msg=>{
if(typeof msg!='string'){ if(typeof msg!='string'){
@ -31320,6 +31323,9 @@
} }
}, },
Client:class{ Client:class{
/**
* @param {InstanceType<typeof lib.element.NodeWS | typeof import('ws').WebSocket>} ws
*/
constructor(ws){ constructor(ws){
this.ws=ws; this.ws=ws;
this.id=ws.wsid||get.id(); this.id=ws.wsid||get.id();
@ -31377,14 +31383,20 @@
return this; return this;
} }
}, },
nodews:{ NodeWS:class{
send:function(message){ /**
* @param {string} id
*/
constructor(id){
this.wsid=id;
}
send(message){
game.send('server','send',this.wsid,message); game.send('server','send',this.wsid,message);
}, }
on:function(type,func){ on(type,func){
this['on'+type]=func; this['on'+type]=func;
}, }
close:function(){ close(){
game.send('server','close',this.wsid); game.send('server','close',this.wsid);
} }
}, },
@ -31491,6 +31503,12 @@
*/ */
get client(){ get client(){
return this.Client.prototype; return this.Client.prototype;
},
/**
* @legacy Use `lib.element.NodeWS.prototype` instead.
*/
get nodews(){
return this.NodeWS.prototype;
} }
}, },
card:{ card:{
@ -33682,14 +33700,7 @@
ui.connecting.firstChild.innerHTML='重连成功'; ui.connecting.firstChild.innerHTML='重连成功';
} }
}, },
onconnection:function(id){ onconnection:id=>lib.init.connection(lib.wsOL[id]=new lib.element.NodeWS(id)),
var ws={wsid:id};
for(var i in lib.element.nodews){
ws[i]=lib.element.nodews[i];
}
lib.wsOL[id]=ws;
lib.init.connection(ws);
},
onmessage:function(id,message){ onmessage:function(id,message){
if(lib.wsOL[id]){ if(lib.wsOL[id]){
lib.wsOL[id].onmessage(message); lib.wsOL[id].onmessage(message);
@ -35790,13 +35801,7 @@
game.ws.send(JSON.stringify(get.stringifiedResult(args))); game.ws.send(JSON.stringify(get.stringifiedResult(args)));
} }
}, },
sendTo:function(id,message){ sendTo:(id,message)=>new lib.element.Client(new lib.element.NodeWS(id)).send(message),
var ws={wsid:id};
for(var i in lib.element.nodews){
ws[i]=lib.element.nodews[i];
}
new lib.element.Client(ws).send(message);
},
createServer:function(){ createServer:function(){
lib.node.clients=[]; lib.node.clients=[];
lib.node.banned=[]; lib.node.banned=[];