-
Notifications
You must be signed in to change notification settings - Fork 95
/
chat.js
91 lines (80 loc) · 3 KB
/
chat.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
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
"use strict";
function getRandomDelay() {
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
return getRandom(1000, 2000);
}
function delay(time) {
return new Promise((resolve) => {
setTimeout(() => resolve(), time);
});
}
var connection = new signalR.HubConnectionBuilder()
.withUrl("/chat")
.withAutomaticReconnect({
nextRetryDelayInMilliseconds: function (retryContext) {
return getRandomDelay();
}
})
.build();
//Disable the send button until connection is established.
document.getElementById("getButton").disabled = true;
document.getElementById("sendButton").disabled = true;
var messageCallback = function (message) {
var li = document.createElement("li");
document.getElementById("messagesList").appendChild(li);
// We can assign user-supplied strings to an element's textContent because it
// is not interpreted as markup. If you're assigning in any other way, you
// should be aware of possible script injection concerns.
li.textContent = message;
};
connection.on("Connect", messageCallback);
connection.on("Broadcast", messageCallback);
connection.on("GetMessage", async function () {
document.getElementById("sendButton").disabled = false;
var res = await new Promise(function (resolve, reject) {
document.getElementById("sendButton").addEventListener("click", (event) => {
var message = document.getElementById("messageInput").value;
resolve(message);
});
});
document.getElementById("sendButton").disabled = true;
return res;
});
async function startConnection() {
let count = 0;
do {
try {
count++;
console.log(`Attempt ${count}`);
await connection.start();
document.getElementById("getButton").disabled = false;
break;
} catch (err) {
await delay(getRandomDelay());
console.error(err.toString());
}
} while (true);
}
startConnection();
document.getElementById("getButton").addEventListener("click", async function (event) {
var id = document.getElementById("IDInput").value;
var ret = await connection.invoke("GetMessage", id).catch(function (err) {
return console.error(err.toString());
});
var li = document.createElement("li");
document.getElementById("messagesList").appendChild(li);
// We can assign user-supplied strings to an element's textContent because it
// is not interpreted as markup. If you're assigning in any other way, you
// should be aware of possible script injection concerns.
li.textContent = ret;
event.preventDefault();
});
document.getElementById("broadcastButton").addEventListener("click", async function (event) {
var message = document.getElementById("messageInput").value;
await connection.invoke("Broadcast", message).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();
});