forked from kissthink/scripts
-
Notifications
You must be signed in to change notification settings - Fork 38
/
WebSockets.html
97 lines (79 loc) · 2.14 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
<html>
<head>
<title>WebSocket Tester</title>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script language="javascript" type="text/javascript">
var websocket;
var ping;
jQuery(function ($) {
$('#connect').click(doConnect);
$('#disconnect').click(doDisconnect);
$('#send').click(function () {
doSend( $('#message').val() )
});
function doConnect() {
websocket = new WebSocket( $("#target").val() );
websocket.onopen = function (evt) {
onOpen(evt)
var ping = setInterval(function () { doPing() }, 1000);
};
websocket.onclose = function (evt) {
onClose(evt)
};
websocket.onmessage = function (evt) {
onMessage(evt)
};
websocket.onerror = function (evt) {
onError(evt)
};
}
function doDisconnect() {
websocket.close();
}
function onOpen(evt) {
writeToScreen("CONNECTED");
}
function onClose(evt) {
writeToScreen("DISCONNECTED");
}
function onMessage(evt) {
if ((evt.data != "ping") && (evt.data != "pong")) {
writeToScreen('RECIEVED: ' + evt.data);
}
}
function onError(evt) {
writeToScreen('ERROR:' + evt.data);
}
function doSend(message) {
if (message != "ping") {
writeToScreen('SENT: ' + message);
}
websocket.send(message);
}
function writeToScreen(message) {
$('#output').append(message + '<br /><br />');
}
function doPing() {
if (websocket != "undefined") {
doSend("ping");
}
}
});
</script>
</head>
<body>
<h2>WebSocket Tester</h2>
Target:
<input type="text" id="target" value="" />
<br />
<button id="connect">Connect</button>
<button id="disconnect">Disconnect</button>
<br />
<br />Message:
<input type="text" id="message" value="" />
<button id="send">Send</button>
<br />
<br />Output:
<br /> <pre><div id="output"></div></pre>
</body>
</html>