Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ function fastifyWebsocket (fastify, opts, next) {

fastify.addHook('onClose', close)

// Fastify is missing a pre-close event, or the ability to
// add a hook before the server.close call. We need to resort
// to monkeypatching for now.
const oldClose = fastify.server.close
fastify.server.close = function (cb) {
const server = fastify.websocketServer
for (const client of server.clients) {
client.close()
}
oldClose.call(this, cb)
}

function handleRouting (connection, request) {
const response = new ServerResponse(request)
request[kWs] = WebSocket.createWebSocketStream(connection)
Expand All @@ -72,7 +84,8 @@ function fastifyWebsocket (fastify, opts, next) {
}

function close (fastify, done) {
fastify.websocketServer.close(done)
const server = fastify.websocketServer
server.close(done)
}

module.exports = fp(fastifyWebsocket, {
Expand Down
43 changes: 43 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,46 @@ test('Should throw on an invalid handle parameter', (t) => {
t.equal(err.message, 'invalid handle function')
})
})

test('Should gracefully close with a connected client', (t) => {
t.plan(6)

const fastify = Fastify()

fastify.register(fastifyWebsocket, { handle })

function handle (connection) {
connection.setEncoding('utf8')
connection.write('hello client')

connection.once('data', (chunk) => {
t.equal(chunk, 'hello server')
})

connection.on('end', () => {
t.pass('end emitted on server side')
})
// this connection stays alive untile we close the server
}

fastify.listen(0, (err) => {
t.error(err)

const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })

client.setEncoding('utf8')
client.write('hello server')

client.on('end', () => {
t.pass('end emitted on client side')
})

client.once('data', (chunk) => {
t.equal(chunk, 'hello client')
fastify.close(function (err) {
t.error(err)
})
})
})
})