updated tests. Data now flows correctly between socket endpoints
This commit is contained in:
@ -33,7 +33,12 @@ export class Smartsocket {
|
||||
plugins.beautylog.log("Socket connected. Trying to authenticate...")
|
||||
this.openSockets.add(socketConnection);
|
||||
socketConnection.authenticate()
|
||||
.then(socketConnection.listenToFunctionRequests);
|
||||
.then(() => {
|
||||
return socketConnection.listenToFunctionRequests();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -57,6 +57,8 @@ export class SmartsocketClient {
|
||||
});
|
||||
this.socketConnection.socket.on("authenticated",() => {
|
||||
console.log("client is authenticated");
|
||||
this.socketConnection.authenticated = true;
|
||||
this.socketConnection.listenToFunctionRequests();
|
||||
done.resolve();
|
||||
});
|
||||
});
|
||||
@ -70,7 +72,7 @@ export class SmartsocketClient {
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
}
|
||||
serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){
|
||||
serverCall(functionNameArg:string,dataArg:any){
|
||||
let done = plugins.q.defer();
|
||||
let socketRequest = new SocketRequest({
|
||||
side:"requesting",
|
||||
@ -82,8 +84,8 @@ export class SmartsocketClient {
|
||||
}
|
||||
});
|
||||
socketRequest.dispatch()
|
||||
.then(() => {
|
||||
done.resolve();
|
||||
.then((dataArg:ISocketFunctionCall) => {
|
||||
done.resolve(dataArg.funcDataArg);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
@ -34,7 +34,7 @@ export interface ISocketConnectionAuthenticationObject {
|
||||
*/
|
||||
export class SocketConnection {
|
||||
alias: string;
|
||||
authenticated: boolean;
|
||||
authenticated: boolean = false;
|
||||
role: SocketRole;
|
||||
socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||
constructor(optionsArg: ISocketConnectionConstructorOptions) {
|
||||
@ -67,6 +67,7 @@ export class SocketConnection {
|
||||
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
|
||||
done.resolve(this);
|
||||
} else {
|
||||
this.authenticated = false;
|
||||
this.socket.disconnect();
|
||||
done.reject("not authenticated");
|
||||
};
|
||||
@ -80,15 +81,17 @@ export class SocketConnection {
|
||||
/**
|
||||
* listen to function requests
|
||||
*/
|
||||
listenToFunctionRequests() {
|
||||
listenToFunctionRequests(){
|
||||
let done = plugins.q.defer();
|
||||
if(this.authenticated){
|
||||
this.socket.on("function", (dataArg:ISocketRequestDataObject) => {
|
||||
// check if requested function is available to the socket's scope
|
||||
plugins.beautylog.log("function request received");
|
||||
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
|
||||
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
||||
});
|
||||
if(referencedFunction !== undefined){
|
||||
plugins.beautylog.ok!("function in access scope");
|
||||
let localSocketRequest = new SocketRequest({
|
||||
side:"responding",
|
||||
originSocketConnection:this,
|
||||
@ -101,11 +104,16 @@ export class SocketConnection {
|
||||
};
|
||||
});
|
||||
this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => {
|
||||
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
|
||||
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
|
||||
targetSocketRequest.handleResponse(dataArg);
|
||||
})
|
||||
});
|
||||
plugins.beautylog.log(`now listening to function requests for ${this.alias}`);
|
||||
done.resolve(this);
|
||||
} else {
|
||||
done.reject("socket needs to be authenticated first");
|
||||
let errMessage: "socket needs to be authenticated first";
|
||||
plugins.beautylog.error(errMessage);
|
||||
done.reject(errMessage);
|
||||
};
|
||||
return done.promise;
|
||||
};
|
||||
|
@ -71,7 +71,11 @@ export class SocketFunction {
|
||||
if(dataArg.funcName === this.name){
|
||||
this.funcDef(dataArg.funcDataArg)
|
||||
.then((resultData:any) => {
|
||||
done.resolve(resultData);
|
||||
let funcResponseData:ISocketFunctionCall = {
|
||||
funcName:this.name,
|
||||
funcDataArg:resultData
|
||||
}
|
||||
done.resolve(funcResponseData);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
@ -47,6 +47,7 @@ export class SocketRequest {
|
||||
this.side = optionsArg.side;
|
||||
this.shortid = optionsArg.shortId;
|
||||
this.funcCallData = optionsArg.funcCallData;
|
||||
this.originSocketConnection = optionsArg.originSocketConnection;
|
||||
allSocketRequests.add(this);
|
||||
};
|
||||
|
||||
@ -68,7 +69,8 @@ export class SocketRequest {
|
||||
* handles the response that is received by the requesting side
|
||||
*/
|
||||
handleResponse(responseDataArg: ISocketRequestDataObject) {
|
||||
this.done.resolve(responseDataArg);
|
||||
plugins.beautylog.log("handling response!");
|
||||
this.done.resolve(responseDataArg.funcCallData);
|
||||
allSocketRequests.remove(this);
|
||||
}
|
||||
|
||||
@ -79,12 +81,14 @@ export class SocketRequest {
|
||||
*/
|
||||
createResponse() {
|
||||
let targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(this.funcCallData.funcName);
|
||||
plugins.beautylog.info(`invoking ${targetSocketFunction.name}`);
|
||||
targetSocketFunction.invoke(this.funcCallData)
|
||||
.then((resultData) => {
|
||||
plugins.beautylog.log("got resultData. Sending it to requesting party.")
|
||||
let requestData: ISocketRequestDataObject = {
|
||||
funcCallData: resultData,
|
||||
shortId: this.shortid
|
||||
}
|
||||
};
|
||||
this.originSocketConnection.socket.emit("functionResponse",requestData);
|
||||
allSocketRequests.remove(this);
|
||||
});
|
||||
|
Reference in New Issue
Block a user