MWSE/script/wsjs.js

194 lines
5.3 KiB
JavaScript

function WSJS()
{
this.isActive = false;
this.ws = WSJS.activeWS || null;
this.endpoint = null;
};
WSJS.activeWS = null;
WSJS.prototype.connect = function(url){
this.ws = new WebSocket(url);
this.addListeners();
}
WSJS.prototype.addListeners = function(){
this.ws.addEventListener("close", this.closedEvent.bind(this));
this.ws.addEventListener("message", this.messageEvent.bind(this));
this.ws.addEventListener("open", this.openMessage.bind(this));
}
WSJS.prototype.closedEvent = function(){
WSJS.activeWS = null;
this.isActive = false;
this.ws = null;
this.events.dispatchEvent(new Event("close"));
}
WSJS.prototype.openMessage = function(){
WSJS.activeWS = this.ws;
this.isActive = true;
this.events.dispatchEvent(new Event("open"));
}
WSJS.prototype.messageEvent = function({data}){
let [payload, id, action] = JSON.parse(data);
if(typeof id === 'number')
{
if(this.requests.has(id))
{
this.requests.get(id)(payload, action);
switch(action)
{
case 'E':{ // [E]ND flag
this.requests.delete(id);
break;
}
case 'S': // [S]TREAM flag
default:{
break;
}
}
}else console.warn("Missing event sended from server");
}else{
if(this.signals.has(id))
{
for (const callback of this.signals.get(id)) {
callback(payload);
}
}else console.warn("Missing event sended from server");
}
}
WSJS.prototype.events = new EventTarget();
WSJS.prototype.scope = function(func){
if(this.isActive)
{
func();
}else this.events.addEventListener("open", func);
}
WSJS.prototype.sendOnly = function(obj){
if(this.isActive)
{
this.sendRaw([obj,'R']);
}else throw new Error(`socket could be a active`);
}
WSJS.prototype.requests = new Map();
WSJS.prototype.requestCount = 0;
WSJS.prototype.request = async function(obj){
if(this.isActive)
{
return await new Promise(ok => {
let id = ++this.requestCount;
this.sendRaw([obj,id,'R']);
this.requests.set(id,data => {
ok(data);
});
})
}else throw new Error(`socket could be a active`);
}
WSJS.prototype.stream = async function(obj,callback){
if(this.isActive)
{
let id = ++this.requestCount;
this.sendRaw([obj, id, 'S']);
this.requests.set(id,data => {
callback(data);
});
}else throw new Error(`socket could be a active`);
}
WSJS.prototype.signals = new Map();
WSJS.prototype.signal = async function(name, callback){
if(!this.signals.has(name))
{
this.signals.set(name, [callback]);
}else{
this.signals.get(name).push(callback);
}
}
WSJS.prototype.slot = async function(name, obj){
if(this.isActive)
{
if(typeof name == "string")
{
this.sendOnly([obj,name]);
}else{
throw new Error(`name could be a string, gived ${typeof name}`);
}
}else throw new Error(`socket could be a active`);
}
WSJS.prototype.sendRaw = function(obj){
if(this.isActive)
{
this.ws.send(JSON.stringify(obj))
};
};
WSJS.prototype.authWith = async function(username, password){
await this.request({
type: 'auth/login',
username,
password
});
};
WSJS.prototype.fetchMyRoomInfo = async function(){
return await this.request({
type: 'myroom-info'
});
};
WSJS.prototype.createRoom = async function(options){
let result = await this.request({
type: 'create-room',
accessType: options.accessType || "private",
notifyActionInvite: options.notifyActionInvite === undefined ? true : options.notifyActionInvite,
notifyActionJoined: options.notifyActionJoined === undefined ? true : options.notifyActionJoined,
notifyActionEjected: options.notifyActionEjected === undefined ? true : options.notifyActionEjected,
joinType: options.joinType || "free",
description: options.description || "No Description",
name: options.name || "No",
credential: options.credential || undefined
});
return result;
};
WSJS.prototype.roomInfo = async function(name){
let result = await this.request({
type: 'room-info',
name
});
return result;
};
WSJS.prototype.joinRoom = async function(options){
let result = await this.request({
...options,
type: 'joinroom'
});
return result;
};
WSJS.prototype.joinedRooms = new Map();
WSJS.prototype.getJoinedRooms = async function(){
return await this.request({
type: 'joinedrooms'
});
};
WSJS.prototype.getRoomPeers = async function(id){
return await this.request({
type: 'room-peers',
roomId: id
});
};
WSJS.prototype.sendPackToPeer = async function(roomId, pack){
return await this.sendOnly({
type: 'pack/to',
to: roomId,
pack,
handshake: false
});
};
WSJS.prototype.sendPackToRoom = async function(roomId, pack){
return await this.sendOnly({
type: 'pack/room',
to: roomId,
pack,
handshake: false
});
};