forked from Azure/azure-signalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
99 lines (92 loc) · 3.78 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
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
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
.error {
color: red
}
.success {
color: green
}
</style>
</head>
<body>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<div id="status"></div>
<ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.4.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://localhost:8009/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
$.connection.hub.url = "http://localhost:8009/signalr";
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
chat.client.echo = function (message) {
// Html encode display name and message.
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li>' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
var connection = $.connection.hub;
connection.disconnected(function () {
reconnectOnFail("Disconnected.");
});
startConnectionWithRetry(connection);
function startConnectionWithRetry(hubConnection) {
hubConnection.start()
.done(function () {
$('#status').html("<h5 class='success' >Connected.</h5>")
chat.server.echo($('#message').val() + " connected.");
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.broadcastMessage($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
}
function reconnectOnFail(message) {
$('#status').html("<h5 class='error'>" + message + " Reconnecting...</h5>");
var delay = getRandom(200, 2000);
setTimeout(function () {
startConnectionWithRetry(connection)
}, delay);
}
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
});
</script>
</body>
</html>