-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove fastify-websocket, proxy ws events directly (#122)
* Drop fastify-websocket * Add socket.io tests * Disconnect websockets when server is closing * Remove rejectUnauthorized default value * Add plugin metadata * Actually test socket.io proxy * Fix ws server closing * Test ws clients close event * Add missing tearDown
- Loading branch information
Showing
7 changed files
with
179 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const Fastify = require('fastify') | ||
const proxy = require('../') | ||
const ioServer = require('socket.io') | ||
const ioClient = require('socket.io-client') | ||
const { createServer } = require('http') | ||
const { promisify } = require('util') | ||
const { once } = require('events') | ||
|
||
test('proxy socket.io', async t => { | ||
t.plan(2) | ||
|
||
const srvUpstream = createServer() | ||
t.tearDown(srvUpstream.close.bind(srvUpstream)) | ||
|
||
const srvSocket = new ioServer.Server(srvUpstream) | ||
t.tearDown(srvSocket.close.bind(srvSocket)) | ||
|
||
await promisify(srvUpstream.listen.bind(srvUpstream))(0) | ||
|
||
const srvProxy = Fastify() | ||
t.tearDown(srvProxy.close.bind(srvProxy)) | ||
|
||
srvProxy.register(proxy, { | ||
upstream: `http://127.0.0.1:${srvUpstream.address().port}`, | ||
websocket: true | ||
}) | ||
|
||
await srvProxy.listen(0) | ||
|
||
srvSocket.on('connection', socket => { | ||
socket.on('hello', data => { | ||
t.is(data, 'world') | ||
socket.emit('hi', 'socket') | ||
}) | ||
}) | ||
|
||
const cliSocket = ioClient(`http://127.0.0.1:${srvProxy.server.address().port}`) | ||
t.tearDown(cliSocket.close.bind(cliSocket)) | ||
|
||
cliSocket.emit('hello', 'world') | ||
|
||
const out = await once(cliSocket, 'hi') | ||
t.is(out[0], 'socket') | ||
|
||
await Promise.all([ | ||
once(cliSocket, 'disconnect'), | ||
srvProxy.close() | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters