P2P Messaging complete
This commit is contained in:
parent
4abb19d4c7
commit
ba05a83286
|
@ -13,9 +13,26 @@ addService(({
|
||||||
end,
|
end,
|
||||||
next
|
next
|
||||||
})=>{
|
})=>{
|
||||||
let {type,username,password,to} = message;
|
let {type,username,password,to,value} = message;
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
|
case "auth/pair-system":{
|
||||||
|
if(value == 'everybody')
|
||||||
|
{
|
||||||
|
client.requiredPair = true;
|
||||||
|
end({status:"success"});
|
||||||
|
}
|
||||||
|
if(value == 'disable')
|
||||||
|
{
|
||||||
|
client.requiredPair = false;
|
||||||
|
end({status:"success"});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "my/socketid":{
|
||||||
|
end(client.id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'auth/public':{
|
case 'auth/public':{
|
||||||
client.requiredPair = false;
|
client.requiredPair = false;
|
||||||
return end({
|
return end({
|
||||||
|
|
|
@ -30,6 +30,12 @@ addService(({
|
||||||
if(Client.clients.has(to))
|
if(Client.clients.has(to))
|
||||||
{
|
{
|
||||||
let otherPeer = Client.clients.get(to);
|
let otherPeer = Client.clients.get(to);
|
||||||
|
if(otherPeer.requiredPair && !otherPeer.pairs.has(to))
|
||||||
|
{
|
||||||
|
return handshake && end({
|
||||||
|
type: 'fail'
|
||||||
|
})
|
||||||
|
}
|
||||||
otherPeer.send([{
|
otherPeer.send([{
|
||||||
from: client.id,
|
from: client.id,
|
||||||
pack: pack
|
pack: pack
|
||||||
|
|
|
@ -62,8 +62,9 @@ export class Connection
|
||||||
{
|
{
|
||||||
if(typeof data == "string")
|
if(typeof data == "string")
|
||||||
{
|
{
|
||||||
|
let $data = JSON.parse(data);
|
||||||
for (const callback of this.recaivePackEvent) {
|
for (const callback of this.recaivePackEvent) {
|
||||||
callback(JSON.parse(data));
|
callback($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
export default class EventTarget
|
||||||
|
{
|
||||||
|
private events : {[key:string]:Function[]} = {};
|
||||||
|
public 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public activeScope : boolean = false;
|
||||||
|
scope(f:Function)
|
||||||
|
{
|
||||||
|
if(this.activeScope)
|
||||||
|
{
|
||||||
|
f()
|
||||||
|
}else{
|
||||||
|
this.on('scope', f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,67 @@
|
||||||
|
import EventTarget from "./EventTarget";
|
||||||
import MWSE from "./index";
|
import MWSE from "./index";
|
||||||
|
|
||||||
interface IPeerOptions{
|
interface IPeerOptions{
|
||||||
|
|
||||||
};
|
};
|
||||||
|
interface IPeerMetadata{
|
||||||
|
|
||||||
export default class Peer
|
};
|
||||||
|
|
||||||
|
export default class Peer extends EventTarget
|
||||||
{
|
{
|
||||||
public wsts : MWSE;
|
public mwse : MWSE;
|
||||||
public options! : IPeerOptions;
|
public options! : IPeerOptions;
|
||||||
|
public socketId? : string;
|
||||||
|
public selfSocket : boolean = false;
|
||||||
|
public active : boolean = false;
|
||||||
constructor(wsts:MWSE){
|
constructor(wsts:MWSE){
|
||||||
this.wsts = wsts;
|
super();
|
||||||
|
this.mwse = wsts;
|
||||||
}
|
}
|
||||||
setPeerOptions(options:IPeerOptions){
|
setPeerOptions(options: string | IPeerOptions){
|
||||||
this.options = options;
|
if(typeof options == "string")
|
||||||
|
{
|
||||||
|
this.setSocketId(options)
|
||||||
|
}else{
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setSocketId(uuid: string){
|
||||||
|
this.socketId = uuid;
|
||||||
|
}
|
||||||
|
async metadata() : Promise<any>
|
||||||
|
{
|
||||||
|
if(this.socketId == 'me')
|
||||||
|
{
|
||||||
|
let result = await this.mwse.EventPooling.request({
|
||||||
|
type:'my/socketid'
|
||||||
|
});
|
||||||
|
this.selfSocket = true;
|
||||||
|
this.active ||= true;
|
||||||
|
this.socketId = result;
|
||||||
|
this.emit('scope');
|
||||||
|
this.activeScope = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async enablePairAuth(){
|
||||||
|
await this.mwse.EventPooling.request({
|
||||||
|
type:'auth/pair-system',
|
||||||
|
value: 'everybody'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async disablePairAuth(){
|
||||||
|
await this.mwse.EventPooling.request({
|
||||||
|
type:'auth/pair-system',
|
||||||
|
value: 'disable'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async send(pack: any){
|
||||||
|
await this.mwse.EventPooling.request({
|
||||||
|
type:'pack/to',
|
||||||
|
pack,
|
||||||
|
to: this.socketId
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -34,7 +34,7 @@ export default class WSTSProtocol
|
||||||
}
|
}
|
||||||
public PackAnalyze(data:any)
|
public PackAnalyze(data:any)
|
||||||
{
|
{
|
||||||
let [payload, id, action] = JSON.parse(data);
|
let [payload, id, action] = data;
|
||||||
if(typeof id === 'number')
|
if(typeof id === 'number')
|
||||||
{
|
{
|
||||||
let callback = this.mwse.EventPooling.events.get(id);
|
let callback = this.mwse.EventPooling.events.get(id);
|
||||||
|
|
|
@ -1,19 +1,42 @@
|
||||||
import {Connection,IConnection} from "./Connection";
|
import {Connection,IConnection} from "./Connection";
|
||||||
import EventPool from "./EventPool";
|
import EventPool from "./EventPool";
|
||||||
|
import EventTarget from "./EventTarget";
|
||||||
import Peer from "./Peer";
|
import Peer from "./Peer";
|
||||||
import Room, { IRoomOptions } from "./Room";
|
import Room, { IRoomOptions } from "./Room";
|
||||||
import WSTSProtocol from "./WSTSProtocol";
|
import WSTSProtocol from "./WSTSProtocol";
|
||||||
export default class MWSE {
|
export default class MWSE extends EventTarget {
|
||||||
public server! : Connection;
|
public server! : Connection;
|
||||||
public WSTSProtocol! : WSTSProtocol;
|
public WSTSProtocol! : WSTSProtocol;
|
||||||
public EventPooling! : EventPool;
|
public EventPooling! : EventPool;
|
||||||
public rooms : Map<string, Room> = new Map();
|
public rooms : Map<string, Room> = new Map();
|
||||||
public pairs : Map<string, Peer> = new Map();
|
public pairs : Map<string, Peer> = new Map();
|
||||||
|
public peers : Map<string, Peer> = new Map();
|
||||||
|
public me! : Peer;
|
||||||
constructor(options: IConnection){
|
constructor(options: IConnection){
|
||||||
|
super();
|
||||||
this.server = new Connection(options);
|
this.server = new Connection(options);
|
||||||
this.server.connect();
|
this.server.connect();
|
||||||
this.WSTSProtocol = new WSTSProtocol(this);
|
this.WSTSProtocol = new WSTSProtocol(this);
|
||||||
this.EventPooling = new EventPool(this);
|
this.EventPooling = new EventPool(this);
|
||||||
|
this.me = new Peer(this);
|
||||||
|
this.me.scope(()=>{
|
||||||
|
this.peers.set('me', this.me);
|
||||||
|
this.peers.set(this.me.socketId as string, this.me);
|
||||||
|
})
|
||||||
|
this.server.onActive(async ()=>{
|
||||||
|
this.me.setSocketId('me');
|
||||||
|
await this.me.metadata();
|
||||||
|
this.emit('scope');
|
||||||
|
this.activeScope = true;
|
||||||
|
});
|
||||||
|
this.packMessagingSystem();
|
||||||
|
}
|
||||||
|
private packMessagingSystem()
|
||||||
|
{
|
||||||
|
this.EventPooling.signal('pack',(payload : {to:string,pack:any}) => {
|
||||||
|
let {to,pack} = payload;
|
||||||
|
this.peer(to).emit('message', pack);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
public room(options: IRoomOptions)
|
public room(options: IRoomOptions)
|
||||||
{
|
{
|
||||||
|
@ -21,10 +44,22 @@ export default class MWSE {
|
||||||
room.setRoomOptions(options);
|
room.setRoomOptions(options);
|
||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
public peer(options: IRoomOptions)
|
public peer(options: string | IRoomOptions) : Peer
|
||||||
{
|
{
|
||||||
|
if(typeof options == "string")
|
||||||
|
{
|
||||||
|
if(this.peers.has(options))
|
||||||
|
{
|
||||||
|
return this.peers.get(options) as Peer
|
||||||
|
}
|
||||||
|
if(this.pairs.has(options))
|
||||||
|
{
|
||||||
|
return this.pairs.get(options) as Peer
|
||||||
|
}
|
||||||
|
}
|
||||||
let peer = new Peer(this);
|
let peer = new Peer(this);
|
||||||
peer.setPeerOptions(options);
|
peer.setPeerOptions(options);
|
||||||
|
this.emit('peer', peer);
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,7 +21,8 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@parcel/packager-ts": "^2.7.0",
|
"@parcel/packager-ts": "^2.7.0",
|
||||||
"@parcel/transformer-typescript-types": "^2.7.0"
|
"@parcel/transformer-typescript-types": "^2.7.0",
|
||||||
|
"tslib": "^2.4.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@cronvel/get-pixels": {
|
"node_modules/@cronvel/get-pixels": {
|
||||||
|
@ -3484,6 +3485,12 @@
|
||||||
"resolved": "https://registry.npmjs.org/tree-kit/-/tree-kit-0.7.4.tgz",
|
"resolved": "https://registry.npmjs.org/tree-kit/-/tree-kit-0.7.4.tgz",
|
||||||
"integrity": "sha512-Of3tPmVs3b6BhzyUJ7t0olisf47kYr9qAm0XaUpURMjdBn6TwiVaaMuTFoKkkvPGojd9trKAHlrGGcGKcdR1DA=="
|
"integrity": "sha512-Of3tPmVs3b6BhzyUJ7t0olisf47kYr9qAm0XaUpURMjdBn6TwiVaaMuTFoKkkvPGojd9trKAHlrGGcGKcdR1DA=="
|
||||||
},
|
},
|
||||||
|
"node_modules/tslib": {
|
||||||
|
"version": "2.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz",
|
||||||
|
"integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"node_modules/type": {
|
"node_modules/type": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
|
||||||
|
@ -6233,6 +6240,12 @@
|
||||||
"resolved": "https://registry.npmjs.org/tree-kit/-/tree-kit-0.7.4.tgz",
|
"resolved": "https://registry.npmjs.org/tree-kit/-/tree-kit-0.7.4.tgz",
|
||||||
"integrity": "sha512-Of3tPmVs3b6BhzyUJ7t0olisf47kYr9qAm0XaUpURMjdBn6TwiVaaMuTFoKkkvPGojd9trKAHlrGGcGKcdR1DA=="
|
"integrity": "sha512-Of3tPmVs3b6BhzyUJ7t0olisf47kYr9qAm0XaUpURMjdBn6TwiVaaMuTFoKkkvPGojd9trKAHlrGGcGKcdR1DA=="
|
||||||
},
|
},
|
||||||
|
"tslib": {
|
||||||
|
"version": "2.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz",
|
||||||
|
"integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"type": {
|
"type": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
|
||||||
|
|
|
@ -6,9 +6,9 @@
|
||||||
"compile": "parcel watch --no-hmr"
|
"compile": "parcel watch --no-hmr"
|
||||||
},
|
},
|
||||||
"source": "./frontend/index.ts",
|
"source": "./frontend/index.ts",
|
||||||
"targets":{
|
"targets": {
|
||||||
"default":{
|
"default": {
|
||||||
"distDir" : "./script/",
|
"distDir": "./script/",
|
||||||
"publicUrl": "./",
|
"publicUrl": "./",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outputFormat": "global",
|
"outputFormat": "global",
|
||||||
|
@ -50,6 +50,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@parcel/packager-ts": "^2.7.0",
|
"@parcel/packager-ts": "^2.7.0",
|
||||||
"@parcel/transformer-typescript-types": "^2.7.0"
|
"@parcel/transformer-typescript-types": "^2.7.0",
|
||||||
|
"tslib": "^2.4.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
107
script/index.js
107
script/index.js
|
@ -148,20 +148,42 @@ parcelHelpers.defineInteropFlag(exports);
|
||||||
var _connection = require("./Connection");
|
var _connection = require("./Connection");
|
||||||
var _eventPool = require("./EventPool");
|
var _eventPool = require("./EventPool");
|
||||||
var _eventPoolDefault = parcelHelpers.interopDefault(_eventPool);
|
var _eventPoolDefault = parcelHelpers.interopDefault(_eventPool);
|
||||||
|
var _eventTarget = require("./EventTarget");
|
||||||
|
var _eventTargetDefault = parcelHelpers.interopDefault(_eventTarget);
|
||||||
var _peer = require("./Peer");
|
var _peer = require("./Peer");
|
||||||
var _peerDefault = parcelHelpers.interopDefault(_peer);
|
var _peerDefault = parcelHelpers.interopDefault(_peer);
|
||||||
var _room = require("./Room");
|
var _room = require("./Room");
|
||||||
var _roomDefault = parcelHelpers.interopDefault(_room);
|
var _roomDefault = parcelHelpers.interopDefault(_room);
|
||||||
var _wstsprotocol = require("./WSTSProtocol");
|
var _wstsprotocol = require("./WSTSProtocol");
|
||||||
var _wstsprotocolDefault = parcelHelpers.interopDefault(_wstsprotocol);
|
var _wstsprotocolDefault = parcelHelpers.interopDefault(_wstsprotocol);
|
||||||
class MWSE {
|
class MWSE extends (0, _eventTargetDefault.default) {
|
||||||
rooms = new Map();
|
rooms = new Map();
|
||||||
pairs = new Map();
|
pairs = new Map();
|
||||||
|
peers = new Map();
|
||||||
constructor(options){
|
constructor(options){
|
||||||
|
super();
|
||||||
this.server = new (0, _connection.Connection)(options);
|
this.server = new (0, _connection.Connection)(options);
|
||||||
this.server.connect();
|
this.server.connect();
|
||||||
this.WSTSProtocol = new (0, _wstsprotocolDefault.default)(this);
|
this.WSTSProtocol = new (0, _wstsprotocolDefault.default)(this);
|
||||||
this.EventPooling = new (0, _eventPoolDefault.default)(this);
|
this.EventPooling = new (0, _eventPoolDefault.default)(this);
|
||||||
|
this.me = new (0, _peerDefault.default)(this);
|
||||||
|
this.me.scope(()=>{
|
||||||
|
this.peers.set("me", this.me);
|
||||||
|
this.peers.set(this.me.socketId, this.me);
|
||||||
|
});
|
||||||
|
this.server.onActive(async ()=>{
|
||||||
|
this.me.setSocketId("me");
|
||||||
|
await this.me.metadata();
|
||||||
|
this.emit("scope");
|
||||||
|
this.activeScope = true;
|
||||||
|
});
|
||||||
|
this.packMessagingSystem();
|
||||||
|
}
|
||||||
|
packMessagingSystem() {
|
||||||
|
this.EventPooling.signal("pack", (payload)=>{
|
||||||
|
let { to , pack } = payload;
|
||||||
|
this.peer(to).emit("message", pack);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
room(options) {
|
room(options) {
|
||||||
let room = new (0, _roomDefault.default)(this);
|
let room = new (0, _roomDefault.default)(this);
|
||||||
|
@ -169,15 +191,20 @@ class MWSE {
|
||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
peer(options) {
|
peer(options) {
|
||||||
|
if (typeof options == "string") {
|
||||||
|
if (this.peers.has(options)) return this.peers.get(options);
|
||||||
|
if (this.pairs.has(options)) return this.pairs.get(options);
|
||||||
|
}
|
||||||
let peer = new (0, _peerDefault.default)(this);
|
let peer = new (0, _peerDefault.default)(this);
|
||||||
peer.setPeerOptions(options);
|
peer.setPeerOptions(options);
|
||||||
|
this.emit("peer", peer);
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.default = MWSE;
|
exports.default = MWSE;
|
||||||
window.MWSE = MWSE;
|
window.MWSE = MWSE;
|
||||||
|
|
||||||
},{"./Connection":"dzYK1","./EventPool":"37Faq","./Peer":"bVhKw","./Room":"7qlv2","./WSTSProtocol":"3kvWC","@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"dzYK1":[function(require,module,exports) {
|
},{"./Connection":"dzYK1","./EventPool":"37Faq","./Peer":"bVhKw","./Room":"7qlv2","./WSTSProtocol":"3kvWC","@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE","./EventTarget":"lleyn"}],"dzYK1":[function(require,module,exports) {
|
||||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||||
parcelHelpers.defineInteropFlag(exports);
|
parcelHelpers.defineInteropFlag(exports);
|
||||||
parcelHelpers.export(exports, "Connection", ()=>Connection);
|
parcelHelpers.export(exports, "Connection", ()=>Connection);
|
||||||
|
@ -221,7 +248,10 @@ class Connection {
|
||||||
else this.activeConnectionEvent.push(func);
|
else this.activeConnectionEvent.push(func);
|
||||||
}
|
}
|
||||||
eventMessage(data) {
|
eventMessage(data) {
|
||||||
if (typeof data == "string") for (const callback of this.recaivePackEvent)callback(JSON.parse(data));
|
if (typeof data == "string") {
|
||||||
|
let $data = JSON.parse(data);
|
||||||
|
for (const callback of this.recaivePackEvent)callback($data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tranferToServer(data) {
|
tranferToServer(data) {
|
||||||
if (this.connected) this.ws.send(JSON.stringify(data));
|
if (this.connected) this.ws.send(JSON.stringify(data));
|
||||||
|
@ -305,16 +335,79 @@ exports.default = EventPool;
|
||||||
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"bVhKw":[function(require,module,exports) {
|
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"bVhKw":[function(require,module,exports) {
|
||||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||||
parcelHelpers.defineInteropFlag(exports);
|
parcelHelpers.defineInteropFlag(exports);
|
||||||
class Peer {
|
var _eventTarget = require("./EventTarget");
|
||||||
|
var _eventTargetDefault = parcelHelpers.interopDefault(_eventTarget);
|
||||||
|
class Peer extends (0, _eventTargetDefault.default) {
|
||||||
|
selfSocket = false;
|
||||||
|
active = false;
|
||||||
constructor(wsts){
|
constructor(wsts){
|
||||||
this.wsts = wsts;
|
super();
|
||||||
|
this.mwse = wsts;
|
||||||
}
|
}
|
||||||
setPeerOptions(options) {
|
setPeerOptions(options) {
|
||||||
this.options = options;
|
if (typeof options == "string") this.setSocketId(options);
|
||||||
|
else this.options = options;
|
||||||
|
}
|
||||||
|
setSocketId(uuid) {
|
||||||
|
this.socketId = uuid;
|
||||||
|
}
|
||||||
|
async metadata() {
|
||||||
|
if (this.socketId == "me") {
|
||||||
|
let result = await this.mwse.EventPooling.request({
|
||||||
|
type: "my/socketid"
|
||||||
|
});
|
||||||
|
this.selfSocket = true;
|
||||||
|
this.active ||= true;
|
||||||
|
this.socketId = result;
|
||||||
|
this.emit("scope");
|
||||||
|
this.activeScope = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async enablePairAuth() {
|
||||||
|
await this.mwse.EventPooling.request({
|
||||||
|
type: "auth/pair-system",
|
||||||
|
value: "everybody"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async disablePairAuth() {
|
||||||
|
await this.mwse.EventPooling.request({
|
||||||
|
type: "auth/pair-system",
|
||||||
|
value: "disable"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async send(pack) {
|
||||||
|
await this.mwse.EventPooling.request({
|
||||||
|
type: "pack/to",
|
||||||
|
pack,
|
||||||
|
to: this.socketId
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.default = Peer;
|
exports.default = Peer;
|
||||||
|
|
||||||
|
},{"./EventTarget":"lleyn","@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"lleyn":[function(require,module,exports) {
|
||||||
|
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||||
|
parcelHelpers.defineInteropFlag(exports);
|
||||||
|
class EventTarget {
|
||||||
|
events = {};
|
||||||
|
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
|
||||||
|
];
|
||||||
|
}
|
||||||
|
activeScope = false;
|
||||||
|
scope(f) {
|
||||||
|
if (this.activeScope) f();
|
||||||
|
else this.on("scope", f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.default = EventTarget;
|
||||||
|
|
||||||
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"7qlv2":[function(require,module,exports) {
|
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"7qlv2":[function(require,module,exports) {
|
||||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||||
parcelHelpers.defineInteropFlag(exports);
|
parcelHelpers.defineInteropFlag(exports);
|
||||||
|
@ -375,7 +468,7 @@ class WSTSProtocol {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
PackAnalyze(data) {
|
PackAnalyze(data) {
|
||||||
let [payload, id, action] = JSON.parse(data);
|
let [payload, id, action] = data;
|
||||||
if (typeof id === "number") {
|
if (typeof id === "number") {
|
||||||
let callback = this.mwse.EventPooling.events.get(id);
|
let callback = this.mwse.EventPooling.events.get(id);
|
||||||
if (callback) {
|
if (callback) {
|
||||||
|
|
File diff suppressed because one or more lines are too long
29
test.html
29
test.html
|
@ -9,10 +9,31 @@
|
||||||
<body>
|
<body>
|
||||||
<script src="./script/index.js"></script>
|
<script src="./script/index.js"></script>
|
||||||
<script>
|
<script>
|
||||||
let wsjs = new MWSE({
|
var wsjs;
|
||||||
endpoint: "ws://localhost:8282"
|
async function main(){
|
||||||
});
|
wsjs = new MWSE({
|
||||||
let peer = wsjs.peer('me');
|
endpoint: "ws://localhost:7707"
|
||||||
|
});
|
||||||
|
wsjs.scope(()=>{
|
||||||
|
let me = wsjs.peer('me');
|
||||||
|
me.disablePairAuth();
|
||||||
|
console.log(me.socketId);
|
||||||
|
me.on('message',(...args)=>{
|
||||||
|
console.log(args);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
wsjs.on('peer',(peer)=>{
|
||||||
|
peer.on('message',(...args)=>{
|
||||||
|
console.log(args);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
};
|
||||||
|
main();
|
||||||
|
function sendMessage(id)
|
||||||
|
{
|
||||||
|
let me = wsjs.peer(id);
|
||||||
|
me.send("Merhaba");
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -56,7 +56,7 @@
|
||||||
"outDir": "./script", /* Specify an output folder for all emitted files. */
|
"outDir": "./script", /* Specify an output folder for all emitted files. */
|
||||||
"removeComments": true, /* Disable emitting comments. */
|
"removeComments": true, /* Disable emitting comments. */
|
||||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||||
"importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
"importHelpers": false, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||||
"importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
"importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
||||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||||
|
|
Loading…
Reference in New Issue