forked from oslabs-beta/sono.land
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventhandler.ts
61 lines (52 loc) · 2.12 KB
/
eventhandler.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
import { Client } from "./client.ts"
import { Packet } from "./packet.ts"
export class EventHandler {
constructor(){
return;
}
handleMessage(packet: Packet, client: Client, channelsList: {[key: string]: Record<string, Client>}){
// console.log('imhere', JSON.parse(message))
const { message } = packet.payload;
//now that we have the channel, we need to grab the channelsList
//grab its array of values, run a loop and send message to each client connected
// console.log(to)
const channelName = client.channel;
// console.log(channelName);
// console.log(channelsList[channelName]);
// if(channelsList[to]){
const ids = Object.keys(channelsList[channelName])
ids.forEach((id)=>{
channelsList[channelName][id].socket.send(JSON.stringify(message));
// console.log('client', client);
})
// }
}
changeChannel(packet: Packet, client: Client, channelsList: {[key: string]: Record<string, Client>}): {[key: string]: Record<string, Client>}{
const { to } = packet.payload;
const channel = client.channel;
delete channelsList[channel][client.id];
client.channel = to;
channelsList[to][client.id] = client;
return channelsList;
// delete channelsList[channel]
//how do we actually find the channel that has this specific client
}
broadcast(packet: Packet, client: Client, channelsList: {[key: string]: Record<string, Client>}){
// console.log('imhere', JSON.parse(message))
const { message } = packet.payload;
//now that we have the channel, we need to grab the channelsList
//grab its array of values, run a loop and send message to each client connected
// console.log(to)
const channelName = client.channel; //'home'
const currentClientId = client.id; //1001
// console.log(channelName);
// console.log(channelsList[channelName]);
// if(channelsList[to]){
const ids = Object.keys(channelsList[channelName])
ids.forEach((id)=>{
if(id !== currentClientId.toString()) channelsList[channelName][id].socket.send(JSON.stringify(message));
console.log('broadcasting');
})
// }
}
}