-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
63 lines (49 loc) · 1.77 KB
/
socket.js
File metadata and controls
63 lines (49 loc) · 1.77 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
62
63
import { updateClients } from './main';
// Receiver
let receiveSocket;
let broadcastSocket;
function loadSockets() {
receiveSocket = new WebSocket("wss://alt.mayankkumar.tech/rooms/broadcast/receive");
console.log("Attempting Connection...");
receiveSocket.onopen = () => {
console.log("Successfully Connected");
receiveSocket.send(`{\"room_id\": \"${localStorage.getItem("roomID")}\", \"participant_id\": \"${localStorage.getItem("myID")}\"}`)
};
receiveSocket.onclose = event => {
console.log("Socket Closed Connection: ", event);
receiveSocket.send("Client Closed!")
};
receiveSocket.onerror = error => {
console.log("Socket Error: ", error);
};
receiveSocket.onmessage = data => {
var data = JSON.parse(data.data);
var coord = data.co_ordinates;
var pid = data.participant_id;
console.log(coord, data);
// console.log(`x: ${coord.at_x} y:${coord.at_y}`)
updateClients(coord.at_x, coord.at_y, pid);
}
broadcastSocket = new WebSocket("wss://alt.mayankkumar.tech/rooms/broadcast/send");
console.log("Attempting Connection...");
broadcastSocket.onopen = () => {
console.log("Successfully Connected");
isBroadcastOpen = true;
broadcastSocket.send(`{\"room_id\": \"${localStorage.getItem("roomID")}\", \"participant_id\": \"${localStorage.getItem("myID")}\"}`)
};
broadcastSocket.onclose = event => {
isBroadcastOpen = false;
console.log("Socket Closed Connection: ", event);
broadcastSocket.send("Client Closed!")
};
broadcastSocket.onerror = error => {
isBroadcastOpen = false;
console.log("Socket Error: ", error);
};
broadcastSocket.onmessage = data => {
console.log(data)
}
}
// Broadcast
var isBroadcastOpen = false;
export { isBroadcastOpen, broadcastSocket, loadSockets }