-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimal_chat_selective_replications.html
137 lines (116 loc) · 4.64 KB
/
minimal_chat_selective_replications.html
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Minimal Multichat</title>
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
</head>
<body>
<h1>Multichat</h1>
<!-- Login Section -->
<div id="login">
<label for="username">Enter your username:</label>
<input id="username" type="text">
<button onclick="login()">Login</button>
<p id="loginError" style="color: red;"></p>
</div>
<!-- Chat Section -->
<div id="chat" style="display: none;">
<h2>Welcome, <span id="currentUser"></span></h2>
<!-- Connection Section -->
<div>
<label for="targetUsername">Connect to user:</label>
<input id="targetUsername" type="text">
<button onclick="connectToPeer()">Connect</button>
</div>
<h3>Connected Users:</h3>
<ul id="userList"></ul>
<!-- Chat Box -->
<h3>Chat:</h3>
<div id="chatbox" style="border: 1px solid black; height: 200px; overflow-y: scroll; padding: 5px;"></div>
<!-- Message Input -->
<div>
<input id="message" type="text" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
//let peer;
//let connections = {};
//let chats = [];
let screen = "login"; // initialize at login screen
let usernameInput = localStorage.getItem("username"); // to load saved username
let peerError = "";
let loading = false;
let peer = {}; // initialize as empty object instead of undefined
let targetIdInput = "";
let peerIds = []; // connected to nobody at first
let connections = {}; // maps peerIds to their correspondig PeerJS's DataConnection objects
let chats = [];
let chatMessageInput = "";
function login() {
const username = document.getElementById('username').value;
if (!username) {
document.getElementById('loginError').textContent = 'Username is required!';
return;
}
document.getElementById('loginError').textContent = '';
document.getElementById('currentUser').textContent = username;
document.getElementById('login').style.display = 'none';
document.getElementById('chat').style.display = 'block';
// Initialize PeerJS
peer = new Peer(username);
peer.on('open', () => console.log('Connected to signaling server'));
peer.on('connection', conn => {
configureConnection(conn);
conn.on('open', () => {
connections[conn.peer] = conn;
updateUserList();
});
});
}
function configureConnection(conn) {
conn.on('data', data => {
if (data.type === 'chat') {
chats.push(`${data.sender}: ${data.message}`);
updateChatBox();
}
});
conn.on('close', () => {
delete connections[conn.peer];
updateUserList();
});
}
function connectToPeer() {
const targetUsername = document.getElementById('targetUsername').value;
if (!targetUsername || connections[targetUsername]) return;
const conn = peer.connect(targetUsername);
configureConnection(conn);
conn.on('open', () => {
connections[targetUsername] = conn;
updateUserList();
});
}
function sendMessage() {
const message = document.getElementById('message').value;
if (!message) return;
const sender = peer.id;
const chat = { sender, message };
chats.push(`${sender}: ${message}`);
updateChatBox();
Object.values(connections).forEach(conn => conn.send({ type: 'chat', sender, message }));
document.getElementById('message').value = '';
}
function updateChatBox() {
const chatbox = document.getElementById('chatbox');
chatbox.innerHTML = chats.map(chat => `<p>${chat}</p>`).join('');
chatbox.scrollTop = chatbox.scrollHeight;
}
function updateUserList() {
const userList = document.getElementById('userList');
userList.innerHTML = Object.keys(connections).map(user => `<li>${user}</li>`).join('');
}
</script>
</body>
</html>