redweb 0.7.1 β 0.7.3
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/README.md +1 -2
- package/index.d.ts +15 -14
- package/package.json +2 -2
- package/src/ws/BaseSocketServer.js +15 -1
package/README.md
CHANGED
|
@@ -217,7 +217,6 @@ Useful for managing players, NPCs, chat members, rooms, etc.
|
|
|
217
217
|
### π§ Basic Usage
|
|
218
218
|
|
|
219
219
|
```js
|
|
220
|
-
const { SocketRegistry } = require('redweb');
|
|
221
220
|
|
|
222
221
|
class Player {
|
|
223
222
|
constructor(socket, id) {
|
|
@@ -351,4 +350,4 @@ Hereβs the updated `CHANGELOG.md` entry for **RedWeb v0.7.1**, written profess
|
|
|
351
350
|
|
|
352
351
|
## πͺͺ License
|
|
353
352
|
|
|
354
|
-
MIT
|
|
353
|
+
MIT
|
package/index.d.ts
CHANGED
|
@@ -113,26 +113,27 @@ declare module 'redweb' {
|
|
|
113
113
|
};
|
|
114
114
|
|
|
115
115
|
/** Generic eventβdriven registry for socket objects */
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
onCreate?: (client: T) => boolean | void;
|
|
120
|
-
onRemove?: (client: T) => boolean | void;
|
|
116
|
+
/** Generic event-driven registry for socket objects */
|
|
117
|
+
export class SocketRegistry<T extends SocketWrapper = SocketWrapper> extends EventEmitter {
|
|
118
|
+
protected items: T[];
|
|
121
119
|
|
|
122
120
|
constructor();
|
|
123
121
|
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
/** Adds a socket-bound object to the registry */
|
|
123
|
+
add(item: T): void;
|
|
126
124
|
|
|
127
|
-
|
|
128
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Removes a socket-bound object by reference or id (default key: 'id')
|
|
127
|
+
* @param itemOrId Object or ID string
|
|
128
|
+
* @param by Key name to match against (default is 'id')
|
|
129
|
+
*/
|
|
130
|
+
remove(itemOrId: T | string, by?: keyof T): boolean;
|
|
129
131
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
allWithout(socket: WebSocket): T[];
|
|
132
|
+
/** Returns a shallow copy of all registered items */
|
|
133
|
+
all(): T[];
|
|
133
134
|
|
|
134
|
-
|
|
135
|
-
|
|
135
|
+
/** Returns the number of registered items */
|
|
136
|
+
count(): number;
|
|
136
137
|
}
|
|
137
138
|
|
|
138
139
|
/** βββββββββββββββββββ CONCRETE SERVERS βββββββββββββββββββ */
|
package/package.json
CHANGED
|
@@ -34,7 +34,21 @@ class BaseSocketServer {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
handleUpgrade(req, sock, head) {
|
|
37
|
-
|
|
37
|
+
// Some websocket clients (e.g., certain UE plugins) are finicky about the
|
|
38
|
+
// HTTP upgrade path they send. Normalise the path and fall back to a default
|
|
39
|
+
// route so we can still complete the upgrade instead of tearing the socket down.
|
|
40
|
+
const path = (() => {
|
|
41
|
+
try {
|
|
42
|
+
return new URL(req.url, `http://${req.headers.host || 'localhost'}`).pathname;
|
|
43
|
+
} catch {
|
|
44
|
+
return req.url;
|
|
45
|
+
}
|
|
46
|
+
})();
|
|
47
|
+
|
|
48
|
+
const route =
|
|
49
|
+
this.routes.find(r => r.path === path) ||
|
|
50
|
+
this.routes.find(r => r.path === '/');
|
|
51
|
+
|
|
38
52
|
if (!route) return sock.destroy();
|
|
39
53
|
|
|
40
54
|
route.server.handleUpgrade(req, sock, head, (s, r) =>
|