-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
56 lines (48 loc) · 1.88 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="A simple WebSocket demo on Azure SignalR Serverless WebSocket">
<title>A Simple Serverless WebSocket Chat</title>
</head>
<body>
<div id="app">
<h5>Current user: <span>%%%___user___%%%</span></h5>
<h5>Current endpoint: <span>%%%____URL____%%%</span></h5>
<form>
<input id="message" type="text">
<button id="submit" type="submit">Send</button>
</form>
<div id="log"></div>
<div id="chatbox"></div>
</div>
<script>
const user = '%%%___user___%%%';
document.addEventListener('DOMContentLoaded', function () {
var log = document.getElementById("log");
var message = document.getElementById("message");
var chatbox = document.getElementById("chatbox");
var socket = new WebSocket('%%%____URL____%%%');
socket.onopen = function (e) {
log.innerText = "Connection opened."
socket.send(`Hello I am ${user}`);
}
socket.onmessage = function (e) {
chatbox.innerText += "\n" + e.data;
}
socket.onclose = function (event) {
log.innerText = `Connection closed: code=${event.code} reason=${event.reason}`;
};
socket.onerror = function (error) {
log.innerText = `Connection error: ${error.message}`;
};
document.getElementById('submit').addEventListener('click', function (event) {
event.preventDefault();
console.log("Sending " + message.value);
socket.send(`${user}: ${message.value}`);
});
});
</script>
</body>
</html>