P2P Messaging complete
This commit is contained in:
parent
4abb19d4c7
commit
ba05a83286
|
@ -13,9 +13,26 @@ addService(({
|
|||
end,
|
||||
next
|
||||
})=>{
|
||||
let {type,username,password,to} = message;
|
||||
let {type,username,password,to,value} = message;
|
||||
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':{
|
||||
client.requiredPair = false;
|
||||
return end({
|
||||
|
|
|
@ -30,6 +30,12 @@ addService(({
|
|||
if(Client.clients.has(to))
|
||||
{
|
||||
let otherPeer = Client.clients.get(to);
|
||||
if(otherPeer.requiredPair && !otherPeer.pairs.has(to))
|
||||
{
|
||||
return handshake && end({
|
||||
type: 'fail'
|
||||
})
|
||||
}
|
||||
otherPeer.send([{
|
||||
from: client.id,
|
||||
pack: pack
|
||||
|
|
|
@ -62,8 +62,9 @@ export class Connection
|
|||
{
|
||||
if(typeof data == "string")
|
||||
{
|
||||
let $data = JSON.parse(data);
|
||||
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";
|
||||
|
||||
interface IPeerOptions{
|
||||
|
||||
};
|
||||
interface IPeerMetadata{
|
||||
|
||||
export default class Peer
|
||||
};
|
||||
|
||||
export default class Peer extends EventTarget
|
||||
{
|
||||
public wsts : MWSE;
|
||||
public mwse : MWSE;
|
||||
public options! : IPeerOptions;
|
||||
public socketId? : string;
|
||||
public selfSocket : boolean = false;
|
||||
public active : boolean = false;
|
||||
constructor(wsts:MWSE){
|
||||
this.wsts = wsts;
|
||||
super();
|
||||
this.mwse = wsts;
|
||||
}
|
||||
setPeerOptions(options:IPeerOptions){
|
||||
this.options = options;
|
||||
setPeerOptions(options: string | IPeerOptions){
|
||||
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)
|
||||
{
|
||||
let [payload, id, action] = JSON.parse(data);
|
||||
let [payload, id, action] = data;
|
||||
if(typeof id === 'number')
|
||||
{
|
||||
let callback = this.mwse.EventPooling.events.get(id);
|
||||
|
|
|
@ -1,19 +1,42 @@
|
|||
import {Connection,IConnection} from "./Connection";
|
||||
import EventPool from "./EventPool";
|
||||
import EventTarget from "./EventTarget";
|
||||
import Peer from "./Peer";
|
||||
import Room, { IRoomOptions } from "./Room";
|
||||
import WSTSProtocol from "./WSTSProtocol";
|
||||
export default class MWSE {
|
||||
export default class MWSE extends EventTarget {
|
||||
public server! : Connection;
|
||||
public WSTSProtocol! : WSTSProtocol;
|
||||
public EventPooling! : EventPool;
|
||||
public rooms : Map<string, Room> = new Map();
|
||||
public pairs : Map<string, Peer> = new Map();
|
||||
public peers : Map<string, Peer> = new Map();
|
||||
public me! : Peer;
|
||||
constructor(options: IConnection){
|
||||
super();
|
||||
this.server = new Connection(options);
|
||||
this.server.connect();
|
||||
this.WSTSProtocol = new WSTSProtocol(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)
|
||||
{
|
||||
|
@ -21,10 +44,22 @@ export default class MWSE {
|
|||
room.setRoomOptions(options);
|
||||
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);
|
||||
peer.setPeerOptions(options);
|
||||
this.emit('peer', peer);
|
||||
return peer;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@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": {
|
||||
|
@ -3484,6 +3485,12 @@
|
|||
"resolved": "https://registry.npmjs.org/tree-kit/-/tree-kit-0.7.4.tgz",
|
||||
"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": {
|
||||
"version": "1.2.0",
|
||||
"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",
|
||||
"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": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
"compile": "parcel watch --no-hmr"
|
||||
},
|
||||
"source": "./frontend/index.ts",
|
||||
"targets":{
|
||||
"default":{
|
||||
"distDir" : "./script/",
|
||||
"targets": {
|
||||
"default": {
|
||||
"distDir": "./script/",
|
||||
"publicUrl": "./",
|
||||
"sourceMap": true,
|
||||
"outputFormat": "global",
|
||||
|
@ -50,6 +50,7 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@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 _eventPool = require("./EventPool");
|
||||
var _eventPoolDefault = parcelHelpers.interopDefault(_eventPool);
|
||||
var _eventTarget = require("./EventTarget");
|
||||
var _eventTargetDefault = parcelHelpers.interopDefault(_eventTarget);
|
||||
var _peer = require("./Peer");
|
||||
var _peerDefault = parcelHelpers.interopDefault(_peer);
|
||||
var _room = require("./Room");
|
||||
var _roomDefault = parcelHelpers.interopDefault(_room);
|
||||
var _wstsprotocol = require("./WSTSProtocol");
|
||||
var _wstsprotocolDefault = parcelHelpers.interopDefault(_wstsprotocol);
|
||||
class MWSE {
|
||||
class MWSE extends (0, _eventTargetDefault.default) {
|
||||
rooms = new Map();
|
||||
pairs = new Map();
|
||||
peers = new Map();
|
||||
constructor(options){
|
||||
super();
|
||||
this.server = new (0, _connection.Connection)(options);
|
||||
this.server.connect();
|
||||
this.WSTSProtocol = new (0, _wstsprotocolDefault.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) {
|
||||
let room = new (0, _roomDefault.default)(this);
|
||||
|
@ -169,15 +191,20 @@ class MWSE {
|
|||
return room;
|
||||
}
|
||||
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);
|
||||
peer.setPeerOptions(options);
|
||||
this.emit("peer", peer);
|
||||
return peer;
|
||||
}
|
||||
}
|
||||
exports.default = 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");
|
||||
parcelHelpers.defineInteropFlag(exports);
|
||||
parcelHelpers.export(exports, "Connection", ()=>Connection);
|
||||
|
@ -221,7 +248,10 @@ class Connection {
|
|||
else this.activeConnectionEvent.push(func);
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||
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){
|
||||
this.wsts = wsts;
|
||||
super();
|
||||
this.mwse = wsts;
|
||||
}
|
||||
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;
|
||||
|
||||
},{"./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) {
|
||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||
parcelHelpers.defineInteropFlag(exports);
|
||||
|
@ -375,7 +468,7 @@ class WSTSProtocol {
|
|||
]);
|
||||
}
|
||||
PackAnalyze(data) {
|
||||
let [payload, id, action] = JSON.parse(data);
|
||||
let [payload, id, action] = data;
|
||||
if (typeof id === "number") {
|
||||
let callback = this.mwse.EventPooling.events.get(id);
|
||||
if (callback) {
|
||||
|
|
File diff suppressed because one or more lines are too long
29
test.html
29
test.html
|
@ -9,10 +9,31 @@
|
|||
<body>
|
||||
<script src="./script/index.js"></script>
|
||||
<script>
|
||||
let wsjs = new MWSE({
|
||||
endpoint: "ws://localhost:8282"
|
||||
});
|
||||
let peer = wsjs.peer('me');
|
||||
var wsjs;
|
||||
async function main(){
|
||||
wsjs = new MWSE({
|
||||
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>
|
||||
</body>
|
||||
</html>
|
|
@ -46,7 +46,7 @@
|
|||
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
||||
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
||||
|
||||
|
||||
/* Emit */
|
||||
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
"declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
|
@ -56,7 +56,7 @@
|
|||
"outDir": "./script", /* Specify an output folder for all emitted files. */
|
||||
"removeComments": true, /* Disable emitting comments. */
|
||||
// "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. */
|
||||
// "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. */
|
||||
|
|
Loading…
Reference in New Issue