-
Notifications
You must be signed in to change notification settings - Fork 1
/
websockets.html
123 lines (96 loc) · 3.34 KB
/
websockets.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
{% extends "../demos.html" %}
{% block subSectionClasses %}demo_websockets{% endblock %}
{% block meta_title %}Demo - WebSockets{% endblock %}
{% block meta_description %}WebSockets demo and tutorial using Spincast{% endblock %}
{% block scripts %}
<script>
app.addMessage = function(message) {
var messages = $("#messages").val();
$("#messages").val('').val(messages + message + "\n");
}
app.showcaseWebsocket = null;
app.showcaseInit = function() {
if(!window.WebSocket) {
alert("Your browser does not support WebSockets!");
return;
}
app.showcaseWebsocket = new WebSocket("{{ isHttps ? "wss" : "ws" }}://" + location.host + "/demos-tutorials/websockets/echo-all-endpoint");
app.showcaseWebsocket.onopen = function(event) {
app.addMessage("WebSocket connection established!");
};
app.showcaseWebsocket.onclose = function(event) {
app.addMessage("WebSocket connection closed.");
};
app.showcaseWebsocket.onmessage = function(event) {
app.addMessage(event.data);
};
$("#msgForm").on("submit", function() {
var $msgField = $("#message");
app.sendWebsocketMessage($msgField.val());
$msgField.val("");
$msgField.focus();
return false;
});
$("#message").focus();
};
app.sendWebsocketMessage = function(message) {
if(!window.WebSocket || !app.showcaseWebsocket) {
return;
}
if(app.showcaseWebsocket.readyState != WebSocket.OPEN) {
app.addMessage("The WebSocket connection is not open.");
return;
}
app.showcaseWebsocket.send(message);
};
app.showcaseInit();
</script>
{% endblock %}
{% block subBody %}
<div class="container">
<div id="sectionTitle" class="row">
<div class="col-xs-12">
<h2 class="shortTop">
WebSockets demo
</h2>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<p>
Try opening multiple browser windows on this very page and then send some messages.
You can even ask a friend to do so at the same time: you should see his messages...
</p>
</div>
</div>
<div class="row">
<div class="col-xs-4">
<p>
<strong>Send a message:</strong>
</p>
<form id="msgForm">
<input type="text" id="message" value="Hi!"/>
<input type="submit" id="sendBtn" value="send" />
</form>
</div>
<div class="col-xs-8">
<p>
<strong>Messages:</strong>
</p>
<textarea id="messages" readonly></textarea>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h4>
<a class="anchor_small" href="#more" id="more" title="permanent link"></a>
More info
</h4>
<p>
Learn how to implement <a href="/documentation#websockets">WebSockets</a> in the
documentation.
</p>
</div>
</div>
</div>
{% endblock %}