@@ -51,6 +51,8 @@ export default class SocketDaemon extends Daemon {
5151 this . countConnections = this . countConnections . bind ( this ) ;
5252
5353 // Bind private methods
54+ this . id = this . id . bind ( this ) ;
55+ this . room = this . room . bind ( this ) ;
5456 this . emit = this . emit . bind ( this ) ;
5557 this . user = this . user . bind ( this ) ;
5658 this . onCall = this . onCall . bind ( this ) ;
@@ -122,6 +124,15 @@ export default class SocketDaemon extends Daemon {
122124 next ( ) ;
123125 } ) ;
124126
127+ // user
128+ this . eden . on ( 'socket.id' , this . id , true ) ;
129+
130+ // leave/join
131+ this . eden . on ( 'socket.join' , ( ...args ) => this . room ( 'join' , ...args ) , true ) ;
132+
133+ // leave/join
134+ this . eden . on ( 'socket.leave' , ( ...args ) => this . room ( 'leave' , ...args ) , true ) ;
135+
125136 // Listen for global event for emit
126137 this . eden . on ( 'socket.emit' , this . emit , true ) ;
127138
@@ -244,6 +255,9 @@ export default class SocketDaemon extends Daemon {
244255 // check ids
245256 if ( ! IDs [ `${ key } ID` ] ) return ;
246257
258+ // check size
259+ if ( ! this . __connections [ `${ key } s` ] || ! this . __connections [ `${ key } s` ] . get ( IDs [ `${ key } ID` ] ) ) return ;
260+
247261 // remove
248262 this . __connections [ `${ key } s` ] . get ( IDs [ `${ key } ID` ] ) . delete ( IDs . socketID ) ;
249263
@@ -293,19 +307,45 @@ export default class SocketDaemon extends Daemon {
293307 //
294308 // ////////////////////////////////////////////////////////////////////////////
295309
310+ /**
311+ * emit to id
312+ *
313+ * @param param0
314+ */
315+ id ( { id, type, args } ) {
316+ // check connections
317+ if ( ! this . __connections . sockets . has ( id ) ) return ;
318+
319+ // emit
320+ this . __connections . sockets . get ( id ) . emit ( type , ...args ) ;
321+ }
322+
323+ /**
324+ * emit to id
325+ *
326+ * @param param0
327+ */
328+ room ( type , { id, room } ) {
329+ // check connections
330+ if ( ! this . __connections . sockets . has ( id ) ) return ;
331+
332+ // emit
333+ this . __connections . sockets . get ( id ) [ type ] ( room ) ;
334+ }
335+
296336 /**
297337 * Emit to socket funciton
298338 *
299339 * @param {Object } data
300340 */
301- emit ( data ) {
341+ emit ( { room , type , args } ) {
302342 // Check if room
303- if ( data . room ) {
343+ if ( room ) {
304344 // Emit to room
305- this . __socketIO . to ( data . room ) . emit ( data . type , ... data . args ) ;
345+ this . __socketIO . to ( room ) . emit ( type , ...args ) ;
306346 } else {
307347 // Emit to everyone
308- this . __socketIO . emit ( data . type , ... data . args ) ;
348+ this . __socketIO . emit ( type , ...args ) ;
309349 }
310350 }
311351
@@ -355,7 +395,7 @@ export default class SocketDaemon extends Daemon {
355395 await this . eden . hook ( 'socket.call.opts' , opts ) ;
356396
357397 // Run endpoint
358- const response = await controller [ call . fn ] ( ...data . args , opts ) ;
398+ const response = await controller [ call . fn ] ( opts , ...data . args ) ;
359399
360400 // Return response
361401 socket . emit ( data . id , response ) ;
0 commit comments