redweb 0.6.1 → 0.6.2

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/index.d.ts CHANGED
@@ -90,10 +90,11 @@ declare module 'redweb' {
90
90
  constructor(config: HandlerConfig);
91
91
 
92
92
  /**
93
- * Adds a new WebSocket connection and sets up message handling for this handler.
93
+ * Adds a new WebSocket connection, processes initial data, and sets up message handling for this handler.
94
94
  * @param socket - The WebSocket connection to add.
95
+ * @param data - Optional initial data sent during the connection handshake.
95
96
  */
96
- newConnection(socket: WebSocket): void;
97
+ newConnection(socket: WebSocket, data?: any): void;
97
98
 
98
99
  /**
99
100
  * Handles an incoming message and routes it to the appropriate handler function.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redweb",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "A way to quickly set up an express server",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,24 +28,34 @@ class BaseHandler {
28
28
  this.connections = [];
29
29
  }
30
30
 
31
- /**
32
- * Adds a new WebSocket connection and sets up message handling for this handler.
33
- * @param {WebSocket} socket - The WebSocket connection to add.
34
- */
35
- newConnection(socket) {
36
- this.connections.push(socket);
37
-
31
+ handleSocketMessages(socket) {
38
32
  socket.on('message', (message) => this.handleMessage(socket, message));
33
+ }
39
34
 
35
+ handleSocketClose(socket) {
40
36
  socket.on('close', () => {
41
37
  this.connections = this.connections.filter((conn) => conn !== socket);
42
38
  console.log(`Connection closed for handler "${this.name}".`);
43
39
  });
40
+ }
44
41
 
42
+ handleNewConnection(socket) {
43
+ this.connections.push(socket);
44
+ this.handleSocketMessages(socket);
45
+ this.handleSocketClose(socket);
45
46
  socket.isAssigned = true;
46
47
  console.log(`New connection added to handler "${this.name}".`);
47
48
  }
48
49
 
50
+ /**
51
+ * Adds a new WebSocket connection and sets up message handling for this handler.
52
+ * @param {WebSocket} socket - The WebSocket connection to add.
53
+ */
54
+ newConnection(socket, data) {
55
+ console.log(`Received newConnection data ${data}`);
56
+ this.handleNewConnection(socket);
57
+ }
58
+
49
59
  /**
50
60
  * Handles an incoming message and routes it to the appropriate handler function.
51
61
  * @param {WebSocket} socket - The WebSocket connection that sent the message.