-
Notifications
You must be signed in to change notification settings - Fork 0
/
multichat_robust_gpt.html
293 lines (245 loc) · 10.5 KB
/
multichat_robust_gpt.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!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="submitLogin(event)">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="submitConnection(event)">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="submitChat(event)">Send</button>
</div>
</div>
<script>
const appPrefix = "p2p-multichat-test-";
const ACK_TIMEOUT = 5000; // Timeout for acknowledgment in ms
const MAX_RETRIES = 3; // Maximum retries for sending messages
const RETRY_BACKOFF = 10000; // 2000 Initial backoff interval for retries
const RATE_LIMIT = 500; // Minimum interval between messages
let peer;
let connections = {};
let pendingAcks = {}; // Pending acknowledgments
let usernameInput = localStorage.getItem("username");
let chatMessageInput = "";
let chats = {};
let connectionLock = false;
let lastSentTime = 0; // Timestamp of the last sent message
function getPeerId(username) {
return appPrefix + username;
}
function getUsername(peerId) {
return peerId ? peerId.slice(appPrefix.length) : "";
}
function addConnection(conn) {
connections[conn.peer] = conn;
updatePeerIds();
console.log(`Connected to ${conn.peer}!`);
}
function removeConnection(conn) {
if (connections[conn.peer]) {
conn.close();
delete connections[conn.peer];
updatePeerIds();
}
}
function updatePeerIds() {
const userList = document.getElementById('userList');
userList.innerHTML = Object.keys(connections).map(user => `<li>${getUsername(user)}</li>`).join('');
}
function configureConnection(conn) {
conn.on("data", data => {
if (data.type === "connections") {
data.peerIds.forEach(peerId => {
if (!connections[peerId]) {
initiateConnection(peerId);
}
});
} else if (data.type === "chat") {
receiveChat(data.chat);
// Send acknowledgment back
conn.send({
type: "ack",
hash: data.chat.hash
});
} else if (data.type === "ack") {
if (pendingAcks[data.hash]) {
clearTimeout(pendingAcks[data.hash].timeout);
delete pendingAcks[data.hash];
}
}
});
conn.on("close", () => removeConnection(conn));
conn.on("error", () => removeConnection(conn));
}
function initiateConnection(peerId) {
if (connectionLock || (connections[peerId] && connections[peerId].open)) return;
connectionLock = true;
console.log(`Connecting to ${peerId}...`);
const conn = peer.connect(peerId, { serialization: "json" });
configureConnection(conn);
conn.on("open", () => {
addConnection(conn);
conn.send({
type: "connections",
peerIds: Object.keys(connections)
});
connectionLock = false;
});
setTimeout(() => (connectionLock = false), 1000); // Release lock after 1 second
}
function createPeer() {
peer = new Peer(getPeerId(usernameInput));
peer.on("open", () => {
document.getElementById('login').style.display = 'none';
document.getElementById('chat').style.display = 'block';
document.getElementById('currentUser').textContent = usernameInput;
});
// peer.on("connection", conn => {
// configureConnection(conn);
// conn.on("open", () => addConnection(conn));
// });
// when peer receives a connection
peer.on('connection', conn => {
peerIds = Object.keys(connections)
if (!peerIds.includes(conn.peer)) {
configureConnection(conn);
conn.on("open", () => {
console.log(`!!Connecting here [createpeer open] to ...${conn.peer}`);
addConnection(conn);
// send every connection previously established to connect everyone (merge chat rooms)
conn.send({
type: "connections",
peerIds: peerIds
});
});
}
});
peer.on("error", error => {
console.error(`PeerJS Error: ${error.type}`);
});
}
async function sha256(message) {
const msgBuffer = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
// function receiveChat(chat) {
// const chatbox = document.getElementById('chatbox');
// const chatHtml = `<p>${chat.sender}: ${chat.message} #${chat.hash}</p>`;
// chatbox.innerHTML += chatHtml;
// chatbox.scrollTop = chatbox.scrollHeight;
// }
function receiveChat(chat) {
//chats.push(chat);
//console.log(`received chat with hash... ${chat.hash}`);
chats[chat.hash] = chat;
// if(!chats[chat.hash]){
// chats[chat.hash] = chat;
// }
const chatbox = document.getElementById('chatbox');
//chatbox.innerHTML = chats.map(chat => `<p>${ chat.sender } : ${ chat.message } # ${ chat.hash }</p>`).join('');
//Object.keys( d ).map( function(key){ return key+"="+d[key] }).join("&") //outputs "foo=0&bar=1"
chatbox.innerHTML = Object.keys(chats).map(function(key){
chat = chats[key];
return `<p>${ chat.sender } : ${ chat.message } # ${ chat.hash }</p>`;
}).join('');
chatbox.scrollTop = chatbox.scrollHeight;
//localStorage.setItem("chats", JSON.stringify(chats));
}
function submitLogin(event) {
event.preventDefault();
usernameInput = document.getElementById('username').value;
if (usernameInput.length > 0) {
localStorage.setItem("username", usernameInput);
createPeer();
}
}
function submitConnection(event) {
event.preventDefault();
const targetIdInput = document.getElementById('targetUsername').value;
const peerId = getPeerId(targetIdInput);
initiateConnection(peerId);
}
function canSendMessage() {
const now = Date.now();
if (now - lastSentTime > RATE_LIMIT) {
lastSentTime = now;
return true;
}
return false;
}
async function submitChat(event) {
event.preventDefault();
if (!canSendMessage()) {
console.warn("Rate limit exceeded. Wait before sending another message.");
return;
}
chatMessageInput = document.getElementById('message').value;
if (chatMessageInput.length > 0) {
const chatTime = new Date().getTime();
const fullMessage = `${usernameInput}:${chatMessageInput}:${chatTime}`;
const messageHash = await sha256(fullMessage);
const chat = {
sender: usernameInput,
message: chatMessageInput,
timestamp: chatTime,
hash: messageHash
};
receiveChat(chat);
const sendWithRetry = (conn, retriesLeft, retryInterval) => {
console.log(`Sending chat to ${conn.peer}, retries left: ${retriesLeft}`);
conn.send({
type: "chat",
chat
});
pendingAcks[messageHash] = {
chat,
conn,
retriesLeft,
timeout: setTimeout(() => {
if (retriesLeft > 0) {
console.log(`Retrying for hash: ${messageHash}`);
sendWithRetry(conn, retriesLeft - 1, retryInterval * 2);
} else {
console.error(`Message dropped after max retries: ${messageHash}`);
delete pendingAcks[messageHash];
}
}, retryInterval)
};
};
Object.values(connections).forEach(conn => {
sendWithRetry(conn, MAX_RETRIES, RETRY_BACKOFF);
});
chatMessageInput = "";
document.getElementById('message').value = '';
}
}
</script>
</body>
</html>