Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
2754447aae | |||
544277cb8a | |||
9a23960d21 | |||
23cca6cce3 | |||
2a9e58cc35 | |||
7d6a9921b5 | |||
3ee46a31f7 | |||
d72310ce10 | |||
1e14166ddb | |||
be38e91548 |
22411
package-lock.json
generated
22411
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartsocket",
|
"name": "@pushrocks/smartsocket",
|
||||||
"version": "1.2.4",
|
"version": "1.2.9",
|
||||||
"description": "easy and secure websocket communication",
|
"description": "easy and secure websocket communication",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
@ -22,26 +22,26 @@
|
|||||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||||
"@pushrocks/isohash": "^1.0.2",
|
"@pushrocks/isohash": "^1.0.2",
|
||||||
"@pushrocks/isounique": "^1.0.4",
|
"@pushrocks/isounique": "^1.0.4",
|
||||||
"@pushrocks/lik": "^4.0.20",
|
"@pushrocks/lik": "^5.0.0",
|
||||||
"@pushrocks/smartdelay": "^2.0.10",
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
"@pushrocks/smartenv": "^4.0.16",
|
"@pushrocks/smartenv": "^4.0.16",
|
||||||
"@pushrocks/smartexpress": "^3.0.100",
|
"@pushrocks/smartexpress": "^3.0.108",
|
||||||
"@pushrocks/smartjson": "^4.0.5",
|
"@pushrocks/smartjson": "^4.0.6",
|
||||||
"@pushrocks/smartlog": "^2.0.39",
|
"@pushrocks/smartlog": "^2.0.44",
|
||||||
"@pushrocks/smartpromise": "^3.1.3",
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
"@pushrocks/smartrx": "^2.0.19",
|
"@pushrocks/smartrx": "^2.0.19",
|
||||||
"@pushrocks/smarttime": "^3.0.38",
|
"@pushrocks/smarttime": "^3.0.43",
|
||||||
"@types/socket.io": "^2.1.13",
|
"@types/socket.io": "^3.0.2",
|
||||||
"@types/socket.io-client": "^1.4.35",
|
"@types/socket.io-client": "^3.0.0",
|
||||||
"socket.io": "^3.1.0",
|
"socket.io": "^3.1.0",
|
||||||
"socket.io-client": "^3.1.0"
|
"socket.io-client": "^3.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.28",
|
||||||
"@gitzone/tsrun": "^1.2.12",
|
"@gitzone/tsrun": "^1.2.18",
|
||||||
"@gitzone/tstest": "^1.0.52",
|
"@gitzone/tstest": "^1.0.60",
|
||||||
"@pushrocks/tapbundle": "^3.2.10",
|
"@pushrocks/tapbundle": "^3.2.15",
|
||||||
"@types/node": "^14.14.22",
|
"@types/node": "^17.0.9",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
|
28
test/test.ts
28
test/test.ts
@ -90,16 +90,30 @@ tap.test('should react to a new websocket connection from client', async () => {
|
|||||||
await testSmartsocketClient.connect();
|
await testSmartsocketClient.connect();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should be able to tag a connection', async (tools) => {
|
tap.test('should be able to tag a connection from client', async (tools) => {
|
||||||
await testSmartsocketClient.addTag({
|
await testSmartsocketClient.addTag({
|
||||||
id: 'awesome',
|
id: 'awesome',
|
||||||
payload: 'yes'
|
payload: 'yes',
|
||||||
});
|
});
|
||||||
const tagOnServerSide = await testSmartsocket.socketConnections.find((socketConnection) => {
|
const tagOnServerSide = await testSmartsocket.socketConnections
|
||||||
return true;
|
.findSync((socketConnection) => {
|
||||||
}).getTagById('awesome');
|
return true;
|
||||||
|
})
|
||||||
|
.getTagById('awesome');
|
||||||
expect(tagOnServerSide.payload).to.equal('yes');
|
expect(tagOnServerSide.payload).to.equal('yes');
|
||||||
})
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to tag a connection from server', async (tools) => {
|
||||||
|
await testSmartsocket.socketConnections
|
||||||
|
.findSync((socketConnection) => {
|
||||||
|
return true;
|
||||||
|
}).addTag({
|
||||||
|
id: 'awesome2',
|
||||||
|
payload: 'absolutely',
|
||||||
|
});
|
||||||
|
const tagOnClientSide = await testSmartsocketClient.socketConnection.getTagById('awesome2');
|
||||||
|
expect(tagOnClientSide.payload).to.equal('absolutely');
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('2 clients should connect in parallel', async () => {
|
tap.test('2 clients should connect in parallel', async () => {
|
||||||
// TODO: implement parallel test
|
// TODO: implement parallel test
|
||||||
@ -119,7 +133,7 @@ tap.test('should be able to make a functionCall from server to client', async ()
|
|||||||
{
|
{
|
||||||
hi: 'hi there from server',
|
hi: 'hi there from server',
|
||||||
},
|
},
|
||||||
testSmartsocket.socketConnections.find((socketConnection) => {
|
testSmartsocket.socketConnections.findSync((socketConnection) => {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
export interface ITag <T = any> {
|
export interface ITag<T = any> {
|
||||||
id: string;
|
id: string;
|
||||||
payload: T;
|
payload: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TTagStore = {[key: string]: ITag};
|
export type TTagStore = { [key: string]: ITag };
|
||||||
|
@ -113,7 +113,7 @@ export class Smartsocket {
|
|||||||
/**
|
/**
|
||||||
* the standard handler for new socket connections
|
* the standard handler for new socket connections
|
||||||
*/
|
*/
|
||||||
private async _handleSocketConnection(socketArg: pluginsTyped.socketIo.Socket) {
|
private async _handleSocketConnection(socketArg: SocketIO.Socket) {
|
||||||
const socketConnection: SocketConnection = new SocketConnection({
|
const socketConnection: SocketConnection = new SocketConnection({
|
||||||
alias: undefined,
|
alias: undefined,
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
@ -124,6 +124,12 @@ export class Smartsocket {
|
|||||||
});
|
});
|
||||||
logger.log('info', 'Socket connected. Trying to authenticate...');
|
logger.log('info', 'Socket connected. Trying to authenticate...');
|
||||||
this.socketConnections.add(socketConnection);
|
this.socketConnections.add(socketConnection);
|
||||||
|
const disconnectSubscription = socketConnection.eventSubject.subscribe((eventArg) => {
|
||||||
|
if (eventArg === 'disconnected') {
|
||||||
|
this.socketConnections.remove(socketConnection);
|
||||||
|
disconnectSubscription.unsubscribe();
|
||||||
|
}
|
||||||
|
})
|
||||||
await socketConnection.authenticate();
|
await socketConnection.authenticate();
|
||||||
await socketConnection.listenToFunctionRequests();
|
await socketConnection.listenToFunctionRequests();
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ export class SmartsocketClient {
|
|||||||
public socketRoles = new plugins.lik.ObjectMap<SocketRole>();
|
public socketRoles = new plugins.lik.ObjectMap<SocketRole>();
|
||||||
|
|
||||||
// tagStore
|
// tagStore
|
||||||
private tagStore: {[key: string]: interfaces.ITag} = {};
|
private tagStore: { [key: string]: interfaces.ITag } = {};
|
||||||
private tagStoreSubscription: plugins.smartrx.rxjs.Subscription;
|
private tagStoreSubscription: plugins.smartrx.rxjs.Subscription;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,7 +66,7 @@ export class SmartsocketClient {
|
|||||||
*/
|
*/
|
||||||
public async getTagById(tagIdArg: interfaces.ITag['id']) {
|
public async getTagById(tagIdArg: interfaces.ITag['id']) {
|
||||||
return this.tagStore[tagIdArg];
|
return this.tagStore[tagIdArg];
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* removes a tag from a connection
|
* removes a tag from a connection
|
||||||
@ -164,12 +164,15 @@ export class SmartsocketClient {
|
|||||||
// handle connection
|
// handle connection
|
||||||
this.socketConnection.socket.on('connect', async () => {
|
this.socketConnection.socket.on('connect', async () => {
|
||||||
this.tagStoreSubscription?.unsubscribe();
|
this.tagStoreSubscription?.unsubscribe();
|
||||||
this.tagStoreSubscription = this.socketConnection.tagStoreObservable.subscribe(tagStoreArg => {
|
|
||||||
this.tagStore = tagStoreArg
|
|
||||||
});
|
|
||||||
for (const keyArg of Object.keys(this.tagStore)) {
|
for (const keyArg of Object.keys(this.tagStore)) {
|
||||||
this.socketConnection.addTag(this.tagStore[keyArg]);
|
this.socketConnection.addTag(this.tagStore[keyArg]);
|
||||||
}
|
}
|
||||||
|
this.tagStoreSubscription = this.socketConnection.tagStoreObservable.subscribe(
|
||||||
|
(tagStoreArg) => {
|
||||||
|
this.tagStore = tagStoreArg;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
this.updateStatus('connected');
|
this.updateStatus('connected');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import { SocketRequest, ISocketRequestDataObject } from './smartsocket.classes.s
|
|||||||
import { SocketRole } from './smartsocket.classes.socketrole';
|
import { SocketRole } from './smartsocket.classes.socketrole';
|
||||||
|
|
||||||
// socket.io
|
// socket.io
|
||||||
import * as SocketIO from 'socket.io';
|
import * as pluginsTyped from './smartsocket.pluginstyped';
|
||||||
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
|
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
|
||||||
import { logger } from './smartsocket.logging';
|
import { logger } from './smartsocket.logging';
|
||||||
|
|
||||||
@ -83,6 +83,7 @@ export class SocketConnection {
|
|||||||
);
|
);
|
||||||
await this.disconnect();
|
await this.disconnect();
|
||||||
allSocketConnections.remove(this);
|
allSocketConnections.remove(this);
|
||||||
|
this.eventSubject.next('disconnected');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,12 +96,12 @@ export class SocketConnection {
|
|||||||
this.tagStoreObservable.next(this.tagStore);
|
this.tagStoreObservable.next(this.tagStore);
|
||||||
const remoteSubscription = this.remoteTagStoreObservable.subscribe((remoteTagStore) => {
|
const remoteSubscription = this.remoteTagStoreObservable.subscribe((remoteTagStore) => {
|
||||||
const localTagString = plugins.smartjson.stringify(tagArg);
|
const localTagString = plugins.smartjson.stringify(tagArg);
|
||||||
const remoteTagString = plugins.smartjson.stringify(remoteTagStore[tagArg.id])
|
const remoteTagString = plugins.smartjson.stringify(remoteTagStore[tagArg.id]);
|
||||||
if (localTagString === remoteTagString) {
|
if (localTagString === remoteTagString) {
|
||||||
remoteSubscription.unsubscribe();
|
remoteSubscription.unsubscribe();
|
||||||
done.resolve();
|
done.resolve();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
this.socket.emit('updateTagStore', this.tagStore);
|
this.socket.emit('updateTagStore', this.tagStore);
|
||||||
await done.promise;
|
await done.promise;
|
||||||
}
|
}
|
||||||
@ -111,7 +112,7 @@ export class SocketConnection {
|
|||||||
*/
|
*/
|
||||||
public async getTagById(tagIdArg: interfaces.ITag['id']) {
|
public async getTagById(tagIdArg: interfaces.ITag['id']) {
|
||||||
return this.tagStore[tagIdArg];
|
return this.tagStore[tagIdArg];
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* removes a tag from a connection
|
* removes a tag from a connection
|
||||||
@ -164,7 +165,7 @@ export class SocketConnection {
|
|||||||
this.socket.on('function', (dataArg: ISocketRequestDataObject<any>) => {
|
this.socket.on('function', (dataArg: ISocketRequestDataObject<any>) => {
|
||||||
// check if requested function is available to the socket's scope
|
// check if requested function is available to the socket's scope
|
||||||
// logger.log('info', 'function request received');
|
// logger.log('info', 'function request received');
|
||||||
const referencedFunction: SocketFunction<any> = this.role.allowedFunctions.find(
|
const referencedFunction: SocketFunction<any> = this.role.allowedFunctions.findSync(
|
||||||
(socketFunctionArg) => {
|
(socketFunctionArg) => {
|
||||||
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
||||||
}
|
}
|
||||||
@ -192,17 +193,13 @@ export class SocketConnection {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('updateTagStore', async (tagStoreArg: interfaces.TTagStore) => {
|
this.socket.on('updateTagStore', async (tagStoreArg: interfaces.TTagStore) => {
|
||||||
const exitingStoreString = plugins.smartjson.stringify(this.tagStore);
|
if (!plugins.smartjson.deepEqualObjects(this.tagStore, tagStoreArg)) {
|
||||||
const newStoreString = plugins.smartjson.stringify(tagStoreArg);
|
|
||||||
console.log(exitingStoreString);
|
|
||||||
console.log(newStoreString);
|
|
||||||
if (exitingStoreString !== newStoreString) {
|
|
||||||
this.tagStore = tagStoreArg;
|
this.tagStore = tagStoreArg;
|
||||||
this.socket.emit('updateTagStore', this.tagStore);
|
this.socket.emit('updateTagStore', this.tagStore);
|
||||||
this.tagStoreObservable.next(this.tagStore);
|
this.tagStoreObservable.next(this.tagStore);
|
||||||
}
|
}
|
||||||
this.remoteTagStoreObservable.next(tagStoreArg);
|
this.remoteTagStoreObservable.next(tagStoreArg);
|
||||||
})
|
});
|
||||||
|
|
||||||
logger.log('info', `now listening to function requests for ${this.alias}`);
|
logger.log('info', `now listening to function requests for ${this.alias}`);
|
||||||
done.resolve(this);
|
done.resolve(this);
|
||||||
|
@ -58,7 +58,7 @@ export class SocketFunction<T extends plugins.typedrequestInterfaces.ITypedReque
|
|||||||
smartsocketRefArg: Smartsocket | SmartsocketClient,
|
smartsocketRefArg: Smartsocket | SmartsocketClient,
|
||||||
functionNameArg: string
|
functionNameArg: string
|
||||||
): SocketFunction<Q> {
|
): SocketFunction<Q> {
|
||||||
return smartsocketRefArg.socketFunctions.find((socketFunctionArg) => {
|
return smartsocketRefArg.socketFunctions.findSync((socketFunctionArg) => {
|
||||||
return socketFunctionArg.name === functionNameArg;
|
return socketFunctionArg.name === functionNameArg;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedReques
|
|||||||
smartsocketRef: Smartsocket | SmartsocketClient,
|
smartsocketRef: Smartsocket | SmartsocketClient,
|
||||||
shortIdArg: string
|
shortIdArg: string
|
||||||
): SocketRequest<any> {
|
): SocketRequest<any> {
|
||||||
return smartsocketRef.socketRequests.find((socketRequestArg) => {
|
return smartsocketRef.socketRequests.findSync((socketRequestArg) => {
|
||||||
return socketRequestArg.shortid === shortIdArg;
|
return socketRequestArg.shortid === shortIdArg;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ export class SocketRole {
|
|||||||
referenceSmartsocket: Smartsocket | SmartsocketClient,
|
referenceSmartsocket: Smartsocket | SmartsocketClient,
|
||||||
socketRoleNameArg: string
|
socketRoleNameArg: string
|
||||||
): SocketRole {
|
): SocketRole {
|
||||||
return referenceSmartsocket.socketRoles.find((socketRoleArg) => {
|
return referenceSmartsocket.socketRoles.findSync((socketRoleArg) => {
|
||||||
return socketRoleArg.name === socketRoleNameArg;
|
return socketRoleArg.name === socketRoleNameArg;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,4 @@ export { smartexpress };
|
|||||||
import type socketIo from 'socket.io';
|
import type socketIo from 'socket.io';
|
||||||
import type socketIoClient from 'socket.io-client';
|
import type socketIoClient from 'socket.io-client';
|
||||||
|
|
||||||
export { socketIo, socketIoClient };
|
export { socketIoClient, socketIo };
|
||||||
|
Reference in New Issue
Block a user