smartsocket/ts/smartsocket.classes.smartsocketclient.ts

91 lines
3.0 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-14 01:25:26 +00:00
alias:string; // an alias makes it easier to identify this client in a multo client environment
role:string;
2016-08-11 23:32:57 +00:00
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-14 01:25:26 +00:00
alias:string;
role:string;
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-14 01:25:26 +00:00
this.alias = optionsArg.alias;
this.role = optionsArg.role;
2016-08-11 23:32:57 +00:00
this.serverUrl = optionsArg.url
this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password
};
2016-08-14 01:25:26 +00:00
/**
* connect the client to the server
*/
connect(){
2016-08-11 23:32:57 +00:00
let done = plugins.q.defer();
2016-08-14 01:25:26 +00:00
plugins.beautylog.log("trying to connect...");
2016-08-11 23:32:57 +00:00
let socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new SocketConnection({
2016-08-14 01:25:26 +00:00
alias:this.alias,
2016-08-11 23:32:57 +00:00
authenticated:false,
2016-08-14 01:25:26 +00:00
role:undefined,
2016-08-14 20:17:55 +00:00
socket: plugins.socketIoClient(socketUrl,{multiplex:false})
2016-08-11 23:32:57 +00:00
});
2016-08-14 01:25:26 +00:00
this.socketConnection.socket.on("requestAuth", () => {
2016-08-11 23:32:57 +00:00
console.log("server requested authentication");
this.socketConnection.socket.emit("dataAuth", {
2016-08-14 01:25:26 +00:00
role: this.role,
password: this.serverPassword,
alias: this.alias
2016-08-11 23:32:57 +00:00
});
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-14 01:25:26 +00:00
disconnect(){
let done = plugins.q.defer();
this.socketConnection.socket.disconnect();
2016-08-14 20:17:55 +00:00
this.socketConnection = undefined;
2016-08-14 01:25:26 +00:00
plugins.beautylog.ok("disconnected!");
done.resolve();
return done.promise;
}
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
}