update structure
This commit is contained in:
		| @@ -23,7 +23,7 @@ export class Smartsocket { | |||||||
|     /** |     /** | ||||||
|      * the standard handler for new socket connections |      * the standard handler for new socket connections | ||||||
|      */ |      */ | ||||||
|     private _handleSocket(socketArg) { |     private _handleSocketConnection(socketArg) { | ||||||
|         let socketConnection: SocketConnection =  new SocketConnection({ |         let socketConnection: SocketConnection =  new SocketConnection({ | ||||||
|             authenticated:false, |             authenticated:false, | ||||||
|             socket:socketArg |             socket:socketArg | ||||||
| @@ -41,7 +41,7 @@ export class Smartsocket { | |||||||
|     startServer = () => { |     startServer = () => { | ||||||
|         this.io = plugins.socketIo(this.options.port); |         this.io = plugins.socketIo(this.options.port); | ||||||
|         this.io.on('connection', (socketArg) => { |         this.io.on('connection', (socketArg) => { | ||||||
|             this._handleSocket(socketArg); |             this._handleSocketConnection(socketArg); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|     closeServer = () => { |     closeServer = () => { | ||||||
| @@ -51,5 +51,10 @@ export class Smartsocket { | |||||||
|         }); |         }); | ||||||
|         this.openSockets.wipe(); |         this.openSockets.wipe(); | ||||||
|         this.io.close(); |         this.io.close(); | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     // communication | ||||||
|  |     clientCall(){ | ||||||
|  |         // TODO: target specific client and initiate response         | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -15,14 +15,45 @@ import { SocketRequest } from "./smartsocket.classes.socketrequest"; | |||||||
| export interface ISmartsocketClientOptions { | export interface ISmartsocketClientOptions { | ||||||
|     port: number; |     port: number; | ||||||
|     url: string; |     url: string; | ||||||
|  |     alias?:string; // an alias makes ir easier to identify this client in a multo client environment | ||||||
|  |     password: string; // by setting a password access to functions can be limited | ||||||
| } | } | ||||||
|  |  | ||||||
| export class SmartsocketClient { | export class SmartsocketClient { | ||||||
|     socketConnection:SocketConnection; |     socketConnection:SocketConnection; | ||||||
|  |     serverUrl:string; | ||||||
|  |     serverPort:number; | ||||||
|  |     serverPassword:string; | ||||||
|     constructor(optionsArg:ISmartsocketClientOptions){ |     constructor(optionsArg:ISmartsocketClientOptions){ | ||||||
|         // TODO: implement Socket init |         this.serverUrl = optionsArg.url | ||||||
|  |         this.serverPort = optionsArg.port; | ||||||
|  |         this.serverPassword = optionsArg.password | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     // authenticates the socket against the server | ||||||
|  |     private _handleSocketConnection() { | ||||||
|  |         let done = plugins.q.defer(); | ||||||
|  |         let socketUrl = `${this.serverUrl}:${this.serverPort}`; | ||||||
|  |         this.socketConnection = new SocketConnection({ | ||||||
|  |             authenticated:false, | ||||||
|  |             socket: plugins.socketIoClient(this.serverUrl,{}) | ||||||
|  |         }); | ||||||
|  |         this.socketConnection.socket.on("requestAuth", function () { | ||||||
|  |             console.log("server requested authentication"); | ||||||
|  |             this.socketConnection.socket.emit("dataAuth", { | ||||||
|  |                 role: "coreflowContainer", | ||||||
|  |                 password: "somePassword", | ||||||
|  |                 alias: "coreflow1" | ||||||
|  |             }); | ||||||
|  |             this.socketConnection.socket.on("authenticated",() => { | ||||||
|  |                 console.log("client is authenticated"); | ||||||
|  |                 done.resolve(); | ||||||
|  |             }); | ||||||
|  |         }); | ||||||
|  |         return done.promise; | ||||||
|     }; |     }; | ||||||
|     serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){ |     serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){ | ||||||
|  |         let done = plugins.q.defer(); | ||||||
|         let socketRequest = new SocketRequest({ |         let socketRequest = new SocketRequest({ | ||||||
|             side:"requesting", |             side:"requesting", | ||||||
|             originSocketConnection:this.socketConnection, |             originSocketConnection:this.socketConnection, | ||||||
| @@ -32,7 +63,11 @@ export class SmartsocketClient { | |||||||
|                 funcDataArg:dataArg |                 funcDataArg:dataArg | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|         socketRequest.dispatch(); |         socketRequest.dispatch() | ||||||
|     } |             .then(() => { | ||||||
|  |                 done.resolve(); | ||||||
|  |             }); | ||||||
|  |         return done.promise; | ||||||
|  |     }; | ||||||
|      |      | ||||||
| } | } | ||||||
| @@ -11,11 +11,11 @@ import { SocketRole } from "./smartsocket.classes.socketrole"; | |||||||
| /** | /** | ||||||
|  * interface for constructor of class SocketConnection |  * interface for constructor of class SocketConnection | ||||||
|  */ |  */ | ||||||
| export interface ISocketConnectionOptions { | export interface ISocketConnectionConstructorOptions { | ||||||
|     alias?: string; |     alias?: string; | ||||||
|     authenticated: boolean; |     authenticated: boolean; | ||||||
|     role?: SocketRole; |     role?: SocketRole; | ||||||
|     socket: SocketIO.Socket; |     socket?: SocketIO.Socket | SocketIOClient.Socket; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -36,8 +36,8 @@ export class SocketConnection { | |||||||
|     alias?: string; |     alias?: string; | ||||||
|     authenticated: boolean; |     authenticated: boolean; | ||||||
|     role?: SocketRole; |     role?: SocketRole; | ||||||
|     socket: SocketIO.Socket; |     socket: SocketIO.Socket | SocketIOClient.Socket; | ||||||
|     constructor(optionsArg: ISocketConnectionOptions) { |     constructor(optionsArg: ISocketConnectionConstructorOptions) { | ||||||
|         this.alias = optionsArg.alias; |         this.alias = optionsArg.alias; | ||||||
|         this.authenticated = optionsArg.authenticated; |         this.authenticated = optionsArg.authenticated; | ||||||
|         this.role = optionsArg.role; |         this.role = optionsArg.role; | ||||||
|   | |||||||
| @@ -57,16 +57,29 @@ export class SocketRequest { | |||||||
|             shortId:optionsArg.shortId |             shortId:optionsArg.shortId | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|  |      | ||||||
|  |     // requesting -------------------------- | ||||||
|  |      | ||||||
|     /** |     /** | ||||||
|      *  |      * dispatches a socketrequest from the requesting to the receiving side | ||||||
|      */ |      */ | ||||||
|     dispatch(){ |     dispatch(){ | ||||||
|         this.originSocketConnection.socket.emit("function",this.requestData); |         this.originSocketConnection.socket.emit("function",this.requestData); | ||||||
|         return this.done.promise; |         return this.done.promise; | ||||||
|     }; |     }; | ||||||
|     handleResponse(responseDataArg:ISocketRequestDataObject){ |  | ||||||
|  |     /** | ||||||
|  |      * handles the response that is received by the requesting side | ||||||
|  |      */ | ||||||
|  |     private _handleResponse(responseDataArg:ISocketRequestDataObject){ | ||||||
|         this.done.resolve(responseDataArg); |         this.done.resolve(responseDataArg); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // responding -------------------------- | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * creates the response on the responding side | ||||||
|  |      */ | ||||||
|     createResponse(){ |     createResponse(){ | ||||||
|          |          | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user