@@ -63,7 +63,9 @@ server = net.createServer (connection) ->
6363 if stage is 5
6464 # pipe sockets
6565 data = encryptor .encrypt data
66- ws .send data, { binary : true }
66+ if ws .readyState is WebSocket .OPEN
67+ ws .send data, { binary : true }
68+ connection .pause () if ws .bufferedAmount > 0
6769 return
6870 if stage is 0
6971 tempBuf = new Buffer (2 )
@@ -130,11 +132,15 @@ server = net.createServer (connection) ->
130132 ping = setInterval (->
131133 ws .ping " " , null , true
132134 , 50 * 1000 )
135+
136+ ws ._socket .on " drain" , ->
137+ connection .resume ()
138+
133139 return
134140
135141 ws .on " message" , (data , flags ) ->
136142 data = encryptor .decrypt data
137- connection .write (data)
143+ ws . _socket . pause () unless connection .write (data)
138144
139145 ws .on " close" , ->
140146 clearInterval ping
@@ -165,22 +171,25 @@ server = net.createServer (connection) ->
165171
166172 connection .on " end" , ->
167173 console .log " local disconnected"
168- ws .close () if ws
174+ ws .terminate () if ws
169175 server .getConnections (err, count) ->
170176 console .log " concurrent connections:" , count
171177 return
172178
173179 connection .on " error" , (e )->
174180 console .log " local error: #{ e} "
175- ws .close () if ws
181+ ws .terminate () if ws
176182 server .getConnections (err, count) ->
177183 console .log " concurrent connections:" , count
178184 return
179185
186+ connection .on " drain" , ->
187+ ws .resume ()
188+
180189 connection .setTimeout timeout, ->
181190 console .log " local timeout"
182191 connection .destroy ()
183- ws .close () if ws
192+ ws .terminate () if ws
184193
185194server .listen PORT, LOCAL_ADDRESS, ->
186195 address = server .address ()
0 commit comments