@wxn0brp/gloves-link-server 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +51 -4
- package/dist/index.js +56 -9
- package/dist/namespace.d.ts +33 -0
- package/dist/namespace.js +33 -0
- package/dist/room.d.ts +62 -0
- package/dist/room.js +62 -0
- package/dist/socket.d.ts +65 -0
- package/dist/socket.js +70 -0
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { WebSocketServer } from "ws";
|
|
2
|
-
import { Server_Opts } from "./types.js";
|
|
3
|
-
import { GLSocket } from "./socket.js";
|
|
4
1
|
import FalconFrame, { Router } from "@wxn0brp/falcon-frame";
|
|
5
|
-
import
|
|
2
|
+
import http from "http";
|
|
3
|
+
import { WebSocketServer } from "ws";
|
|
6
4
|
import { Namespace } from "./namespace.js";
|
|
5
|
+
import { Room, Rooms } from "./room.js";
|
|
6
|
+
import { GLSocket } from "./socket.js";
|
|
7
|
+
import { Server_Opts } from "./types.js";
|
|
8
|
+
/**
|
|
9
|
+
* GlovesLinkServer class provides a WebSocket server with namespace and room functionality
|
|
10
|
+
*/
|
|
7
11
|
export declare class GlovesLinkServer {
|
|
8
12
|
wss: WebSocketServer;
|
|
9
13
|
logs: boolean;
|
|
@@ -14,13 +18,56 @@ export declare class GlovesLinkServer {
|
|
|
14
18
|
}>;
|
|
15
19
|
rooms: Rooms;
|
|
16
20
|
namespaces: Map<string, Namespace>;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new GlovesLinkServer instance
|
|
23
|
+
* @param opts - Server options including the HTTP server instance
|
|
24
|
+
*/
|
|
17
25
|
constructor(opts: Partial<Server_Opts>);
|
|
26
|
+
createServer(server: http.Server): void;
|
|
27
|
+
/**
|
|
28
|
+
* Saves the status of a socket connection for temporary tracking
|
|
29
|
+
* @param socketSelfId - The ID of the socket
|
|
30
|
+
* @param namespace - The namespace of the socket
|
|
31
|
+
* @param status - The status code to save
|
|
32
|
+
* @param msg - Optional message to save with the status
|
|
33
|
+
* @private
|
|
34
|
+
*/
|
|
18
35
|
private saveSocketStatus;
|
|
36
|
+
/**
|
|
37
|
+
* Gets or creates a namespace by path
|
|
38
|
+
* @param path - The path for the namespace
|
|
39
|
+
* @returns The namespace instance
|
|
40
|
+
*/
|
|
19
41
|
of(path: string): Namespace;
|
|
42
|
+
/**
|
|
43
|
+
* Broadcasts an event to all sockets in a room
|
|
44
|
+
* @param roomName - The name of the room to broadcast to
|
|
45
|
+
* @param event - The event name to broadcast
|
|
46
|
+
* @param args - Arguments to send with the event
|
|
47
|
+
*/
|
|
20
48
|
broadcastRoom(roomName: string, event: string, ...args: any[]): void;
|
|
49
|
+
/**
|
|
50
|
+
* Gets or creates a room by name
|
|
51
|
+
* @param name - The name of the room
|
|
52
|
+
* @returns The room instance
|
|
53
|
+
*/
|
|
21
54
|
room(name: string): Room;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a router for handling status requests
|
|
57
|
+
* @returns A router instance for status endpoints
|
|
58
|
+
*/
|
|
22
59
|
statusRouter(): Router;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a router for serving client files
|
|
62
|
+
* @param clientDir - Optional directory path for client files, defaults to node_modules/@wxn0brp/gloves-link-client/dist/
|
|
63
|
+
* @returns A router instance for client file serving
|
|
64
|
+
*/
|
|
23
65
|
clientRouter(clientDir?: string): Router;
|
|
66
|
+
/**
|
|
67
|
+
* Integrates the GlovesLink server with a FalconFrame application
|
|
68
|
+
* @param app - The FalconFrame application instance
|
|
69
|
+
* @param clientDir - Optional directory path for client files, or false to disable client serving
|
|
70
|
+
*/
|
|
24
71
|
falconFrame(app: FalconFrame, clientDir?: string | false): void;
|
|
25
72
|
}
|
|
26
73
|
export { GLSocket, Namespace, Server_Opts };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { WebSocketServer } from "ws";
|
|
2
|
-
import { GLSocket } from "./socket.js";
|
|
3
1
|
import { Router } from "@wxn0brp/falcon-frame";
|
|
4
|
-
import {
|
|
2
|
+
import { WebSocketServer } from "ws";
|
|
5
3
|
import { Namespace } from "./namespace.js";
|
|
4
|
+
import { Room } from "./room.js";
|
|
5
|
+
import { GLSocket } from "./socket.js";
|
|
6
|
+
/**
|
|
7
|
+
* GlovesLinkServer class provides a WebSocket server with namespace and room functionality
|
|
8
|
+
*/
|
|
6
9
|
export class GlovesLinkServer {
|
|
7
10
|
wss;
|
|
8
11
|
logs = false;
|
|
@@ -10,17 +13,18 @@ export class GlovesLinkServer {
|
|
|
10
13
|
initStatusTemp = {};
|
|
11
14
|
rooms = new Map();
|
|
12
15
|
namespaces = new Map();
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new GlovesLinkServer instance
|
|
18
|
+
* @param opts - Server options including the HTTP server instance
|
|
19
|
+
*/
|
|
13
20
|
constructor(opts) {
|
|
14
21
|
this.opts = {
|
|
15
|
-
server: null,
|
|
16
22
|
logs: false,
|
|
17
23
|
...opts
|
|
18
24
|
};
|
|
19
|
-
if (!this.opts?.server) {
|
|
20
|
-
throw new Error("Server is not provided");
|
|
21
|
-
}
|
|
22
|
-
const { server } = opts;
|
|
23
25
|
this.wss = new WebSocketServer({ noServer: true });
|
|
26
|
+
}
|
|
27
|
+
createServer(server) {
|
|
24
28
|
server.on("upgrade", async (request, socket, head) => {
|
|
25
29
|
const headers = request.headers;
|
|
26
30
|
let socketSelfId;
|
|
@@ -36,7 +40,12 @@ export class GlovesLinkServer {
|
|
|
36
40
|
socket.destroy();
|
|
37
41
|
return;
|
|
38
42
|
}
|
|
39
|
-
const
|
|
43
|
+
const data = url.searchParams.has("data") ? JSON.parse(url.searchParams.get("data")) : {};
|
|
44
|
+
const authResult = await namespace.authFn({
|
|
45
|
+
token, data,
|
|
46
|
+
url, headers,
|
|
47
|
+
request, socket, head,
|
|
48
|
+
});
|
|
40
49
|
if (!authResult || authResult.status !== 200) {
|
|
41
50
|
this.saveSocketStatus(socketSelfId, pathname, authResult?.status || 401, authResult?.msg || "Unauthorized");
|
|
42
51
|
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
|
@@ -68,6 +77,14 @@ export class GlovesLinkServer {
|
|
|
68
77
|
}
|
|
69
78
|
});
|
|
70
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Saves the status of a socket connection for temporary tracking
|
|
82
|
+
* @param socketSelfId - The ID of the socket
|
|
83
|
+
* @param namespace - The namespace of the socket
|
|
84
|
+
* @param status - The status code to save
|
|
85
|
+
* @param msg - Optional message to save with the status
|
|
86
|
+
* @private
|
|
87
|
+
*/
|
|
71
88
|
saveSocketStatus(socketSelfId, namespace, status, msg) {
|
|
72
89
|
if (!socketSelfId)
|
|
73
90
|
return;
|
|
@@ -80,6 +97,11 @@ export class GlovesLinkServer {
|
|
|
80
97
|
delete this.initStatusTemp[id];
|
|
81
98
|
}, 10_000);
|
|
82
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Gets or creates a namespace by path
|
|
102
|
+
* @param path - The path for the namespace
|
|
103
|
+
* @returns The namespace instance
|
|
104
|
+
*/
|
|
83
105
|
of(path) {
|
|
84
106
|
let namespace = this.namespaces.get(path);
|
|
85
107
|
if (!namespace) {
|
|
@@ -88,15 +110,30 @@ export class GlovesLinkServer {
|
|
|
88
110
|
}
|
|
89
111
|
return namespace;
|
|
90
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Broadcasts an event to all sockets in a room
|
|
115
|
+
* @param roomName - The name of the room to broadcast to
|
|
116
|
+
* @param event - The event name to broadcast
|
|
117
|
+
* @param args - Arguments to send with the event
|
|
118
|
+
*/
|
|
91
119
|
broadcastRoom(roomName, event, ...args) {
|
|
92
120
|
const room = this.room(roomName);
|
|
93
121
|
if (!room)
|
|
94
122
|
return;
|
|
95
123
|
room.emit(event, ...args);
|
|
96
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Gets or creates a room by name
|
|
127
|
+
* @param name - The name of the room
|
|
128
|
+
* @returns The room instance
|
|
129
|
+
*/
|
|
97
130
|
room(name) {
|
|
98
131
|
return this.rooms.get(name) || this.rooms.set(name, new Room()).get(name);
|
|
99
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Creates a router for handling status requests
|
|
135
|
+
* @returns A router instance for status endpoints
|
|
136
|
+
*/
|
|
100
137
|
statusRouter() {
|
|
101
138
|
const router = new Router();
|
|
102
139
|
router.get("/status", (req, res) => {
|
|
@@ -120,6 +157,11 @@ export class GlovesLinkServer {
|
|
|
120
157
|
});
|
|
121
158
|
return router;
|
|
122
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Creates a router for serving client files
|
|
162
|
+
* @param clientDir - Optional directory path for client files, defaults to node_modules/@wxn0brp/gloves-link-client/dist/
|
|
163
|
+
* @returns A router instance for client file serving
|
|
164
|
+
*/
|
|
123
165
|
clientRouter(clientDir) {
|
|
124
166
|
const router = new Router();
|
|
125
167
|
clientDir = clientDir || "node_modules/@wxn0brp/gloves-link-client/dist/";
|
|
@@ -130,6 +172,11 @@ export class GlovesLinkServer {
|
|
|
130
172
|
});
|
|
131
173
|
return router;
|
|
132
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Integrates the GlovesLink server with a FalconFrame application
|
|
177
|
+
* @param app - The FalconFrame application instance
|
|
178
|
+
* @param clientDir - Optional directory path for client files, or false to disable client serving
|
|
179
|
+
*/
|
|
133
180
|
falconFrame(app, clientDir) {
|
|
134
181
|
const router = new Router();
|
|
135
182
|
app.use("/gloves-link", router);
|
package/dist/namespace.d.ts
CHANGED
|
@@ -2,16 +2,49 @@ import { AuthFn } from "./types.js";
|
|
|
2
2
|
import { GLSocket } from "./socket.js";
|
|
3
3
|
import { Room } from "./room.js";
|
|
4
4
|
import { GlovesLinkServer } from "./index.js";
|
|
5
|
+
/**
|
|
6
|
+
* Namespace class represents a logical grouping of sockets that can communicate with each other
|
|
7
|
+
*/
|
|
5
8
|
export declare class Namespace {
|
|
6
9
|
name: string;
|
|
7
10
|
private server;
|
|
8
11
|
private onConnectEvent;
|
|
9
12
|
authFn: AuthFn;
|
|
10
13
|
room: Room;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new Namespace instance
|
|
16
|
+
* @param name - The name of the namespace
|
|
17
|
+
* @param server - The GlovesLinkServer instance
|
|
18
|
+
*/
|
|
11
19
|
constructor(name: string, server: GlovesLinkServer);
|
|
20
|
+
/**
|
|
21
|
+
* Sets the connection event handler for this namespace
|
|
22
|
+
* @param handler - The function to be called when a socket connects to this namespace
|
|
23
|
+
* @returns The current Namespace instance for chaining
|
|
24
|
+
*/
|
|
12
25
|
onConnect(handler: (ws: GLSocket) => void): this;
|
|
26
|
+
/**
|
|
27
|
+
* Sets the authentication function for this namespace
|
|
28
|
+
* @param authFn - The authentication function to be used for this namespace
|
|
29
|
+
* @returns The current Namespace instance for chaining
|
|
30
|
+
*/
|
|
13
31
|
auth(authFn: AuthFn): this;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the connection event handler for this namespace
|
|
34
|
+
* @returns The connection event handler function
|
|
35
|
+
*/
|
|
14
36
|
get onConnectHandler(): (ws: GLSocket) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Emits an event to all sockets in the namespace's room
|
|
39
|
+
* @param event - The event name to emit
|
|
40
|
+
* @param args - The arguments to pass with the event
|
|
41
|
+
*/
|
|
15
42
|
emit(event: string, ...args: any[]): void;
|
|
43
|
+
/**
|
|
44
|
+
* Emits an event to all sockets in the namespace's room except the specified socket
|
|
45
|
+
* @param socket - The socket to exclude from the emission
|
|
46
|
+
* @param event - The event name to emit
|
|
47
|
+
* @param args - The arguments to pass with the event
|
|
48
|
+
*/
|
|
16
49
|
emitWithoutSelf(socket: GLSocket, event: string, ...args: any[]): void;
|
|
17
50
|
}
|
package/dist/namespace.js
CHANGED
|
@@ -1,29 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Namespace class represents a logical grouping of sockets that can communicate with each other
|
|
3
|
+
*/
|
|
1
4
|
export class Namespace {
|
|
2
5
|
name;
|
|
3
6
|
server;
|
|
4
7
|
onConnectEvent = () => { };
|
|
5
8
|
authFn = async () => ({ status: 200 });
|
|
6
9
|
room;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new Namespace instance
|
|
12
|
+
* @param name - The name of the namespace
|
|
13
|
+
* @param server - The GlovesLinkServer instance
|
|
14
|
+
*/
|
|
7
15
|
constructor(name, server) {
|
|
8
16
|
this.name = name;
|
|
9
17
|
this.server = server;
|
|
10
18
|
const roomName = `gls-namespace-${name}`;
|
|
11
19
|
this.room = this.server.room(roomName);
|
|
12
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Sets the connection event handler for this namespace
|
|
23
|
+
* @param handler - The function to be called when a socket connects to this namespace
|
|
24
|
+
* @returns The current Namespace instance for chaining
|
|
25
|
+
*/
|
|
13
26
|
onConnect(handler) {
|
|
14
27
|
this.onConnectEvent = handler;
|
|
15
28
|
return this;
|
|
16
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Sets the authentication function for this namespace
|
|
32
|
+
* @param authFn - The authentication function to be used for this namespace
|
|
33
|
+
* @returns The current Namespace instance for chaining
|
|
34
|
+
*/
|
|
17
35
|
auth(authFn) {
|
|
18
36
|
this.authFn = authFn;
|
|
19
37
|
return this;
|
|
20
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Gets the connection event handler for this namespace
|
|
41
|
+
* @returns The connection event handler function
|
|
42
|
+
*/
|
|
21
43
|
get onConnectHandler() {
|
|
22
44
|
return this.onConnectEvent;
|
|
23
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Emits an event to all sockets in the namespace's room
|
|
48
|
+
* @param event - The event name to emit
|
|
49
|
+
* @param args - The arguments to pass with the event
|
|
50
|
+
*/
|
|
24
51
|
emit(event, ...args) {
|
|
25
52
|
this.room.emit(event, ...args);
|
|
26
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Emits an event to all sockets in the namespace's room except the specified socket
|
|
56
|
+
* @param socket - The socket to exclude from the emission
|
|
57
|
+
* @param event - The event name to emit
|
|
58
|
+
* @param args - The arguments to pass with the event
|
|
59
|
+
*/
|
|
27
60
|
emitWithoutSelf(socket, event, ...args) {
|
|
28
61
|
this.room.emitWithoutSelf(socket, event, ...args);
|
|
29
62
|
}
|
package/dist/room.d.ts
CHANGED
|
@@ -1,22 +1,84 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
import { GLSocket } from "./socket.js";
|
|
3
3
|
export type Rooms = Map<string, Room>;
|
|
4
|
+
/**
|
|
5
|
+
* Room class represents a collection of sockets that can communicate with each other
|
|
6
|
+
*/
|
|
4
7
|
export declare class Room {
|
|
5
8
|
clients: Set<GLSocket>;
|
|
6
9
|
eventEmitter: EventEmitter<any>;
|
|
10
|
+
/**
|
|
11
|
+
* Adds a socket to the room
|
|
12
|
+
* @param socket - The socket to add to the room
|
|
13
|
+
*/
|
|
7
14
|
join(socket: GLSocket): void;
|
|
15
|
+
/**
|
|
16
|
+
* Removes a socket from the room
|
|
17
|
+
* @param socket - The socket to remove from the room
|
|
18
|
+
*/
|
|
8
19
|
leave(socket: GLSocket): void;
|
|
20
|
+
/**
|
|
21
|
+
* Removes all sockets from the room
|
|
22
|
+
*/
|
|
9
23
|
leaveAll(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Registers a handler for when a socket joins the room
|
|
26
|
+
* @param handler - The function to be called when a socket joins the room
|
|
27
|
+
* @returns The current Room instance for chaining
|
|
28
|
+
*/
|
|
10
29
|
onJoin(handler: (socket: GLSocket, room: Room) => void): this;
|
|
30
|
+
/**
|
|
31
|
+
* Registers a handler for when a socket leaves the room
|
|
32
|
+
* @param handler - The function to be called when a socket leaves the room
|
|
33
|
+
* @returns The current Room instance for chaining
|
|
34
|
+
*/
|
|
11
35
|
onLeave(handler: (socket: GLSocket, room: Room) => void): this;
|
|
36
|
+
/**
|
|
37
|
+
* Gets the number of clients in the room
|
|
38
|
+
* @returns The number of clients in the room
|
|
39
|
+
*/
|
|
12
40
|
get size(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Gets an array of all clients in the room
|
|
43
|
+
* @returns An array containing all the sockets in the room
|
|
44
|
+
*/
|
|
13
45
|
get sockets(): GLSocket<{
|
|
14
46
|
_id?: string;
|
|
15
47
|
}>[];
|
|
48
|
+
/**
|
|
49
|
+
* Emits an event to all clients in the room
|
|
50
|
+
* @param evtName - The name of the event to emit
|
|
51
|
+
* @param data - The data to send with the event
|
|
52
|
+
*/
|
|
16
53
|
emit(evtName: string, ...data: any): void;
|
|
54
|
+
/**
|
|
55
|
+
* Emits an event to all clients in the room except the specified socket
|
|
56
|
+
* @param socket - The socket to exclude from the emission
|
|
57
|
+
* @param evtName - The name of the event to emit
|
|
58
|
+
* @param data - The data to send with the event
|
|
59
|
+
*/
|
|
17
60
|
emitWithoutSelf(socket: GLSocket, evtName: string, ...data: any): void;
|
|
61
|
+
/**
|
|
62
|
+
* Checks if a socket is in the room
|
|
63
|
+
* @param socket - The socket to check
|
|
64
|
+
* @returns True if the socket is in the room, false otherwise
|
|
65
|
+
*/
|
|
18
66
|
has(socket: GLSocket): boolean;
|
|
19
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Adds a socket to a room by name, creating the room if it doesn't exist
|
|
70
|
+
* @param socket - The socket to add to the room
|
|
71
|
+
* @param name - The name of the room to join
|
|
72
|
+
*/
|
|
20
73
|
export declare function joinSocketToRoom(socket: GLSocket, name: string): void;
|
|
74
|
+
/**
|
|
75
|
+
* Removes a socket from a room by name
|
|
76
|
+
* @param socket - The socket to remove from the room
|
|
77
|
+
* @param roomName - The name of the room to leave
|
|
78
|
+
*/
|
|
21
79
|
export declare function leaveSocketFromRoom(socket: GLSocket, roomName: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* Removes a socket from all rooms it has joined
|
|
82
|
+
* @param socket - The socket to remove from all rooms
|
|
83
|
+
*/
|
|
22
84
|
export declare function leaveAllSocketFromRoom(socket: GLSocket): void;
|
package/dist/room.js
CHANGED
|
@@ -1,38 +1,81 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
|
+
/**
|
|
3
|
+
* Room class represents a collection of sockets that can communicate with each other
|
|
4
|
+
*/
|
|
2
5
|
export class Room {
|
|
3
6
|
clients = new Set();
|
|
4
7
|
eventEmitter = new EventEmitter();
|
|
8
|
+
/**
|
|
9
|
+
* Adds a socket to the room
|
|
10
|
+
* @param socket - The socket to add to the room
|
|
11
|
+
*/
|
|
5
12
|
join(socket) {
|
|
6
13
|
this.clients.add(socket);
|
|
7
14
|
this.eventEmitter.emit("join", socket, this);
|
|
8
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Removes a socket from the room
|
|
18
|
+
* @param socket - The socket to remove from the room
|
|
19
|
+
*/
|
|
9
20
|
leave(socket) {
|
|
10
21
|
this.clients.delete(socket);
|
|
11
22
|
this.eventEmitter.emit("leave", socket, this);
|
|
12
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Removes all sockets from the room
|
|
26
|
+
*/
|
|
13
27
|
leaveAll() {
|
|
14
28
|
this.clients.clear();
|
|
15
29
|
this.eventEmitter.emit("leaveAll", this);
|
|
16
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Registers a handler for when a socket joins the room
|
|
33
|
+
* @param handler - The function to be called when a socket joins the room
|
|
34
|
+
* @returns The current Room instance for chaining
|
|
35
|
+
*/
|
|
17
36
|
onJoin(handler) {
|
|
18
37
|
this.eventEmitter.on("join", handler);
|
|
19
38
|
return this;
|
|
20
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Registers a handler for when a socket leaves the room
|
|
42
|
+
* @param handler - The function to be called when a socket leaves the room
|
|
43
|
+
* @returns The current Room instance for chaining
|
|
44
|
+
*/
|
|
21
45
|
onLeave(handler) {
|
|
22
46
|
this.eventEmitter.on("leave", handler);
|
|
23
47
|
return this;
|
|
24
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Gets the number of clients in the room
|
|
51
|
+
* @returns The number of clients in the room
|
|
52
|
+
*/
|
|
25
53
|
get size() {
|
|
26
54
|
return this.clients.size;
|
|
27
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets an array of all clients in the room
|
|
58
|
+
* @returns An array containing all the sockets in the room
|
|
59
|
+
*/
|
|
28
60
|
get sockets() {
|
|
29
61
|
return Array.from(this.clients);
|
|
30
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Emits an event to all clients in the room
|
|
65
|
+
* @param evtName - The name of the event to emit
|
|
66
|
+
* @param data - The data to send with the event
|
|
67
|
+
*/
|
|
31
68
|
emit(evtName, ...data) {
|
|
32
69
|
for (const socket of this.clients) {
|
|
33
70
|
socket.emit(evtName, ...data);
|
|
34
71
|
}
|
|
35
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Emits an event to all clients in the room except the specified socket
|
|
75
|
+
* @param socket - The socket to exclude from the emission
|
|
76
|
+
* @param evtName - The name of the event to emit
|
|
77
|
+
* @param data - The data to send with the event
|
|
78
|
+
*/
|
|
36
79
|
emitWithoutSelf(socket, evtName, ...data) {
|
|
37
80
|
for (const client of this.clients) {
|
|
38
81
|
if (client === socket)
|
|
@@ -40,15 +83,30 @@ export class Room {
|
|
|
40
83
|
client.emit(evtName, ...data);
|
|
41
84
|
}
|
|
42
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Checks if a socket is in the room
|
|
88
|
+
* @param socket - The socket to check
|
|
89
|
+
* @returns True if the socket is in the room, false otherwise
|
|
90
|
+
*/
|
|
43
91
|
has(socket) {
|
|
44
92
|
return this.clients.has(socket);
|
|
45
93
|
}
|
|
46
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Adds a socket to a room by name, creating the room if it doesn't exist
|
|
97
|
+
* @param socket - The socket to add to the room
|
|
98
|
+
* @param name - The name of the room to join
|
|
99
|
+
*/
|
|
47
100
|
export function joinSocketToRoom(socket, name) {
|
|
48
101
|
const rooms = socket.server.rooms;
|
|
49
102
|
const room = rooms.get(name) || rooms.set(name, new Room()).get(name);
|
|
50
103
|
room.join(socket);
|
|
51
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Removes a socket from a room by name
|
|
107
|
+
* @param socket - The socket to remove from the room
|
|
108
|
+
* @param roomName - The name of the room to leave
|
|
109
|
+
*/
|
|
52
110
|
export function leaveSocketFromRoom(socket, roomName) {
|
|
53
111
|
const rooms = socket.server.rooms;
|
|
54
112
|
const room = rooms.get(roomName);
|
|
@@ -59,6 +117,10 @@ export function leaveSocketFromRoom(socket, roomName) {
|
|
|
59
117
|
return;
|
|
60
118
|
rooms.delete(roomName);
|
|
61
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Removes a socket from all rooms it has joined
|
|
122
|
+
* @param socket - The socket to remove from all rooms
|
|
123
|
+
*/
|
|
62
124
|
export function leaveAllSocketFromRoom(socket) {
|
|
63
125
|
const rooms = socket.server.rooms;
|
|
64
126
|
for (const room of rooms.values())
|
package/dist/socket.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { WebSocket } from "ws";
|
|
2
2
|
import { GlovesLinkServer } from "./index.js";
|
|
3
|
+
import { Room, Rooms } from "./room.js";
|
|
4
|
+
/**
|
|
5
|
+
* GLSocket class represents a WebSocket connection with additional functionality
|
|
6
|
+
* @template T - The type of user data associated with the socket
|
|
7
|
+
*/
|
|
3
8
|
export declare class GLSocket<T = {
|
|
4
9
|
_id?: string;
|
|
5
10
|
}> {
|
|
@@ -15,14 +20,74 @@ export declare class GLSocket<T = {
|
|
|
15
20
|
[key: string]: Function;
|
|
16
21
|
};
|
|
17
22
|
rooms: Set<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new GLSocket instance
|
|
25
|
+
* @param ws - The underlying WebSocket connection
|
|
26
|
+
* @param server - The GlovesLinkServer instance
|
|
27
|
+
* @param id - Optional ID for the socket, will be generated if not provided
|
|
28
|
+
*/
|
|
18
29
|
constructor(ws: WebSocket, server: GlovesLinkServer, id?: string);
|
|
30
|
+
/**
|
|
31
|
+
* Internal method to handle incoming messages from the WebSocket
|
|
32
|
+
* @param raw - The raw message string received from the WebSocket
|
|
33
|
+
*/
|
|
19
34
|
_handle(raw: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Registers an event handler for the specified event
|
|
37
|
+
* @param evt - The event name to listen for
|
|
38
|
+
* @param handler - The function to be called when the event is received
|
|
39
|
+
*/
|
|
20
40
|
on(evt: string, handler: (...args: any[]) => void | any): void;
|
|
41
|
+
/**
|
|
42
|
+
* Sends an event to the connected WebSocket client
|
|
43
|
+
* @param evt - The event name to send
|
|
44
|
+
* @param args - The arguments to pass with the event
|
|
45
|
+
*/
|
|
21
46
|
emit(evt: string, ...args: any[]): void;
|
|
47
|
+
/**
|
|
48
|
+
* Sends an event to the connected WebSocket client (alias for emit)
|
|
49
|
+
* @param evt - The event name to send
|
|
50
|
+
* @param args - The arguments to pass with the event
|
|
51
|
+
* @returns The result of the emit method
|
|
52
|
+
*/
|
|
22
53
|
send(evt: string, ...args: any[]): void;
|
|
54
|
+
/**
|
|
55
|
+
* Closes the WebSocket connection
|
|
56
|
+
*/
|
|
23
57
|
close(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Joins the socket to a room
|
|
60
|
+
* @param roomName - The name of the room to join
|
|
61
|
+
*/
|
|
24
62
|
joinRoom(roomName: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Removes the socket from a room
|
|
65
|
+
* @param roomName - The name of the room to leave
|
|
66
|
+
*/
|
|
25
67
|
leaveRoom(roomName: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Removes the socket from all rooms it has joined
|
|
70
|
+
*/
|
|
26
71
|
leaveAllRooms(): void;
|
|
72
|
+
/**
|
|
73
|
+
* Gets the namespace associated with this socket
|
|
74
|
+
* @returns The namespace object or undefined if not found
|
|
75
|
+
*/
|
|
27
76
|
getNamespace(): import("./namespace.js").Namespace;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the room associated with this socket's namespace
|
|
79
|
+
* @returns The room object or undefined if namespace is not found
|
|
80
|
+
*/
|
|
81
|
+
namespaceRoom(): Room;
|
|
82
|
+
/**
|
|
83
|
+
* Gets a room from the server by name
|
|
84
|
+
* @param roomName - The name of the room to retrieve
|
|
85
|
+
* @returns The room object
|
|
86
|
+
*/
|
|
87
|
+
serverRoom(roomName: string): Room;
|
|
88
|
+
/**
|
|
89
|
+
* Gets all rooms from the server
|
|
90
|
+
* @returns A map of all rooms on the server
|
|
91
|
+
*/
|
|
92
|
+
serverRooms(): Rooms;
|
|
28
93
|
}
|
package/dist/socket.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { joinSocketToRoom, leaveSocketFromRoom } from "./room.js";
|
|
2
|
+
/**
|
|
3
|
+
* GLSocket class represents a WebSocket connection with additional functionality
|
|
4
|
+
* @template T - The type of user data associated with the socket
|
|
5
|
+
*/
|
|
2
6
|
export class GLSocket {
|
|
3
7
|
ws;
|
|
4
8
|
server;
|
|
@@ -10,6 +14,12 @@ export class GLSocket {
|
|
|
10
14
|
logs = false;
|
|
11
15
|
handlers;
|
|
12
16
|
rooms = new Set();
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new GLSocket instance
|
|
19
|
+
* @param ws - The underlying WebSocket connection
|
|
20
|
+
* @param server - The GlovesLinkServer instance
|
|
21
|
+
* @param id - Optional ID for the socket, will be generated if not provided
|
|
22
|
+
*/
|
|
13
23
|
constructor(ws, server, id) {
|
|
14
24
|
this.ws = ws;
|
|
15
25
|
this.server = server;
|
|
@@ -18,6 +28,10 @@ export class GLSocket {
|
|
|
18
28
|
this.handlers = {};
|
|
19
29
|
this.ws.on("message", (raw) => this._handle(raw));
|
|
20
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Internal method to handle incoming messages from the WebSocket
|
|
33
|
+
* @param raw - The raw message string received from the WebSocket
|
|
34
|
+
*/
|
|
21
35
|
_handle(raw) {
|
|
22
36
|
let msg;
|
|
23
37
|
try {
|
|
@@ -53,9 +67,19 @@ export class GLSocket {
|
|
|
53
67
|
}
|
|
54
68
|
this.handlers[evt]?.(...data);
|
|
55
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Registers an event handler for the specified event
|
|
72
|
+
* @param evt - The event name to listen for
|
|
73
|
+
* @param handler - The function to be called when the event is received
|
|
74
|
+
*/
|
|
56
75
|
on(evt, handler) {
|
|
57
76
|
this.handlers[evt] = handler;
|
|
58
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Sends an event to the connected WebSocket client
|
|
80
|
+
* @param evt - The event name to send
|
|
81
|
+
* @param args - The arguments to pass with the event
|
|
82
|
+
*/
|
|
59
83
|
emit(evt, ...args) {
|
|
60
84
|
const ackI = args.map((data, i) => {
|
|
61
85
|
if (typeof data === "function")
|
|
@@ -73,27 +97,73 @@ export class GLSocket {
|
|
|
73
97
|
ackI: ackI.length ? ackI : undefined
|
|
74
98
|
}));
|
|
75
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Sends an event to the connected WebSocket client (alias for emit)
|
|
102
|
+
* @param evt - The event name to send
|
|
103
|
+
* @param args - The arguments to pass with the event
|
|
104
|
+
* @returns The result of the emit method
|
|
105
|
+
*/
|
|
76
106
|
send(evt, ...args) {
|
|
77
107
|
return this.emit(evt, ...args);
|
|
78
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Closes the WebSocket connection
|
|
111
|
+
*/
|
|
79
112
|
close() {
|
|
80
113
|
this.ws.close();
|
|
81
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Joins the socket to a room
|
|
117
|
+
* @param roomName - The name of the room to join
|
|
118
|
+
*/
|
|
82
119
|
joinRoom(roomName) {
|
|
83
120
|
joinSocketToRoom(this, roomName);
|
|
84
121
|
this.rooms.add(roomName);
|
|
85
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Removes the socket from a room
|
|
125
|
+
* @param roomName - The name of the room to leave
|
|
126
|
+
*/
|
|
86
127
|
leaveRoom(roomName) {
|
|
87
128
|
leaveSocketFromRoom(this, roomName);
|
|
88
129
|
this.rooms.delete(roomName);
|
|
89
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Removes the socket from all rooms it has joined
|
|
133
|
+
*/
|
|
90
134
|
leaveAllRooms() {
|
|
91
135
|
for (const roomName of this.rooms) {
|
|
92
136
|
leaveSocketFromRoom(this, roomName);
|
|
93
137
|
}
|
|
94
138
|
this.rooms.clear();
|
|
95
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Gets the namespace associated with this socket
|
|
142
|
+
* @returns The namespace object or undefined if not found
|
|
143
|
+
*/
|
|
96
144
|
getNamespace() {
|
|
97
145
|
return this.server.namespaces.get(this.namespace);
|
|
98
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Gets the room associated with this socket's namespace
|
|
149
|
+
* @returns The room object or undefined if namespace is not found
|
|
150
|
+
*/
|
|
151
|
+
namespaceRoom() {
|
|
152
|
+
return this.getNamespace()?.room;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Gets a room from the server by name
|
|
156
|
+
* @param roomName - The name of the room to retrieve
|
|
157
|
+
* @returns The room object
|
|
158
|
+
*/
|
|
159
|
+
serverRoom(roomName) {
|
|
160
|
+
return this.server.room(roomName);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Gets all rooms from the server
|
|
164
|
+
* @returns A map of all rooms on the server
|
|
165
|
+
*/
|
|
166
|
+
serverRooms() {
|
|
167
|
+
return this.server.rooms;
|
|
168
|
+
}
|
|
99
169
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import http from "http";
|
|
2
2
|
import Stream from "stream";
|
|
3
3
|
export interface Server_Opts {
|
|
4
|
-
server: http.Server;
|
|
5
4
|
logs: boolean;
|
|
6
5
|
}
|
|
7
6
|
export interface Server_DataEvent {
|
|
@@ -20,6 +19,7 @@ export interface Server_Auth_Opts {
|
|
|
20
19
|
request: http.IncomingMessage;
|
|
21
20
|
socket: Stream.Duplex;
|
|
22
21
|
head: Buffer<ArrayBufferLike>;
|
|
22
|
+
data?: Record<string, any>;
|
|
23
23
|
}
|
|
24
24
|
export interface AuthFnResult {
|
|
25
25
|
status: number;
|