Frontend Core added

This commit is contained in:
Abdussamed ULUTAŞ 2022-11-27 23:32:28 +03:00
parent 0e1bcc0f6c
commit 5f43315ca8
9 changed files with 370 additions and 23 deletions

View File

@ -1,4 +1,4 @@
interface IConnection{
export interface IConnection{
endpoint: string;
autoPair?: boolean;
}
@ -11,13 +11,16 @@ export class Connection
constructor(options: IConnection){
this.endpoint = new URL(options.endpoint);
this.autoPair = options.autoPair || false;
this.connect();
}
public connect()
{
this.ws = new WebSocket(this.endpoint.href);
this.addWSEvents();
}
public disconnect()
{
this.ws.close();
}
public addWSEvents()
{
this.ws.addEventListener("open", () => this.eventOpen());
@ -28,6 +31,9 @@ export class Connection
private eventOpen()
{
this.connected = true;
for (const callback of this.activeConnectionEvent) {
callback(void 0);
}
}
private eventClose()
{
@ -42,6 +48,16 @@ export class Connection
{
this.recaivePackEvent.push(func);
}
private activeConnectionEvent : Function[] = [];
public onActive(func:Function)
{
if(this.connected)
{
func()
}else{
this.activeConnectionEvent.push(func);
}
}
private eventMessage(data: string | ArrayBuffer)
{
if(typeof data == "string")

49
frontend/EventPool.ts Normal file
View File

@ -0,0 +1,49 @@
import WSTS from "./index";
import { Message } from "./WSTSProtocol";
export default class EventPool
{
public wsts : WSTS;
public events : Map<number, [Function,Function]> = new Map();
public signals : Map<string, Function[]> = new Map();
public count = 0;
constructor(wsts:WSTS){
this.wsts = wsts;
}
public request(msg: Message) : Promise<any>
{
return new Promise((ok,rej) => {
let id = ++this.count;
this.wsts.WSTSProtocol.SendRequest(msg, id);
this.events.set(id,[
(data:any) => {
ok(data);
},
(data:any) => {
rej(data);
}
]);
})
}
public stream(msg: Message, callback: Function)
{
let id = ++this.count;
this.wsts.WSTSProtocol.StartStream(msg, id);
this.events.set(id,[
(data:any) => {
callback(data);
},
() => { }
]);
}
public signal(event: string, callback: Function)
{
let T = this.signals.get(event);
if(!T)
{
this.signals.set(event, [callback]);
}else{
T.push(callback);
}
}
}

47
frontend/Room.ts Normal file
View File

@ -0,0 +1,47 @@
import WSTS from "./index";
export interface IRoomOptions
{
name: string;
description?:string;
joinType: "free"|"invite"|"password"|"lock";
credential?: string;
ifexistsJoin?: boolean;
accessType?: "public"|"private";
notifyActionInvite?: boolean;
notifyActionJoined?: boolean;
notifyActionEjected?: boolean;
}
export default class Room
{
public wsts : WSTS;
public options! : IRoomOptions;
public events : {[key:string]:Function[]} = {};
constructor(wsts:WSTS){
this.wsts = wsts;
}
public setRoomOptions(options : IRoomOptions)
{
this.options = options;
}
private emit(eventName :string, ...args:any[])
{
if(this.events[eventName])
{
for (const callback of this.events[eventName]) {
callback(...args);
}
}
}
public on(eventName :string, callback:Function)
{
if(this.events[eventName])
{
this.events[eventName].push(callback)
}else{
this.events[eventName] = [callback];
}
}
}

66
frontend/WSTSProtocol.ts Normal file
View File

@ -0,0 +1,66 @@
import WSTS from "./index";
export interface Message {
[key:string|number]:any;
}
export default class WSTSProtocol
{
public wsts : WSTS;
constructor(wsts:WSTS){
this.wsts = wsts;
this.addListener();
}
public addListener()
{
this.wsts.server?.onRecaivePack((pack)=>{
this.PackAnalyze(pack)
})
}
public SendRaw(pack: Message)
{
this.wsts.server.tranferToServer(pack);
}
public SendOnly(pack: Message)
{
this.wsts.server.tranferToServer([pack,'R']);
}
public SendRequest(pack: Message, id: number)
{
this.wsts.server.tranferToServer([pack, id, 'R']);
}
public StartStream(pack: Message, id: number)
{
this.wsts.server.tranferToServer([pack, id, 'S']);
}
public PackAnalyze(data:any)
{
let [payload, id, action] = JSON.parse(data);
if(typeof id === 'number')
{
let callback = this.wsts.EventPooling.events.get(id);
if(callback)
{
callback[0](payload, action);
switch(action)
{
case 'E':{ // [E]ND flag
this.wsts.EventPooling.events.delete(id);
break;
}
case 'S': // [S]TREAM flag
default:{
break;
}
}
}else console.warn("Missing event sended from server");
}else{
let signals = this.wsts.EventPooling.signals.get(id);
if(signals)
{
for (const callback of signals) {
callback(payload);
}
}else console.warn("Missing event sended from server");
}
}
}

View File

@ -1,10 +1,22 @@
import {Connection} from "./Connection";
import {Connection,IConnection} from "./Connection";
import EventPool from "./EventPool";
import Room, { IRoomOptions } from "./Room";
import WSTSProtocol from "./WSTSProtocol";
export default class WSTS {
public connection? : Connection;
public a = 25;
constructor(){
this.connection = new Connection({
endpoint: "25"
});
public server! : Connection;
public WSTSProtocol! : WSTSProtocol;
public EventPooling! : EventPool;
public rooms : Map<string, Room> = new Map();
constructor(options: IConnection){
this.server = new Connection(options);
this.server.connect();
this.WSTSProtocol = new WSTSProtocol(this);
this.EventPooling = new EventPool(this);
}
public room(options: IRoomOptions)
{
let room = new Room(this);
room.setRoomOptions(options);
return room;
}
}

View File

@ -12,7 +12,17 @@
"publicUrl": "./",
"sourceMap": true,
"outputFormat": "global",
"optimize": true
"optimize": true,
"context": "browser",
"engines": {
"chrome": "65",
"android": "4.4.3",
"edge": "16",
"firefox": "59",
"ie": "10",
"ios": "10",
"safari": "10"
}
}
},
"repository": {

View File

@ -142,21 +142,33 @@
this[globalName] = mainExports;
}
}
})({"jQXDy":[function(require,module,exports) {
})({"6snCa":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
var _connection = require("./Connection");
var _eventPool = require("./EventPool");
var _eventPoolDefault = parcelHelpers.interopDefault(_eventPool);
var _room = require("./Room");
var _roomDefault = parcelHelpers.interopDefault(_room);
var _wstsprotocol = require("./WSTSProtocol");
var _wstsprotocolDefault = parcelHelpers.interopDefault(_wstsprotocol);
class WSTS {
a = 25;
constructor(){
this.connection = new (0, _connection.Connection)({
endpoint: "25"
});
rooms = new Map();
constructor(options){
this.server = new (0, _connection.Connection)(options);
this.server.connect();
this.WSTSProtocol = new (0, _wstsprotocolDefault.default)(this);
this.EventPooling = new (0, _eventPoolDefault.default)(this);
}
room(options) {
let room = new (0, _roomDefault.default)(this);
room.setRoomOptions(options);
return room;
}
}
exports.default = WSTS;
},{"./Connection":"aAICD","@parcel/transformer-js/src/esmodule-helpers.js":"e7rGL"}],"aAICD":[function(require,module,exports) {
},{"./Connection":"dzYK1","@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE","./WSTSProtocol":"3kvWC","./EventPool":"37Faq","./Room":"7qlv2"}],"dzYK1":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
parcelHelpers.export(exports, "Connection", ()=>Connection);
@ -166,12 +178,14 @@ class Connection {
constructor(options){
this.endpoint = new URL(options.endpoint);
this.autoPair = options.autoPair || false;
this.connect();
}
connect() {
this.ws = new WebSocket(this.endpoint.href);
this.addWSEvents();
}
disconnect() {
this.ws.close();
}
addWSEvents() {
this.ws.addEventListener("open", ()=>this.eventOpen());
this.ws.addEventListener("close", ()=>this.eventClose());
@ -180,6 +194,7 @@ class Connection {
}
eventOpen() {
this.connected = true;
for (const callback of this.activeConnectionEvent)callback(void 0);
}
eventClose() {
this.connected = false;
@ -191,6 +206,11 @@ class Connection {
onRecaivePack(func) {
this.recaivePackEvent.push(func);
}
activeConnectionEvent = [];
onActive(func) {
if (this.connected) func();
else this.activeConnectionEvent.push(func);
}
eventMessage(data) {
if (typeof data == "string") for (const callback of this.recaivePackEvent)callback(JSON.parse(data));
}
@ -199,7 +219,7 @@ class Connection {
}
}
},{"@parcel/transformer-js/src/esmodule-helpers.js":"e7rGL"}],"e7rGL":[function(require,module,exports) {
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"i1YYE":[function(require,module,exports) {
exports.interopDefault = function(a) {
return a && a.__esModule ? a : {
default: a
@ -229,6 +249,133 @@ exports.export = function(dest, destName, get) {
});
};
},{}]},["jQXDy"], "jQXDy", "parcelRequiref9d4")
},{}],"3kvWC":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
class WSTSProtocol {
constructor(wsts){
this.wsts = wsts;
this.addListener();
}
addListener() {
this.wsts.server?.onRecaivePack((pack)=>{
this.PackAnalyze(pack);
});
}
SendRaw(pack) {
this.wsts.server.tranferToServer(pack);
}
SendOnly(pack) {
this.wsts.server.tranferToServer([
pack,
"R"
]);
}
SendRequest(pack, id) {
this.wsts.server.tranferToServer([
pack,
id,
"R"
]);
}
StartStream(pack, id) {
this.wsts.server.tranferToServer([
pack,
id,
"S"
]);
}
PackAnalyze(data) {
let [payload, id, action] = JSON.parse(data);
if (typeof id === "number") {
let callback = this.wsts.EventPooling.events.get(id);
if (callback) {
callback[0](payload, action);
switch(action){
case "E":
this.wsts.EventPooling.events.delete(id);
break;
case "S":
default:
break;
}
} else console.warn("Missing event sended from server");
} else {
let signals = this.wsts.EventPooling.signals.get(id);
if (signals) for (const callback1 of signals)callback1(payload);
else console.warn("Missing event sended from server");
}
}
}
exports.default = WSTSProtocol;
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"37Faq":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
class EventPool {
events = new Map();
signals = new Map();
count = 0;
constructor(wsts){
this.wsts = wsts;
}
request(msg) {
return new Promise((ok, rej)=>{
let id = ++this.count;
this.wsts.WSTSProtocol.SendRequest(msg, id);
this.events.set(id, [
(data)=>{
ok(data);
},
(data)=>{
rej(data);
}
]);
});
}
stream(msg, callback) {
let id = ++this.count;
this.wsts.WSTSProtocol.StartStream(msg, id);
this.events.set(id, [
(data)=>{
callback(data);
},
()=>{}
]);
}
signal(event, callback) {
let T = this.signals.get(event);
if (!T) this.signals.set(event, [
callback
]);
else T.push(callback);
}
}
exports.default = EventPool;
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"7qlv2":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports);
class Room {
events = {};
constructor(wsts){
this.wsts = wsts;
}
setRoomOptions(options) {
this.options = options;
}
emit(eventName, ...args) {
if (this.events[eventName]) for (const callback of this.events[eventName])callback(...args);
}
on(eventName, callback) {
if (this.events[eventName]) this.events[eventName].push(callback);
else this.events[eventName] = [
callback
];
}
}
exports.default = Room;
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}]},["6snCa"], "6snCa", "parcelRequiref9d4")
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@ -15,8 +15,8 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "ES3", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["DOM","ES2015","ES2015.Promise"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"target": "ES6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["DOM","ES6","ES2015.Promise"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */