Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
2b82061
feat: enhance WebSocket dispatcher to include connection ID and impro…
AxiosLeo Apr 6, 2026
5e5d6a0
feat: add connection ID to WebSocketContext and implement methods for…
AxiosLeo Apr 6, 2026
7aca920
feat: add debug logging to WebSocket server for message broadcasting
AxiosLeo Apr 6, 2026
685bf1d
fix: update TypeScript definitions for send method to require WebSock…
AxiosLeo Apr 6, 2026
39bcd68
chore: update ECMAScript version in ESLint configuration from 2018 to…
AxiosLeo Apr 6, 2026
02b64a9
chore: update Node.js version requirements in package.json and CI wor…
AxiosLeo Apr 6, 2026
1703efc
feat: extend socket dispatcher to include connection ID in context fo…
AxiosLeo Apr 6, 2026
8300666
refactor: replace optional chaining with direct property access for a…
AxiosLeo Apr 6, 2026
0442187
feat: add application instance to KoaContext, SocketContext, and WebS…
AxiosLeo Apr 6, 2026
7090bf9
fix: ensure sendByConnectionId method checks for valid connection bef…
AxiosLeo Apr 6, 2026
90181cf
chore: lower Node.js version requirement to 16 in package.json and CI…
AxiosLeo Apr 6, 2026
63f59e9
feat: implement methods for sending, closing, and managing connection…
AxiosLeo Apr 6, 2026
b5097e1
feat: add ping configuration to SocketApplication and WebSocketApplic…
AxiosLeo Apr 7, 2026
1d64ee9
refactor: enhance SocketApplication and WebSocketApplication by addin…
AxiosLeo Apr 10, 2026
0cf61a8
docs: update project structure and add detailed documentation for Soc…
AxiosLeo Apr 10, 2026
e29063a
test: add comprehensive unit tests for Application, Controller, core …
AxiosLeo Apr 10, 2026
b47da55
refactor: update websocketOptions in WebSocketApplication to remove u…
AxiosLeo Apr 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default [...compat.extends("eslint:recommended"), {
beforeEach: true,
},

ecmaVersion: 2018,
ecmaVersion: 2020,
sourceType: "commonjs",

parserOptions: {
Expand Down
7 changes: 7 additions & 0 deletions examples/websocket.server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { debug } = require('@axiosleo/cli-tool');
const { WebSocketApplication } = require('../src/apps');
const root = require('./api.router');

Expand All @@ -11,4 +12,10 @@ const app = new WebSocketApplication({
}
});

setInterval(() => {
debug.log('send message');
const res = app.broadcast('Hello, world!', 'ok', 0, null);
debug.log('send message result:', res);
}, 1000);

app.start();
25 changes: 25 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ export interface WebSocketContext<
TBody = any,
TQuery = any,
> extends AppContext<TParams, TBody, TQuery> {
/** Connection ID */
connection_id: string;
/** Route parameters */
params?: TParams;
/** Application configuration */
Expand Down Expand Up @@ -1349,6 +1351,29 @@ export declare class WebSocketApplication extends Application {
code?: number,
connections?: WebSocket[],
): void;

/**
* Send data to a specific connection
* @param connection Connection to send to
* @param data Data to send
* @param msg Message
* @param code Status code
*/
send(connection: WebSocket, data?: any, msg?: string, code?: number): boolean;

/**
* Send data to a specific connection by connection ID
* @param connection_id Connection ID to send to
* @param data Data to send
* @param msg Message
* @param code Status code
*/
sendByConnectionId(
connection_id: string,
data?: any,
msg?: string,
code?: number,
): boolean;
}

// ========================================
Expand Down
31 changes: 29 additions & 2 deletions src/apps/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ const { _sleep } = require('@axiosleo/cli-tool/src/helper/cmd');
* @param {{request: import('http').IncomingMessage}} param0
* @returns
*/
const dispatcher = ({ app, app_id, workflow, connection, request }) => {
const dispatcher = ({ app, app_id, workflow, connection_id, connection, request }) => {
return async (ctx) => {
const url = new URL(request.url, `ws://localhost:${app.port}`);
const host = request.headers.host || `localhost:${app.port}`;
const protocol = request.headers['x-forwarded-proto'] === 'https' ? 'wss' : 'ws';
const url = new URL(request.url, `${protocol}://${host}`);
let context = initContext({
app,
method: request.method ? request.method.toUpperCase() : 'GET',
pathinfo: url.pathname,
app_id,
});
context.socket = connection;
context.connection_id = connection_id;
context.query = Object.fromEntries(url.searchParams);
context.body = ctx || {};
context.headers = request.headers;
Expand Down Expand Up @@ -116,6 +119,7 @@ class WebSocketApplication extends Application {
app_id: self.app_id,
workflow: self.workflow,
connection: ws,
connection_id,
request
});
process.nextTick(callback, context);
Expand Down Expand Up @@ -168,6 +172,29 @@ class WebSocketApplication extends Application {
Object.keys(connections).map((id) => connections[id].send(data));
}
}

send(connection = null, data = '', msg = 'ok', code = 0) {
data = JSON.stringify({
request_id: _uuid_salt(this.app_id),
timestamp: (new Date()).getTime(),
code,
message: msg,
data: data
});
if (connection) {
connection.send(data);
return true;
}
return false;
}

sendByConnectionId(connection_id = null, data = '', msg = 'ok', code = 0) {
if (connection_id) {
this.send(this.connections[connection_id], data, msg, code);
return true;
}
return false;
}
Comment thread
cursor[bot] marked this conversation as resolved.
}

module.exports = WebSocketApplication;
Loading