smartsocket/ts/smartsocket.classes.smartsocketclient.ts

73 lines
2.5 KiB
TypeScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
import * as plugins from "./smartsocket.plugins"
2016-08-09 09:42:21 +00:00
// import interfaces
2016-08-09 16:22:30 +00:00
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
import { ISocketRequestDataObject } from "./smartsocket.classes.socketrequest"
2016-08-09 09:42:21 +00:00
// import classes
2016-08-09 16:22:30 +00:00
import { SocketConnection } from "./smartsocket.classes.socketconnection";
2016-08-09 09:42:21 +00:00
import { SocketFunction } from "./smartsocket.classes.socketfunction";
2016-08-09 16:22:30 +00:00
import { SocketRequest } from "./smartsocket.classes.socketrequest";
2016-08-07 12:58:20 +00:00
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
2016-08-09 09:42:21 +00:00
port: number;
url: string;
2016-08-11 23:32:57 +00:00
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
2016-08-07 12:58:20 +00:00
}
export class SmartsocketClient {
2016-08-09 16:22:30 +00:00
socketConnection:SocketConnection;
2016-08-11 23:32:57 +00:00
serverUrl:string;
serverPort:number;
serverPassword:string;
2016-08-09 21:37:25 +00:00
constructor(optionsArg:ISmartsocketClientOptions){
2016-08-11 23:32:57 +00:00
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;
2016-08-09 09:42:21 +00:00
};
2016-08-09 16:22:30 +00:00
serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){
2016-08-11 23:32:57 +00:00
let done = plugins.q.defer();
2016-08-09 16:22:30 +00:00
let socketRequest = new SocketRequest({
side:"requesting",
originSocketConnection:this.socketConnection,
shortId:plugins.shortid.generate(),
funcCallData:{
funcName: functionNameArg,
funcDataArg:dataArg
}
});
2016-08-11 23:32:57 +00:00
socketRequest.dispatch()
.then(() => {
done.resolve();
});
return done.promise;
};
2016-08-09 16:22:30 +00:00
2016-08-07 12:58:20 +00:00
}