feat(typedsocket): Add SmartServe integration, tagging and improved client reconnect/backoff; update deps and tests

This commit is contained in:
2025-12-04 00:00:38 +00:00
parent cace6ca85a
commit 632045edd9
7 changed files with 739 additions and 480 deletions

View File

@@ -1,6 +1,7 @@
import { expect, tap } from '@push.rocks/tapbundle';
import * as typedrequest from '@api.global/typedrequest';
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
import { SmartServe } from '@push.rocks/smartserve';
import * as typedsocket from '../ts/index.js';
@@ -18,12 +19,15 @@ interface IRequest_Client_Server
};
}
let testSmartServe: SmartServe;
let testTypedSocketServer: typedsocket.TypedSocket;
let testTypedSocketClient: typedsocket.TypedSocket;
const testTypedRouter = new typedrequest.TypedRouter();
const clientTypedRouter = new typedrequest.TypedRouter();
tap.test('should add some handlers', async () => {
// Server-side handler
testTypedRouter.addTypedHandler<IRequest_Client_Server>(
new typedrequest.TypedHandler('sayhi', async (requestData) => {
return {
@@ -31,45 +35,86 @@ tap.test('should add some handlers', async () => {
};
})
);
// Client-side handler (for server-to-client messages)
clientTypedRouter.addTypedHandler<IRequest_Client_Server>(
new typedrequest.TypedHandler('sayhi', async (requestData) => {
return {
answer: `client got: ${requestData.greeting}`,
};
})
);
});
tap.test('should create Server and Client', async (tools) => {
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
// Create SmartServe with TypedRouter for WebSocket
testSmartServe = new SmartServe({
port: 3000,
websocket: {
typedRouter: testTypedRouter,
},
});
await testSmartServe.start();
console.log('SmartServe started on port 3000');
// Create TypedSocket server from SmartServe
testTypedSocketServer = typedsocket.TypedSocket.fromSmartServe(testSmartServe, testTypedRouter);
console.log('TypedSocket server created');
// Create client
testTypedSocketClient = await typedsocket.TypedSocket.createClient(
testTypedRouter,
clientTypedRouter,
'http://localhost:3000'
);
console.log('test: waiting 5 seconds');
await tools.delayFor(5000);
await testTypedSocketServer.stop();
console.log('TypedSocket client connected');
// lets create another server
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
// lets see if auto reconnect works
console.log('test: waiting 21 seconds for reconnect');
await tools.delayFor(21000);
console.log('test: waiting 1 second for connection to stabilize');
await tools.delayFor(1000);
});
tap.test('should process messages from both sides', async () => {
const myServerSideTypedRequest =
testTypedSocketServer.createTypedRequest<IRequest_Client_Server>('sayhi');
tap.test('should set tags via TypedRequest', async () => {
console.log('Setting tag...');
await testTypedSocketClient.setTag('testTag', { userId: 123 });
console.log('Tag set successfully');
});
tap.test('should process messages from client to server', async () => {
console.log('Testing client to server...');
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({
console.log('Client got response:', response);
expect(response.answer).toContain('ok, got it');
});
tap.test('should find connections by tag', async () => {
console.log('Finding connections by tag...');
const connections = await testTypedSocketServer.findAllTargetConnectionsByTag('testTag');
console.log(`Found ${connections.length} connections with tag`);
expect(connections.length).toEqual(1);
});
tap.test('should process messages from server to client', async () => {
console.log('Testing server to client...');
const connections = await testTypedSocketServer.findAllTargetConnectionsByTag('testTag');
const myServerSideTypedRequest =
testTypedSocketServer.createTypedRequest<IRequest_Client_Server>('sayhi', connections[0]);
const response = await myServerSideTypedRequest.fire({
greeting: 'that is a greeting from the server',
});
console.log(response2);
console.log('Server got response:', response);
expect(response.answer).toContain('client got');
});
tap.test('should disconnect', async (tools) => {
console.log('Stopping client...');
await testTypedSocketClient.stop();
await testTypedSocketServer.stop();
tools.delayFor(1000).then(() => process.exit(0));
console.log('Stopping server...');
await testSmartServe.stop();
console.log('All stopped');
tools.delayFor(500).then(() => process.exit(0));
});
tap.start();
export default tap.start();