File tree 3 files changed +32
-1
lines changed
3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,8 @@ This class represents a WebSocket server. It extends the `EventEmitter`.
84
84
- ` verifyClient ` {Function} A function which can be used to validate incoming
85
85
connections. See description below. (Usage is discouraged: see
86
86
[ Issue #337 ] ( https://github.com/websockets/ws/issues/377#issuecomment-462152231 ) )
87
+ - ` WebSocket ` {Function} Specifies the ` WebSocket ` class to be used. It must
88
+ be extended from the original ` WebSocket ` . Defaults to ` WebSocket ` .
87
89
- ` callback ` {Function}
88
90
89
91
Create a new server instance. One and only one of ` port ` , ` server ` or ` noServer `
Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ class WebSocketServer extends EventEmitter {
49
49
* @param {Boolean } [options.skipUTF8Validation=false] Specifies whether or
50
50
* not to skip UTF-8 validation for text and close messages
51
51
* @param {Function } [options.verifyClient] A hook to reject connections
52
+ * @param {Function } [options.WebSocket=WebSocket] Specifies the `WebSocket`
53
+ * class to use. It must be the `WebSocket` class or class that extends it
52
54
* @param {Function } [callback] A listener for the `listening` event
53
55
*/
54
56
constructor ( options , callback ) {
@@ -67,6 +69,7 @@ class WebSocketServer extends EventEmitter {
67
69
host : null ,
68
70
path : null ,
69
71
port : null ,
72
+ WebSocket,
70
73
...options
71
74
} ;
72
75
@@ -356,7 +359,7 @@ class WebSocketServer extends EventEmitter {
356
359
`Sec-WebSocket-Accept: ${ digest } `
357
360
] ;
358
361
359
- const ws = new WebSocket ( null ) ;
362
+ const ws = new this . options . WebSocket ( null ) ;
360
363
361
364
if ( protocols . size ) {
362
365
//
Original file line number Diff line number Diff line change @@ -89,6 +89,32 @@ describe('WebSocketServer', () => {
89
89
wss . close ( done ) ;
90
90
} ) ;
91
91
} ) ;
92
+
93
+ it ( 'honors the `WebSocket` option' , ( done ) => {
94
+ class CustomWebSocket extends WebSocket . WebSocket {
95
+ get foo ( ) {
96
+ return 'foo' ;
97
+ }
98
+ }
99
+
100
+ const wss = new WebSocket . Server (
101
+ {
102
+ port : 0 ,
103
+ WebSocket : CustomWebSocket
104
+ } ,
105
+ ( ) => {
106
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
107
+
108
+ ws . on ( 'open' , ws . close ) ;
109
+ }
110
+ ) ;
111
+
112
+ wss . on ( 'connection' , ( ws ) => {
113
+ assert . ok ( ws instanceof CustomWebSocket ) ;
114
+ assert . strictEqual ( ws . foo , 'foo' ) ;
115
+ wss . close ( done ) ;
116
+ } ) ;
117
+ } ) ;
92
118
} ) ;
93
119
94
120
it ( 'emits an error if http server bind fails' , ( done ) => {
You can’t perform that action at this time.
0 commit comments