ulduldp commited on
Commit
c510df4
·
verified ·
1 Parent(s): 63ce6bd

Update public/chat.js

Browse files
Files changed (1) hide show
  1. public/chat.js +74 -9
public/chat.js CHANGED
@@ -1,23 +1,88 @@
1
  const socket = io();
2
 
3
- const urlParams = new URLSearchParams(window.location.search);
4
- const roomId = urlParams.get("room");
5
- const name = urlParams.get("name");
6
 
7
  document.getElementById("roomTitle").innerText = "Room: " + roomId;
8
 
 
 
 
9
  socket.emit("join-room", { roomId, name });
10
 
11
- socket.on("message", (data) => {
12
- const chat = document.getElementById("chat");
13
- chat.innerHTML += `<p><b>${data.name}:</b> ${data.text}</p>`;
14
- chat.scrollTop = chat.scrollHeight;
15
  });
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  function send() {
18
  const msgInput = document.getElementById("msg");
19
  const msg = msgInput.value;
20
- if (!msg) return;
21
- socket.emit("send-message", msg);
 
 
 
22
  msgInput.value = "";
23
  }
 
 
 
 
 
 
 
1
  const socket = io();
2
 
3
+ const params = new URLSearchParams(location.search);
4
+ const roomId = params.get("room");
5
+ const name = params.get("name");
6
 
7
  document.getElementById("roomTitle").innerText = "Room: " + roomId;
8
 
9
+ let peers = {};
10
+ let channels = {};
11
+
12
  socket.emit("join-room", { roomId, name });
13
 
14
+ socket.on("user-joined", ({ id }) => {
15
+ createPeer(id, true);
 
 
16
  });
17
 
18
+ socket.on("signal", async ({ from, data }) => {
19
+ if (!peers[from]) createPeer(from, false);
20
+
21
+ if (data.sdp) {
22
+ await peers[from].setRemoteDescription(data.sdp);
23
+ if (data.sdp.type === "offer") {
24
+ const answer = await peers[from].createAnswer();
25
+ await peers[from].setLocalDescription(answer);
26
+ socket.emit("signal", { to: from, data: { sdp: peers[from].localDescription } });
27
+ }
28
+ }
29
+
30
+ if (data.candidate) {
31
+ await peers[from].addIceCandidate(data.candidate);
32
+ }
33
+ });
34
+
35
+ function createPeer(id, initiator) {
36
+ const peer = new RTCPeerConnection({
37
+ iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
38
+ });
39
+
40
+ if (initiator) {
41
+ const channel = peer.createDataChannel("chat");
42
+ setupChannel(id, channel);
43
+ } else {
44
+ peer.ondatachannel = (e) => {
45
+ setupChannel(id, e.channel);
46
+ };
47
+ }
48
+
49
+ peer.onicecandidate = (e) => {
50
+ if (e.candidate) {
51
+ socket.emit("signal", { to: id, data: { candidate: e.candidate } });
52
+ }
53
+ };
54
+
55
+ peers[id] = peer;
56
+
57
+ if (initiator) {
58
+ peer.createOffer().then(offer => {
59
+ peer.setLocalDescription(offer);
60
+ socket.emit("signal", { to: id, data: { sdp: offer } });
61
+ });
62
+ }
63
+ }
64
+
65
+ function setupChannel(id, channel) {
66
+ channels[id] = channel;
67
+
68
+ channel.onmessage = (e) => {
69
+ addMessage("Peer", e.data);
70
+ };
71
+ }
72
+
73
  function send() {
74
  const msgInput = document.getElementById("msg");
75
  const msg = msgInput.value;
76
+ addMessage("Me", msg);
77
+
78
+ for (let id in channels) {
79
+ channels[id].send(msg);
80
+ }
81
  msgInput.value = "";
82
  }
83
+
84
+ function addMessage(sender, text) {
85
+ const chat = document.getElementById("chat");
86
+ chat.innerHTML += `<p><b>${sender}:</b> ${text}</p>`;
87
+ chat.scrollTop = chat.scrollHeight;
88
+ }