smartsocket/ts/smartsocket.classes.smartsocketclient.ts

90 lines
2.9 KiB
TypeScript
Raw Normal View History

2016-08-07 14:58:20 +02:00
import * as plugins from "./smartsocket.plugins"
2016-08-09 11:42:21 +02:00
// import interfaces
2016-08-09 18:22:30 +02:00
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
import { ISocketRequestDataObject } from "./smartsocket.classes.socketrequest"
2016-08-09 11:42:21 +02:00
// import classes
2016-08-09 18:22:30 +02:00
import { SocketConnection } from "./smartsocket.classes.socketconnection";
2016-08-09 11:42:21 +02:00
import { SocketFunction } from "./smartsocket.classes.socketfunction";
2016-08-09 18:22:30 +02:00
import { SocketRequest } from "./smartsocket.classes.socketrequest";
2016-08-07 14:58:20 +02:00
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
2016-08-09 11:42:21 +02:00
port: number;
url: string;
2016-08-14 03:25:26 +02:00
alias:string; // an alias makes it easier to identify this client in a multo client environment
role:string;
2016-08-12 01:32:57 +02:00
password: string; // by setting a password access to functions can be limited
2016-08-07 14:58:20 +02:00
}
export class SmartsocketClient {
2016-08-14 03:25:26 +02:00
alias:string;
role:string;
2016-08-09 18:22:30 +02:00
socketConnection:SocketConnection;
2016-08-12 01:32:57 +02:00
serverUrl:string;
serverPort:number;
serverPassword:string;
2016-08-09 23:37:25 +02:00
constructor(optionsArg:ISmartsocketClientOptions){
2016-08-14 03:25:26 +02:00
this.alias = optionsArg.alias;
this.role = optionsArg.role;
2016-08-12 01:32:57 +02:00
this.serverUrl = optionsArg.url
this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password
};
2016-08-14 03:25:26 +02:00
/**
* connect the client to the server
*/
connect(){
2016-08-12 01:32:57 +02:00
let done = plugins.q.defer();
2016-08-14 03:25:26 +02:00
plugins.beautylog.log("trying to connect...");
2016-08-12 01:32:57 +02:00
let socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new SocketConnection({
2016-08-14 03:25:26 +02:00
alias:this.alias,
2016-08-12 01:32:57 +02:00
authenticated:false,
2016-08-14 03:25:26 +02:00
role:undefined,
socket: plugins.socketIoClient(socketUrl,{})
2016-08-12 01:32:57 +02:00
});
2016-08-14 03:25:26 +02:00
this.socketConnection.socket.on("requestAuth", () => {
2016-08-12 01:32:57 +02:00
console.log("server requested authentication");
this.socketConnection.socket.emit("dataAuth", {
2016-08-14 03:25:26 +02:00
role: this.role,
password: this.serverPassword,
alias: this.alias
2016-08-12 01:32:57 +02:00
});
this.socketConnection.socket.on("authenticated",() => {
console.log("client is authenticated");
done.resolve();
});
});
return done.promise;
2016-08-09 11:42:21 +02:00
};
2016-08-14 03:25:26 +02:00
disconnect(){
let done = plugins.q.defer();
this.socketConnection.socket.disconnect();
plugins.beautylog.ok("disconnected!");
done.resolve();
return done.promise;
}
2016-08-09 18:22:30 +02:00
serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){
2016-08-12 01:32:57 +02:00
let done = plugins.q.defer();
2016-08-09 18:22:30 +02:00
let socketRequest = new SocketRequest({
side:"requesting",
originSocketConnection:this.socketConnection,
shortId:plugins.shortid.generate(),
funcCallData:{
funcName: functionNameArg,
funcDataArg:dataArg
}
});
2016-08-12 01:32:57 +02:00
socketRequest.dispatch()
.then(() => {
done.resolve();
});
return done.promise;
};
2016-08-09 18:22:30 +02:00
2016-08-07 14:58:20 +02:00
}