11import URL from 'url-parse' ;
22import WebSocket from './websocket' ;
3+ import dedupe from './helpers/dedupe' ;
34import EventTarget from './event/target' ;
4- import networkBridge from './network-bridge' ;
55import { CLOSE_CODES } from './constants' ;
6+ import networkBridge from './network-bridge' ;
67import globalObject from './helpers/global-object' ;
7- import dedupe from './helpers/dedupe' ;
88import normalizeSendData from './helpers/normalize-send' ;
99import { createEvent , createMessageEvent , createCloseEvent } from './event/factory' ;
1010
11- /*
12- * https://github.com/websockets/ws#server-example
13- */
1411class Server extends EventTarget {
15- /*
16- * @param {string } url
17- */
1812 constructor ( url , options = { } ) {
1913 super ( ) ;
2014 const urlRecord = new URL ( url ) ;
@@ -42,13 +36,12 @@ class Server extends EventTarget {
4236 }
4337
4438 this . options = options ;
45-
4639 this . start ( ) ;
4740 }
4841
4942 /*
50- * Attaches the mock websocket object to the global object
51- */
43+ * Attaches the mock websocket object to the global object
44+ */
5245 start ( ) {
5346 const globalObj = globalObject ( ) ;
5447
@@ -60,8 +53,8 @@ class Server extends EventTarget {
6053 }
6154
6255 /*
63- * Removes the mock websocket object from the global object
64- */
56+ * Removes the mock websocket object from the global object
57+ */
6558 stop ( callback = ( ) => { } ) {
6659 const globalObj = globalObject ( ) ;
6760
@@ -81,30 +74,51 @@ class Server extends EventTarget {
8174 }
8275
8376 /*
84- * This is the main function for the mock server to subscribe to the on events.
85- *
86- * ie: mockServer.on('connection', function() { console.log('a mock client connected'); });
87- *
88- * @param {string } type - The event key to subscribe to. Valid keys are: connection, message, and close.
89- * @param {function } callback - The callback which should be called when a certain event is fired.
90- */
77+ * This is the main function for the mock server to subscribe to the on events.
78+ *
79+ * ie: mockServer.on('connection', function() { console.log('a mock client connected'); });
80+ *
81+ * @param {string } type - The event key to subscribe to. Valid keys are: connection, message, and close.
82+ * @param {function } callback - The callback which should be called when a certain event is fired.
83+ */
9184 on ( type , callback ) {
9285 this . addEventListener ( type , callback ) ;
9386 }
9487
9588 /*
96- * This send function will notify all mock clients via their onmessage callbacks that the server
97- * has a message for them.
98- *
99- * @param {* } data - Any javascript object which will be crafted into a MessageObject.
100- */
101- send ( data , options = { } ) {
102- this . emit ( 'message' , data , options ) ;
89+ * Closes the connection and triggers the onclose method of all listening
90+ * websockets. After that it removes itself from the urlMap so another server
91+ * could add itself to the url.
92+ *
93+ * @param {object } options
94+ */
95+ close ( options = { } ) {
96+ const { code, reason, wasClean } = options ;
97+ const listeners = networkBridge . websocketsLookup ( this . url ) ;
98+
99+ // Remove server before notifications to prevent immediate reconnects from
100+ // socket onclose handlers
101+ networkBridge . removeServer ( this . url ) ;
102+
103+ listeners . forEach ( socket => {
104+ socket . readyState = WebSocket . CLOSE ;
105+ socket . dispatchEvent (
106+ createCloseEvent ( {
107+ type : 'close' ,
108+ target : socket ,
109+ code : code || CLOSE_CODES . CLOSE_NORMAL ,
110+ reason : reason || '' ,
111+ wasClean
112+ } )
113+ ) ;
114+ } ) ;
115+
116+ this . dispatchEvent ( createCloseEvent ( { type : 'close' } ) , this ) ;
103117 }
104118
105119 /*
106- * Sends a generic message event to all mock clients.
107- */
120+ * Sends a generic message event to all mock clients.
121+ */
108122 emit ( event , data , options = { } ) {
109123 let { websockets } = options ;
110124
@@ -144,48 +158,18 @@ class Server extends EventTarget {
144158 }
145159
146160 /*
147- * Closes the connection and triggers the onclose method of all listening
148- * websockets. After that it removes itself from the urlMap so another server
149- * could add itself to the url.
150- *
151- * @param {object } options
152- */
153- close ( options = { } ) {
154- const { code, reason, wasClean } = options ;
155- const listeners = networkBridge . websocketsLookup ( this . url ) ;
156-
157- // Remove server before notifications to prevent immediate reconnects from
158- // socket onclose handlers
159- networkBridge . removeServer ( this . url ) ;
160-
161- listeners . forEach ( socket => {
162- socket . readyState = WebSocket . CLOSE ;
163- socket . dispatchEvent (
164- createCloseEvent ( {
165- type : 'close' ,
166- target : socket ,
167- code : code || CLOSE_CODES . CLOSE_NORMAL ,
168- reason : reason || '' ,
169- wasClean
170- } )
171- ) ;
172- } ) ;
173-
174- this . dispatchEvent ( createCloseEvent ( { type : 'close' } ) , this ) ;
175- }
176-
177- /*
178- * Returns an array of websockets which are listening to this server
179- */
161+ * Returns an array of websockets which are listening to this server
162+ * TOOD: this should return a set and not be a method
163+ */
180164 clients ( ) {
181165 return networkBridge . websocketsLookup ( this . url ) ;
182166 }
183167
184168 /*
185- * Prepares a method to submit an event to members of the room
186- *
187- * e.g. server.to('my-room').emit('hi!');
188- */
169+ * Prepares a method to submit an event to members of the room
170+ *
171+ * e.g. server.to('my-room').emit('hi!');
172+ */
189173 to ( room , broadcaster , broadcastList = [ ] ) {
190174 const self = this ;
191175 const websockets = dedupe ( broadcastList . concat ( networkBridge . websocketsLookup ( this . url , room , broadcaster ) ) ) ;
0 commit comments