-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_handler.js
More file actions
61 lines (61 loc) · 2.6 KB
/
websocket_handler.js
File metadata and controls
61 lines (61 loc) · 2.6 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handle_room_message = handle_room_message;
const data_1 = require("./data");
const utility_1 = require("./utility");
/*
* SEND MESSAGES VIA WEBSOCKET - Parameters that are always required (see websocket_server.ts)
* - token: the user's session token
*/
function handle_room_message(message, sender, client_socket) {
var _a;
// Check that a target room (one in which the user is participating) exists
const target_room = data_1.liveRooms.find(room => room.get_participants.includes(sender));
if (!target_room) {
console.error(`User ${sender.username} sent a message but is not a participant in any room`);
return;
}
switch (message.type) {
case "confirm_registration":
// Check that the user has a websocket
if (!sender.websocket) {
console.warn(`Could not catch ${sender.username} up: user has no bound WebSocket`);
return;
}
// Catch the user up on others' text
target_room.get_participants.forEach(participant => {
const text = target_room.userText.get(participant);
if (!text) {
return;
}
// @ts-ignore (websocket was already checked earlier)
sender.websocket.send(JSON.stringify({
type: "room_message",
body: { sender: participant, text: text }
}));
});
return;
case 'room_message':
// Send typing updates to peers
// console.log(`Message from ${sender.username} to room ${target_room.id}!`);
target_room.message(sender, message);
return;
case 'room-event_user-expel':
if (sender !== target_room.owner) {
console.log(`${sender.username} tried to expel a participant without being the room owner`);
return;
}
// Propagate event and expel user
const target = data_1.liveUsers.find(user => user.uuid === message.body.target_user);
if (!target) {
return;
}
console.log(`${sender.username} has expelled ${target === null || target === void 0 ? void 0 : target.username}`);
(_a = target.websocket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
type: 'room-event_user-expel',
body: { reason: message.body.reason },
}));
(0, utility_1.deleteUser)(target);
return;
}
}