2020-12-21 21:01:37 +00:00
|
|
|
import { expect, tap } from '@pushrocks/tapbundle';
|
2020-12-26 18:14:19 +00:00
|
|
|
import * as typedrequest from '@apiglobal/typedrequest';
|
|
|
|
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
|
|
|
|
2020-12-21 21:01:37 +00:00
|
|
|
import * as typedsocket from '../ts/index';
|
2020-12-26 18:14:19 +00:00
|
|
|
|
|
|
|
interface IRequest_Client_Server extends typedrequestInterfaces.implementsTR<
|
|
|
|
typedrequestInterfaces.ITypedRequest,
|
|
|
|
IRequest_Client_Server
|
|
|
|
> {
|
|
|
|
method: 'sayhi';
|
|
|
|
request: {
|
|
|
|
greeting: string;
|
|
|
|
};
|
|
|
|
response: {
|
|
|
|
answer: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let testTypedSocketServer: typedsocket.TypedSocket;
|
|
|
|
let testTypedSocketClient: typedsocket.TypedSocket;
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
const testTypedRouter = new typedrequest.TypedRouter();
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
tap.test('should add some handlers', async () => {
|
|
|
|
testTypedRouter.addTypedHandler<IRequest_Client_Server>(new typedrequest.TypedHandler('sayhi', async requestData => {
|
|
|
|
return {
|
|
|
|
answer: `ok, got it : ${requestData.greeting}`
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
|
2020-12-26 18:45:17 +00:00
|
|
|
tap.test('should create Server and Client', async (tools) => {
|
2020-12-26 18:14:19 +00:00
|
|
|
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
|
2021-01-23 05:14:29 +00:00
|
|
|
testTypedSocketClient = await typedsocket.TypedSocket.createClient(testTypedRouter, 'http://localhost:3000');
|
2021-10-22 22:27:46 +00:00
|
|
|
console.log('test: waiting 5 seconds');
|
|
|
|
await tools.delayFor(5000);
|
2021-07-20 00:04:45 +00:00
|
|
|
await testTypedSocketServer.stop();
|
2021-10-22 22:27:46 +00:00
|
|
|
|
|
|
|
// lets create another server
|
2021-07-20 00:04:45 +00:00
|
|
|
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
|
2021-10-22 19:09:42 +00:00
|
|
|
|
|
|
|
// lets see if auto reconnect works
|
2022-01-19 18:17:22 +00:00
|
|
|
console.log('test: waiting 21 seconds for reconnect');
|
|
|
|
await tools.delayFor(21000);
|
2020-12-21 21:01:37 +00:00
|
|
|
});
|
|
|
|
|
2020-12-26 18:45:17 +00:00
|
|
|
tap.test('should process messages from both sides', async () => {
|
|
|
|
const myServerSideTypedRequest = testTypedSocketServer.createTypedRequest<IRequest_Client_Server>('sayhi');
|
|
|
|
const myClientSideTypedRequest = testTypedSocketClient.createTypedRequest<IRequest_Client_Server>('sayhi');
|
|
|
|
const response = await myClientSideTypedRequest.fire({
|
|
|
|
greeting: 'that is a greeting from the client'
|
|
|
|
});
|
|
|
|
console.log(response);
|
|
|
|
const response2 = await myServerSideTypedRequest.fire({
|
|
|
|
greeting: 'that is a greeting from the server'
|
|
|
|
});
|
|
|
|
console.log(response2);
|
|
|
|
})
|
|
|
|
|
2022-01-19 18:17:22 +00:00
|
|
|
tap.test('should disconnect', async (tools) => {
|
2020-12-26 18:14:19 +00:00
|
|
|
await testTypedSocketClient.stop();
|
|
|
|
await testTypedSocketServer.stop();
|
|
|
|
})
|
|
|
|
|
2020-12-21 21:01:37 +00:00
|
|
|
tap.start();
|