-
Notifications
You must be signed in to change notification settings - Fork 2
/
webtest.html
92 lines (78 loc) · 2.75 KB
/
webtest.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
<!doctype html>
<html>
<head>
<title>WebSockets Test</title>
<meta charset="utf-8" />
<style type="text/css">
body {
text-align: center;
min-width: 500px;
}
</style>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
// log function
log = function(data){
$("div#terminal").prepend("</br>" +data);
console.log(data);
};
$(document).ready(function () {
$("div#message_details").hide()
var ws;
$("#open").click(function(evt) {
evt.preventDefault();
var host = $("#host").val();
var port = $("#port").val();
var uri = $("#uri").val();
// create websocket instance
ws = new WebSocket("ws://" + host + ":" + port + uri);
// Handle incoming websocket message callback
ws.onmessage = function(evt) {
log("Message Received: " + evt.data)
document.getElementById("message").innerHTML = evt.data;
};
// alert("message received: " + evt.data);
// };
// Close Websocket callback
ws.onclose = function(evt) {
log("***Connection Closed***");
alert("Connection close");
$("#host").css("background", "#ff0000");
$("#port").css("background", "#ff0000");
$("#uri").css("background", "#ff0000");
$("div#message_details").empty();
};
// Open Websocket callback
ws.onopen = function(evt) {
$("#host").css("background", "#00ff00");
$("#port").css("background", "#00ff00");
$("#uri").css("background", "#00ff00");
$("div#message_details").show();
log("***Connection Opened***");
};
});
// Send websocket message function
// $("#send").click(function(evt) {
// log("Sending Message: "+$("#message").val());
// ws.send($("#message").val());
// });
});
</script>
</head>
<body>
<h1>WebSocket Test</h1>
<div id="connection_details">
<label for="host">host:</label>
<input type="text" id="host" value="localhost" style="background:#ff0000;"/><br />
<label for="port">port:</label>
<input type="text" id="port" value="8888" style="background:#ff0000;"/><br />
<label for="uri">uri:</label>
<input type="text" id="uri" value="/ws" style="background:#ff0000;"/><br />
<input type="submit" id="open" value="open" />
</div>
<div id="message"><p></p>
</div>
<div id="terminal">
</div>
</body>
</html>