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
Add preClose opt + merge close and preClose hook (#261)
* Add preClose option
Also commenting out failing test from before these changes, so preCommit hook passes
* Rm onClose hook and handle that logic in preClose
* Make preClose top level option
* Add preClose opt to readme
* Put noHandle back in its original spot
* Add back test
* Clarify preClose can be an async function
* Add preClose to types
* Reset plan count to original (need to force this through the precommit because it fails locally, but I think it will pass the CI)
* Add type tests for preClose opt
Note: Fastify's `onError` and error handlers registered by `setErrorHandler` will still be called for errors encountered *before* the websocket connection is established. This means errors thrown by `onRequest` hooks, `preValidation` handlers, and hooks registered by plugins will use the normal error handling mechanisms in Fastify. Once the websocket is established and your websocket route handler is called, `fastify-websocket`'s `errorHandler` takes over.
236
+
237
+
### Custom preClose hook:
238
+
239
+
By default, all ws connections are closed when the server closes. If you wish to modify this behaviour, you can pass your own `preClose` function.
240
+
241
+
Note that `preClose` is responsible for closing all connections and closing the websocket server.
242
+
243
+
```js
244
+
constfastify=require('fastify')()
245
+
246
+
fastify.register(require('@fastify/websocket'), {
247
+
preClose: (done) => { // Note: can also use async style, without done-callback
248
+
constserver=this.websocketServer
249
+
250
+
for (constconnectionofserver.clients) {
251
+
connection.close(1001, 'WS server is going offline in custom manner, sending a code + message')
252
+
}
253
+
254
+
server.close(done)
255
+
}
256
+
})
257
+
```
258
+
236
259
## Options
237
260
238
261
`@fastify/websocket` accept these options for [`ws`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback) :
Copy file name to clipboardExpand all lines: index.js
+14-7Lines changed: 14 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,15 @@ function fastifyWebsocket (fastify, opts, next) {
19
19
errorHandler=opts.errorHandler
20
20
}
21
21
22
+
letpreClose=defaultPreClose
23
+
if(opts&&opts.preClose){
24
+
if(typeofopts.preClose!=='function'){
25
+
returnnext(newError('invalid preClose function'))
26
+
}
27
+
28
+
preClose=opts.preClose
29
+
}
30
+
22
31
if(opts.options&&opts.options.noServer){
23
32
returnnext(newError("fastify-websocket doesn't support the ws noServer option. If you want to create a websocket server detatched from fastify, use the ws library directly."))
24
33
}
@@ -143,25 +152,23 @@ function fastifyWebsocket (fastify, opts, next) {
143
152
}
144
153
})
145
154
146
-
fastify.addHook('onClose',close)
147
-
148
155
// Fastify is missing a pre-close event, or the ability to
149
156
// add a hook before the server.close call. We need to resort
0 commit comments