-
Notifications
You must be signed in to change notification settings - Fork 12
/
websockets.ts
155 lines (134 loc) · 4.26 KB
/
websockets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { EventSocket } from '@andes/event-bus';
import * as debug from 'debug';
import { Server } from 'http';
import { Auth } from './auth/auth.class';
import { RedisWebSockets } from './config.private';
const log = debug('websocket');
export class Packet {
private io;
private socket;
public event: String;
public data: any;
public user: any;
constructor(io, socket, event, data) {
this.io = io;
this.socket = socket;
this.event = event;
this.data = data;
this.user = socket.user;
}
broadcast(event, data) {
this.socket.emit(event, data);
}
toRoom(room, event, data) {
this.socket.to(room).emit(event, data);
}
}
export class Websockets {
static io;
/**
* Inicializa el servicio de websockets
*
* @static
* @param {express.Express} app aplicación de Express
*
* @memberOf Auth
*/
static onAuth(socket, token) {
const user = Auth.validateToken(token);
if (user) {
socket.user = user;
// Automaticamente agregamos al usuario a ciertos rooms.
switch (user.type) {
case 'user-token':
socket.join(`user-${user.usuario.id}`);
break;
case 'turnero-token':
socket.join(`turnero-pantalla-${user.app.id}`);
break;
case 'paciente-token':
for (const paciente of user.pacientes) {
socket.join(`paciente-${paciente.id}`);
}
break;
default:
socket.emit('auth', { status: 'error' });
return null;
}
socket.emit('auth', { status: 'ok' });
} else {
socket.emit('auth', { status: 'error' });
}
}
static onRoom(socket, { name }) {
if (name) {
switch (name) {
case 'turnero':
if (socket.user.organizacion) {
socket.join(`turnero-${socket.user.organizacion._id}`);
}
break;
default:
socket.join(name);
break;
}
}
}
static onLeave(socket, { name }) {
if (name) {
switch (name) {
case 'turnero':
if (socket.user.organizacion) {
socket.leave(`turnero-${socket.user.organizacion._id}`);
}
break;
default:
socket.leave(name);
break;
}
}
}
static broadcast(event, data) {
this.io.emit(event, data);
}
static toRoom(room, event, data) {
this.io.to(room).emit(event, data);
}
static initialize(server: Server) {
log('Websocket start');
const redis = require('socket.io-redis');
const socketIO = require('socket.io');
this.io = socketIO(server, { path: '/ws', transports: ['websocket', 'polling'] });
if (RedisWebSockets.active) {
this.io.adapter(redis({ host: RedisWebSockets.host, port: RedisWebSockets.port }));
}
this.io.on('connection', (socket) => {
log('onConnection');
socket.use((packet, next) => {
// Handler
const [event, data] = packet;
log(event, data);
switch (event) {
case 'auth':
const token = data.token;
this.onAuth(socket, token);
break;
case 'room':
this.onRoom(socket, data);
break;
case 'leave':
this.onLeave(socket, data);
break;
default:
const p = new Packet(this.io, socket, event, data);
EventSocket.emitAsync(event, p);
break;
}
next();
});
socket.on('disconnect', () => {
log('onDisconnect');
});
});
}
}