BREAKING CHANGE(smartsocket): Replace setExternalServer with hooks-based SmartServe integration and refactor SocketServer to support standalone and hooks modes
This commit is contained in:
@@ -7,7 +7,7 @@ import { logger } from './smartsocket.logging.js';
|
||||
|
||||
/**
|
||||
* class SocketServer
|
||||
* handles the WebSocket server, either standalone or integrated with smartserve
|
||||
* handles the WebSocket server in standalone mode, or provides hooks for smartserve integration
|
||||
*/
|
||||
export class SocketServer {
|
||||
private smartsocket: Smartsocket;
|
||||
@@ -15,14 +15,7 @@ export class SocketServer {
|
||||
private wsServer: pluginsTyped.ws.WebSocketServer;
|
||||
|
||||
/**
|
||||
* whether we're using an external server (smartserve)
|
||||
*/
|
||||
private externalServerMode = false;
|
||||
private externalServer: any = null;
|
||||
private externalWebSocketHooks: pluginsTyped.ISmartserveWebSocketHooks = null;
|
||||
|
||||
/**
|
||||
* whether httpServer is standalone
|
||||
* whether httpServer is standalone (created by us)
|
||||
*/
|
||||
private standaloneServer = false;
|
||||
|
||||
@@ -31,66 +24,38 @@ export class SocketServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an external server (smartserve) for WebSocket handling
|
||||
*/
|
||||
public async setExternalServer(
|
||||
serverType: 'smartserve',
|
||||
serverArg: any,
|
||||
websocketHooks?: pluginsTyped.ISmartserveWebSocketHooks
|
||||
) {
|
||||
if (serverType !== 'smartserve') {
|
||||
throw new Error(`Unsupported server type: ${serverType}. Only 'smartserve' is supported.`);
|
||||
}
|
||||
this.externalServerMode = true;
|
||||
this.externalServer = serverArg;
|
||||
this.externalWebSocketHooks = websocketHooks || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* starts listening to incoming websocket connections
|
||||
* Starts listening to incoming websocket connections (standalone mode).
|
||||
* If no port is specified, this is a no-op (hooks mode via smartserve).
|
||||
*/
|
||||
public async start() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
|
||||
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 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 port ${this.smartsocket.options.port}`
|
||||
);
|
||||
done.resolve();
|
||||
});
|
||||
// If no port specified, we're in hooks mode - nothing to start
|
||||
if (!this.smartsocket.options.port) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Standalone mode - create our own HTTP server and WebSocket server
|
||||
const done = plugins.smartpromise.defer();
|
||||
const httpModule = await this.smartsocket.smartenv.getSafeNodeModule('http');
|
||||
const wsModule = await this.smartsocket.smartenv.getSafeNodeModule('ws');
|
||||
|
||||
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 port ${this.smartsocket.options.port}`
|
||||
);
|
||||
done.resolve();
|
||||
});
|
||||
|
||||
await done.promise;
|
||||
}
|
||||
|
||||
@@ -141,8 +106,8 @@ export class SocketServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns WebSocket hooks for integration with smartserve
|
||||
* Call this to get hooks that you can pass to smartserve's websocket config
|
||||
* Returns WebSocket hooks for integration with smartserve.
|
||||
* Pass these hooks to SmartServe's websocket config.
|
||||
*/
|
||||
public getSmartserveWebSocketHooks(): pluginsTyped.ISmartserveWebSocketHooks {
|
||||
return {
|
||||
@@ -150,27 +115,35 @@ export class SocketServer {
|
||||
// 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);
|
||||
// Dispatch message to the SocketConnection via the adapter
|
||||
const adapter = peer.data.get('smartsocket_adapter') as any;
|
||||
if (adapter) {
|
||||
let textData: string | undefined;
|
||||
if (message.type === 'text' && message.text) {
|
||||
textData = message.text;
|
||||
} else if (message.type === 'binary' && message.data) {
|
||||
// Convert binary to text (Buffer/Uint8Array to string)
|
||||
textData = new TextDecoder().decode(message.data);
|
||||
}
|
||||
if (textData) {
|
||||
adapter.dispatchMessage(textData);
|
||||
}
|
||||
}
|
||||
},
|
||||
onClose: async (peer: pluginsTyped.ISmartserveWebSocketPeer, code: number, reason: string) => {
|
||||
if (this.externalWebSocketHooks?.onClose) {
|
||||
await this.externalWebSocketHooks.onClose(peer, code, reason);
|
||||
// Dispatch close to the SocketConnection via the adapter
|
||||
const adapter = peer.data.get('smartsocket_adapter') as any;
|
||||
if (adapter) {
|
||||
adapter.dispatchClose();
|
||||
}
|
||||
},
|
||||
onError: async (peer: pluginsTyped.ISmartserveWebSocketPeer, error: Error) => {
|
||||
if (this.externalWebSocketHooks?.onError) {
|
||||
await this.externalWebSocketHooks.onError(peer, error);
|
||||
// Dispatch error to the SocketConnection via the adapter
|
||||
const adapter = peer.data.get('smartsocket_adapter') as any;
|
||||
if (adapter) {
|
||||
adapter.dispatchError();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user