Skip to content

Commit c6a569b

Browse files
author
edencoder
committed
amend issues, push for live
1 parent 659e27e commit c6a569b

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

bundles/socket/daemons/socket.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

bundles/socket/helpers/socket.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,61 @@ class SocketHelper extends Helper {
1414
super();
1515

1616
// Bind methods
17+
this.id = this.id.bind(this);
18+
this.join = this.join.bind(this);
1719
this.room = this.room.bind(this);
1820
this.user = this.user.bind(this);
1921
this.emit = this.emit.bind(this);
22+
this.leave = this.leave.bind(this);
2023
this.session = this.session.bind(this);
2124
}
2225

26+
/**
27+
* Emits to room
28+
*
29+
* @param {String} name
30+
* @param {String} type
31+
* @param {*} args
32+
*/
33+
id(id, type, ...args) {
34+
// Emit to socket
35+
this.eden.emit('socket.id', {
36+
id,
37+
type,
38+
args,
39+
}, true);
40+
}
41+
42+
/**
43+
* Emits to room
44+
*
45+
* @param {String} name
46+
* @param {String} type
47+
* @param {*} args
48+
*/
49+
join(id, room) {
50+
// Emit to socket
51+
this.eden.emit('socket.join', {
52+
id,
53+
room,
54+
}, true);
55+
}
56+
57+
/**
58+
* Emits to room
59+
*
60+
* @param {String} name
61+
* @param {String} type
62+
* @param {*} args
63+
*/
64+
leave(id, room) {
65+
// Emit to socket
66+
this.eden.emit('socket.leave', {
67+
id,
68+
room,
69+
}, true);
70+
}
71+
2372
/**
2473
* Emits to room
2574
*

0 commit comments

Comments
 (0)