You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Same as the above routes API, but you can only declare them when the server is `listening`
225
229
@@ -241,10 +245,6 @@ app.runtimeRoute
241
245
});
242
246
```
243
247
244
-
### `BareServer.ws` (WebSocketServer)
245
-
246
-
Refer to [external WebSocketServer](https://github.com/websockets/ws#external-https-server) for documentation.
247
-
248
248
#### `RouteOptions` (Object)
249
249
250
250
If set, provide per-route options for behavior handling
@@ -261,6 +261,54 @@ If set, provides a granular cache headers handling per route.
261
261
262
262
Request timeout value in `ms`. This will cancel the request _only_ for this route if time expired
263
263
264
+
---
265
+
266
+
## `BareServer.ws?` (WebSocketServer)
267
+
268
+
Based on `ws` package, for internals please refer to [external WebSocketServer](https://github.com/websockets/ws#external-https-server) for documentation.
269
+
270
+
This particular implementation works out easily for WebSockets interaction for pushing data to server from the clients and waiting for some answer in async.
271
+
272
+
Also exposes an way to keep pushing messages to the Client from the Server on server handle through internal clients list. (WIP optimizing this)
273
+
274
+
### `WebSocketServer.declareReceiver` ((Data, UserClient, WSClient, MessageEvent) => Promise\<M> | M)
275
+
276
+
This is the main 'handler' function for any kind of Client request. If there's a response to that push from the client the return should contain it, otherwise if the response is `void` there will be no answer to the client side.
277
+
278
+
- `Data`: is the data received from the client for this exact `Type`
279
+
- `UserClient`: is an optional client defined on the stage of `Upgrade` to provide some closured client data to be able to know what Client is exactly making the request to the Server
280
+
- `WSClient`: raw instance of `ws.Client& { userClient: UC }`
281
+
- `MessageEvent`: raw instance of `ws.MessageClient`
282
+
283
+
Code Example:
284
+
285
+
```ts
286
+
app.ws?.declareReceiver<{ ok:string }>({
287
+
type: 'BASE_TYPE',
288
+
handler: async (data, client) => {
289
+
// do your async or sync operations here
290
+
// return the response if you need to send an answer
291
+
return { cool: 'some answer', client };
292
+
},
293
+
});
294
+
```
295
+
296
+
### `WebSocketServer.defineUpgrade` ((IncomingRequest) => Promise\<M> | M)
297
+
298
+
To de able to handle authorization or any other previous operation before opening and upgrading an incoming client's request.
299
+
**If this function is not initialized with the callback, all incoming connections will be accepted by default**
300
+
301
+
```ts
302
+
app.ws?.defineUpgrade(async (req) => {
303
+
// you can do some async or sync operation here
304
+
// the returning of this function will be
305
+
// defined as the `UserClient` and attached to the `ws.Client` instance
0 commit comments