-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (44 loc) · 1.42 KB
/
script.js
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
let peer;
let conn;
let fixedPeerId;
function initializePeer() {
fixedPeerId = prompt("Enter your fixed Peer ID (e.g., 'user1' or 'user2'):");
peer = new Peer(fixedPeerId);
peer.on('open', function(id) {
document.getElementById('peerId').textContent = id;
});
peer.on('connection', function(connection) {
conn = connection;
conn.on('data', function(data) {
displayMessage(data, false);
});
});
}
function connectToPeer() {
const otherPeerId = prompt("Enter the Peer ID you want to connect to (e.g., 'user1' or 'user2'):");
conn = peer.connect(otherPeerId);
conn.on('open', function() {
conn.on('data', function(data) {
displayMessage(data, false);
});
});
}
function sendMessage() {
const message = document.getElementById('messageInput').value;
if (conn && conn.open) {
conn.send(message);
displayMessage(message, true);
}
document.getElementById('messageInput').value = '';
}
function displayMessage(message, isMine) {
const chatWindow = document.getElementById('chatWindow');
const messageElement = document.createElement('div');
messageElement.className = isMine ? 'my-message' : 'other-message';
messageElement.textContent = message;
chatWindow.appendChild(messageElement);
chatWindow.scrollTop = chatWindow.scrollHeight;
}
window.onload = function() {
initializePeer();
}