@@ -247,6 +247,15 @@ function emitRefresh(socket) {
247247 } ) ;
248248}
249249
250+ function clearSocketQueue ( queue , socket ) {
251+ for ( var i = 0 ; i < queue . length ; i ++ ) {
252+ if ( ! queue [ i ] || queue [ i ] . id == socket . id ) {
253+ queue . splice ( i , 1 ) ;
254+ i -- ;
255+ }
256+ }
257+ }
258+
250259var isConnectionBusy = false ;
251260var connectionSocketQueue = [ ] ;
252261var isDisconnectBusy = false ;
@@ -263,12 +272,7 @@ function finishConnection(socket, note, user) {
263272 emitRefresh ( socket ) ;
264273
265274 //clear finished socket in queue
266- for ( var i = 0 ; i < connectionSocketQueue . length ; i ++ ) {
267- if ( ! connectionSocketQueue [ i ] || connectionSocketQueue [ i ] . id == socket . id ) {
268- connectionSocketQueue . splice ( i , 1 ) ;
269- i -- ;
270- }
271- }
275+ clearSocketQueue ( connectionSocketQueue , socket ) ;
272276 //seek for next socket
273277 isConnectionBusy = false ;
274278 if ( connectionSocketQueue . length > 0 )
@@ -291,6 +295,7 @@ function startConnection(socket) {
291295
292296 var notename = getNotenameFromSocket ( socket ) ;
293297 if ( ! notename ) {
298+ clearSocketQueue ( connectionSocketQueue , socket ) ;
294299 isConnectionBusy = false ;
295300 return ;
296301 }
@@ -303,12 +308,7 @@ function startConnection(socket) {
303308 } ) ;
304309 socket . disconnect ( true ) ;
305310 //clear err socket in queue
306- for ( var i = 0 ; i < connectionSocketQueue . length ; i ++ ) {
307- if ( ! connectionSocketQueue [ i ] || connectionSocketQueue [ i ] . id == socket . id ) {
308- connectionSocketQueue . splice ( i , 1 ) ;
309- i -- ;
310- }
311- }
311+ clearSocketQueue ( connectionSocketQueue , socket ) ;
312312 isConnectionBusy = false ;
313313 return logger . error ( err ) ;
314314 }
@@ -320,6 +320,7 @@ function startConnection(socket) {
320320 Note . findOrNewNote ( notename , permission , function ( err , note ) {
321321 if ( err ) {
322322 responseError ( res , "404" , "Not Found" , "oops." ) ;
323+ clearSocketQueue ( connectionSocketQueue , socket ) ;
323324 isConnectionBusy = false ;
324325 return ;
325326 }
@@ -393,12 +394,7 @@ function disconnect(socket) {
393394 emitOnlineUsers ( socket ) ;
394395
395396 //clear finished socket in queue
396- for ( var i = 0 ; i < disconnectSocketQueue . length ; i ++ ) {
397- if ( ! disconnectSocketQueue [ i ] || disconnectSocketQueue [ i ] . id == socket . id ) {
398- disconnectSocketQueue . splice ( i , 1 ) ;
399- i -- ;
400- }
401- }
397+ clearSocketQueue ( disconnectSocketQueue , socket ) ;
402398 //seek for next socket
403399 isDisconnectBusy = false ;
404400 if ( disconnectSocketQueue . length > 0 )
0 commit comments