-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket: add websocketinit (#2088)
* websocket: add websocketinit Refs: #1811 (comment) * update types * remove 3rd param it's not as backwards compatible as I thought... * update docs
- Loading branch information
Showing
5 changed files
with
113 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket, Dispatcher, Agent } = require('../..') | ||
|
||
test('WebSocketInit', (t) => { | ||
t.plan(2) | ||
|
||
class WsDispatcher extends Dispatcher { | ||
constructor () { | ||
super() | ||
this.agent = new Agent() | ||
} | ||
|
||
dispatch () { | ||
t.pass() | ||
return this.agent.dispatch(...arguments) | ||
} | ||
} | ||
|
||
t.test('WebSocketInit as 2nd param', (t) => { | ||
t.plan(1) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.send(Buffer.from('hello, world')) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`, { | ||
dispatcher: new WsDispatcher() | ||
}) | ||
|
||
ws.onerror = t.fail | ||
|
||
ws.addEventListener('message', async (event) => { | ||
t.equal(await event.data.text(), 'hello, world') | ||
server.close() | ||
ws.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