File size: 2,226 Bytes
a7476ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import RoomServices from './RoomServices.js';
import WebSocket from 'ws';
class MessageService {
    username;
    message;
    room_id;
    action;
    rooms;
    con;
    constructor({ msg, rooms, con, }) {
        const layers = JSON.parse(msg);
        this.room_id = layers['room_id'];
        this.username = layers['username'];
        this.message = layers['message'];
        this.action = layers['action'];
        this.rooms = rooms;
        this.con = con;
        if (this.action.toLowerCase() === 'join') {
            this.join();
        }
        else if (this.action.toLowerCase() === 'leave') {
            this.leave();
            // } else if (this.action.toLocaleLowerCase() === 'switch') {
            //     this.switchRooms()
        }
        else {
            this.broadcast();
        }
    }
    join() {
        this.rooms.putRoom(this.room_id, this.username, this.con);
        this.broadcast();
    }
    leave() {
        this.rooms.leaveRoom(this.room_id, this.username);
        this.broadcast();
    }
    broadcast() {
        if (!this.rooms.hasRoom(this.room_id))
            return;
        if (!this.rooms.getRoom(this.room_id))
            return;
        let arr_rooms = this.rooms.getRoom(this.room_id);
        if (!arr_rooms)
            return;
        for (const [key, value] of arr_rooms) {
            if (this.action.toLowerCase() === 'join') {
                value.send(`${this.username} has joined the ${this.room_id} chat!`);
            }
            else if (this.action.toLowerCase() === 'leave') {
                value.send(`${this.username} has left the ${this.room_id} chat!`);
            }
            else {
                value.send(`${this.username}: ${this.message}`);
            }
        }
    }
    broadcast_message(msg) {
        if (!this.rooms.hasRoom(this.room_id))
            return;
        if (!this.rooms.getRoom(this.room_id))
            return;
        let arr_rooms = this.rooms.getRoom(this.room_id);
        if (!arr_rooms)
            return;
        for (const [key, value] of arr_rooms) {
            value.send(`${this.username}: ${msg}`);
        }
    }
}
export default MessageService;
//# sourceMappingURL=MessageService.js.map