now working in both directions so mesh setups work.
This commit is contained in:
@ -1,8 +1,12 @@
|
||||
import * as plugins from "./smartsocket.plugins";
|
||||
|
||||
// export main classes
|
||||
export * from "./smartsocket.classes.smartsocket";
|
||||
export * from "./smartsocket.classes.smartsocketclient";
|
||||
|
||||
// export further classes and objects
|
||||
export * from "./smartsocket.classes.socketfunction";
|
||||
export * from "./smartsocket.classes.socketrole";
|
||||
export {allSocketConnections} from "./smartsocket.classes.socketconnection";
|
||||
|
||||
// need something more exposed? Create an issue on GitLab!
|
||||
|
@ -3,13 +3,13 @@ import * as helpers from "./smartsocket.helpers";
|
||||
|
||||
// classes
|
||||
import { Objectmap } from "lik";
|
||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
import { SocketFunction } from "./smartsocket.classes.socketfunction";
|
||||
import { SocketFunction,ISocketFunctionCall} from "./smartsocket.classes.socketfunction";
|
||||
import { SocketConnection } from "./smartsocket.classes.socketconnection";
|
||||
import { SocketRequest } from "./smartsocket.classes.socketrequest";
|
||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
|
||||
export interface ISmartsocketConstructorOptions {
|
||||
port: number;
|
||||
|
||||
};
|
||||
|
||||
export class Smartsocket {
|
||||
@ -28,6 +28,7 @@ export class Smartsocket {
|
||||
alias:undefined,
|
||||
authenticated:false,
|
||||
role:undefined,
|
||||
side:"server",
|
||||
socket:socketArg
|
||||
});
|
||||
plugins.beautylog.log("Socket connected. Trying to authenticate...")
|
||||
@ -61,8 +62,25 @@ export class Smartsocket {
|
||||
};
|
||||
|
||||
// communication
|
||||
clientCall(){
|
||||
|
||||
// TODO: target specific client and initiate response
|
||||
|
||||
/**
|
||||
* allows call to specific client.
|
||||
*/
|
||||
clientCall(functionNameArg:string,dataArg:any,targetSocketConnectionArg:SocketConnection){
|
||||
let done = plugins.q.defer();
|
||||
let socketRequest = new SocketRequest({
|
||||
side:"requesting",
|
||||
originSocketConnection:targetSocketConnectionArg,
|
||||
shortId:plugins.shortid.generate(),
|
||||
funcCallData:{
|
||||
funcName: functionNameArg,
|
||||
funcDataArg:dataArg
|
||||
}
|
||||
});
|
||||
socketRequest.dispatch()
|
||||
.then((dataArg:ISocketFunctionCall) => {
|
||||
done.resolve(dataArg.funcDataArg);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
}
|
@ -46,6 +46,7 @@ export class SmartsocketClient {
|
||||
alias:this.alias,
|
||||
authenticated:false,
|
||||
role:undefined,
|
||||
side:"client",
|
||||
socket: plugins.socketIoClient(socketUrl,{multiplex:false})
|
||||
});
|
||||
this.socketConnection.socket.on("requestAuth", () => {
|
||||
|
@ -1,6 +1,8 @@
|
||||
import * as plugins from "./smartsocket.plugins";
|
||||
import * as helpers from "./smartsocket.helpers";
|
||||
|
||||
import {Objectmap} from "lik";
|
||||
|
||||
// import classes
|
||||
import { SocketFunction } from "./smartsocket.classes.socketfunction";
|
||||
import { SocketRequest, ISocketRequestDataObject, allSocketRequests } from "./smartsocket.classes.socketrequest";
|
||||
@ -8,6 +10,11 @@ import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
|
||||
// export interfaces
|
||||
|
||||
/**
|
||||
* defines is a SocketConnection is server or client side. Important for mesh setups.
|
||||
*/
|
||||
export type TSocketConnectionSide = "server" | "client";
|
||||
|
||||
/**
|
||||
* interface for constructor of class SocketConnection
|
||||
*/
|
||||
@ -15,6 +22,7 @@ export interface ISocketConnectionConstructorOptions {
|
||||
alias: string;
|
||||
authenticated: boolean;
|
||||
role: SocketRole;
|
||||
side: TSocketConnectionSide;
|
||||
socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||
};
|
||||
|
||||
@ -25,15 +33,17 @@ export interface ISocketConnectionAuthenticationObject {
|
||||
role: "coreflowContainer",
|
||||
password: "somePassword",
|
||||
alias: "coreflow1"
|
||||
}
|
||||
};
|
||||
|
||||
// export classes
|
||||
export let allSocketConnections = new Objectmap<SocketConnection>();
|
||||
|
||||
/**
|
||||
* class SocketConnection represents a websocket connection
|
||||
*/
|
||||
export class SocketConnection {
|
||||
alias: string;
|
||||
side:TSocketConnectionSide;
|
||||
authenticated: boolean = false;
|
||||
role: SocketRole;
|
||||
socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||
@ -41,11 +51,14 @@ export class SocketConnection {
|
||||
this.alias = optionsArg.alias;
|
||||
this.authenticated = optionsArg.authenticated;
|
||||
this.role = optionsArg.role;
|
||||
this.side = optionsArg.side;
|
||||
this.socket = optionsArg.socket;
|
||||
|
||||
// standard behaviour that is always true
|
||||
allSocketConnections.add(this);
|
||||
this.socket.on("disconnect", () => {
|
||||
plugins.beautylog.info(`Client ${this.alias} disconnected`);
|
||||
plugins.beautylog.info(`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`);
|
||||
allSocketConnections.remove(this);
|
||||
});
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user