Refactor smartsocket implementation for improved WebSocket handling and message protocol

- Updated test files to use new testing library and reduced test cycles for efficiency.
- Removed dependency on smartexpress and integrated direct WebSocket handling.
- Enhanced Smartsocket and SmartsocketClient classes to support new message types and authentication flow.
- Implemented a new message interface for structured communication between client and server.
- Added external server support for smartserve with appropriate WebSocket hooks.
- Improved connection management and error handling in SocketConnection and SocketRequest classes.
- Cleaned up code and removed deprecated socket.io references in favor of native WebSocket.
This commit is contained in:
2025-12-03 02:20:38 +00:00
parent ee59471e14
commit 1d62c9c695
14 changed files with 3901 additions and 3007 deletions

View File

@@ -1,15 +1,10 @@
// tslint:disable-next-line:no-implicit-dependencies
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import * as isohash from '@push.rocks/isohash';
import * as smartexpress from '@api.global/typedserver';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartsocket from '../ts/index.js';
let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketFunction1: smartsocket.SocketFunction<any>;
let myseServer: smartexpress.servertools.Server;
const testConfig = {
port: 3000,
@@ -21,18 +16,6 @@ tap.test('should create a new smartsocket', async () => {
expect(testSmartsocket).toBeInstanceOf(smartsocket.Smartsocket);
});
tap.test('Should accept an smartExpressServer as server', async () => {
myseServer = new smartexpress.servertools.Server({
cors: true,
forceSsl: false,
port: testConfig.port,
});
testSmartsocket.setExternalServer('smartexpress', myseServer);
await myseServer.start();
});
// class SocketFunction
tap.test('should register a new Function', async () => {
testSocketFunction1 = new smartsocket.SocketFunction({
@@ -71,7 +54,7 @@ tap.test('2 clients should connect in parallel', async () => {
});
tap.test('should be able to make a functionCall from client to server', async () => {
const totalCycles = 20000;
const totalCycles = 100; // Reduced for faster test
let counter = 0;
let startTime = Date.now();
while (counter < totalCycles) {
@@ -80,9 +63,9 @@ tap.test('should be able to make a functionCall from client to server', async ()
value1: randomString,
});
expect(response.value1).toEqual(randomString);
if (counter % 100 === 0) {
if (counter % 50 === 0) {
console.log(
`processed 100 more messages in ${Date.now() - startTime}ms. ${
`processed 50 more messages in ${Date.now() - startTime}ms. ${
totalCycles - counter
} messages to go.`
);
@@ -96,8 +79,8 @@ tap.test('should be able to make a functionCall from server to client', async ()
// terminate
tap.test('should close the server', async () => {
await testSmartsocketClient.stop();
await testSmartsocket.stop();
await myseServer.stop();
});
tap.start();
export default tap.start();

View File

@@ -1,5 +1,4 @@
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@push.rocks/tapbundle';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartsocket from '../ts/index.js';
@@ -64,6 +63,9 @@ tap.test('should react to a new websocket connection from client', async () => {
url: 'http://localhost',
alias: 'testClient1',
autoReconnect: true,
maxRetries: 20,
initialBackoffDelay: 500,
maxBackoffDelay: 3000,
});
testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
await testSmartsocketClient.connect();
@@ -129,7 +131,8 @@ tap.test('should be able to switch to a new server', async (toolsArg) => {
await testSmartsocket.stop();
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver2', port: testConfig.port });
await testSmartsocket.start();
await toolsArg.delayFor(30000);
// Wait for client to reconnect with shorter backoff settings
await toolsArg.delayFor(5000);
});
tap.test('should be able to locate a connection tag after reconnect', async (tools) => {
@@ -149,4 +152,4 @@ tap.test('should close the server', async (tools) => {
tools.delayFor(1000).then(() => process.exit(0));
});
tap.start();
export default tap.start();

View File

@@ -1,5 +1,4 @@
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@push.rocks/tapbundle';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartsocket from '../ts/index.js';
@@ -144,4 +143,4 @@ tap.test('should close the server', async () => {
await testSmartsocket.stop();
});
tap.start();
export default tap.start();