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

@@ -6,16 +6,23 @@ import { Smartsocket } from './smartsocket.classes.smartsocket.js';
import { logger } from './smartsocket.logging.js';
/**
* class socketServer
* handles the attachment of socketIo to whatever server is in play
* class SocketServer
* handles the WebSocket server, either standalone or integrated with smartserve
*/
export class SocketServer {
private smartsocket: Smartsocket;
private httpServerDeferred: plugins.smartpromise.Deferred<any>;
private httpServer: pluginsTyped.http.Server | pluginsTyped.https.Server;
private wsServer: pluginsTyped.ws.WebSocketServer;
/**
* wether httpServer is standalone
* whether we're using an external server (smartserve)
*/
private externalServerMode = false;
private externalServer: any = null;
private externalWebSocketHooks: pluginsTyped.ISmartserveWebSocketHooks = null;
/**
* whether httpServer is standalone
*/
private standaloneServer = false;
@@ -24,71 +31,200 @@ export class SocketServer {
}
/**
* starts the server with another server
* also works with an express style server
* Set an external server (smartserve) for WebSocket handling
*/
public async setExternalServer(
serverType: 'smartexpress',
serverArg: pluginsTyped.typedserver.servertools.Server
serverType: 'smartserve',
serverArg: any,
websocketHooks?: pluginsTyped.ISmartserveWebSocketHooks
) {
this.httpServerDeferred = plugins.smartpromise.defer();
await serverArg.startedPromise;
this.httpServer = serverArg.httpServer;
this.httpServerDeferred.resolve();
if (serverType !== 'smartserve') {
throw new Error(`Unsupported server type: ${serverType}. Only 'smartserve' is supported.`);
}
this.externalServerMode = true;
this.externalServer = serverArg;
this.externalWebSocketHooks = websocketHooks || null;
}
/**
* gets the server for socket.io
*/
public async getServerForSocketIo() {
if (this.httpServerDeferred) {
await this.httpServerDeferred.promise;
}
if (this.httpServer) {
return this.httpServer;
} else {
const httpModule = await this.smartsocket.smartenv.getSafeNodeModule('http');
this.httpServer = new httpModule.Server();
this.standaloneServer = true;
return this.httpServer;
}
}
/**
* starts listening to incoming sockets:
* starts listening to incoming websocket connections
*/
public async start() {
const done = plugins.smartpromise.defer();
// handle http servers
// in case an external server has been set "this.standaloneServer" should be false
if (this.httpServer && this.standaloneServer) {
if (this.externalServerMode) {
// Using external smartserve server
// The smartserve server should be configured with websocket hooks
// that call our handleNewConnection method
logger.log('info', 'Using external smartserve server for WebSocket handling');
// If smartserve provides a way to get the underlying http server for upgrade,
// we could attach ws to it. For now, we expect smartserve to handle WS
// and call us back via the hooks.
done.resolve();
} else {
// Standalone mode - create our own HTTP server and WebSocket server
const httpModule = await this.smartsocket.smartenv.getSafeNodeModule('http');
const wsModule = await this.smartsocket.smartenv.getSafeNodeModule('ws');
if (!this.smartsocket.options.port) {
logger.log('error', 'there should be a port specifed for smartsocket!');
logger.log('error', 'there should be a port specified for smartsocket!');
throw new Error('there should be a port specified for smartsocket');
}
this.httpServer = httpModule.createServer();
this.standaloneServer = true;
// Create WebSocket server attached to HTTP server
this.wsServer = new wsModule.WebSocketServer({ server: this.httpServer });
this.wsServer.on('connection', (ws: pluginsTyped.ws.WebSocket) => {
this.smartsocket.handleNewConnection(ws);
});
this.httpServer.listen(this.smartsocket.options.port, () => {
logger.log(
'success',
`Server started in standalone mode on ${this.smartsocket.options.port}`
`Server started in standalone mode on port ${this.smartsocket.options.port}`
);
done.resolve();
});
} else {
done.resolve();
}
// nothing else to do if express server is set
await done.promise;
return;
}
/**
* closes the server
*/
public async stop() {
if (this.httpServer) {
this.httpServer.close();
const done = plugins.smartpromise.defer<void>();
let resolved = false;
if (this.wsServer) {
// Close all WebSocket connections
this.wsServer.clients.forEach((client) => {
client.terminate();
});
this.wsServer.close();
this.wsServer = null;
}
if (this.httpServer && this.standaloneServer) {
const resolveOnce = () => {
if (!resolved) {
resolved = true;
this.httpServer = null;
this.standaloneServer = false;
done.resolve();
}
};
this.httpServer.close(() => {
resolveOnce();
});
// Add a timeout in case close callback doesn't fire
const timeoutId = setTimeout(() => {
resolveOnce();
}, 2000);
// Ensure timeout doesn't keep process alive
if (timeoutId.unref) {
timeoutId.unref();
}
} else {
done.resolve();
}
await done.promise;
}
/**
* Returns WebSocket hooks for integration with smartserve
* Call this to get hooks that you can pass to smartserve's websocket config
*/
public getSmartserveWebSocketHooks(): pluginsTyped.ISmartserveWebSocketHooks {
return {
onOpen: async (peer: pluginsTyped.ISmartserveWebSocketPeer) => {
// Create a wrapper that adapts ISmartserveWebSocketPeer to WebSocket-like interface
const wsLikeSocket = this.createWsLikeFromPeer(peer);
await this.smartsocket.handleNewConnection(wsLikeSocket as any);
// Call external hooks if provided
if (this.externalWebSocketHooks?.onOpen) {
await this.externalWebSocketHooks.onOpen(peer);
}
},
onMessage: async (peer: pluginsTyped.ISmartserveWebSocketPeer, message: pluginsTyped.ISmartserveWebSocketMessage) => {
// Messages are handled by SocketConnection via the adapter
// But we still call external hooks if provided
if (this.externalWebSocketHooks?.onMessage) {
await this.externalWebSocketHooks.onMessage(peer, message);
}
},
onClose: async (peer: pluginsTyped.ISmartserveWebSocketPeer, code: number, reason: string) => {
if (this.externalWebSocketHooks?.onClose) {
await this.externalWebSocketHooks.onClose(peer, code, reason);
}
},
onError: async (peer: pluginsTyped.ISmartserveWebSocketPeer, error: Error) => {
if (this.externalWebSocketHooks?.onError) {
await this.externalWebSocketHooks.onError(peer, error);
}
},
};
}
/**
* Creates a WebSocket-like object from a smartserve peer
* This allows our SocketConnection to work with both native WebSocket and smartserve peers
*/
private createWsLikeFromPeer(peer: pluginsTyped.ISmartserveWebSocketPeer): any {
const messageListeners: Array<(event: any) => void> = [];
const closeListeners: Array<() => void> = [];
const errorListeners: Array<() => void> = [];
// Store the adapter on the peer for message routing
peer.data.set('smartsocket_adapter', {
dispatchMessage: (data: string) => {
messageListeners.forEach((listener) => {
listener({ data });
});
},
dispatchClose: () => {
closeListeners.forEach((listener) => listener());
},
dispatchError: () => {
errorListeners.forEach((listener) => listener());
},
});
return {
readyState: peer.readyState,
send: (data: string) => peer.send(data),
close: (code?: number, reason?: string) => peer.close(code, reason),
addEventListener: (event: string, listener: any) => {
if (event === 'message') {
messageListeners.push(listener);
} else if (event === 'close') {
closeListeners.push(listener);
} else if (event === 'error') {
errorListeners.push(listener);
}
},
removeEventListener: (event: string, listener: any) => {
if (event === 'message') {
const idx = messageListeners.indexOf(listener);
if (idx >= 0) messageListeners.splice(idx, 1);
} else if (event === 'close') {
const idx = closeListeners.indexOf(listener);
if (idx >= 0) closeListeners.splice(idx, 1);
} else if (event === 'error') {
const idx = errorListeners.indexOf(listener);
if (idx >= 0) errorListeners.splice(idx, 1);
}
},
};
}
}