-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
100 lines (94 loc) · 2.22 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
100
<html>
<head>
<title>WebSocket Client</title>
<style type="text/css">
html, body {
font-family: sans-serif;
}
h1 {
font-size: 18px;
}
#viewport {
width: 440px;
height: 200px;
border: 1px solid #7F9DB9;
overflow: auto;
}
#msg {
width: 330px;
}
</style>
<script type="text/javascript">
var socket;
function init()
{
try {
var host = "ws://127.0.0.1:1222";
socket = new WebSocket(host);
with (socket) {
onopen = function(msg)
{
echo('Connected!');
};
onmessage = function(msg)
{
echo('Server said: ' + msg.data);
};
onclose = function(msg)
{
echo('Disconnected!');
};
}
}
catch (e) {
echo(e);
}
finally {
$('msg').focus();
}
} // init
function send()
{
try {
var msg = $('msg').value;
if (msg.length == 0) {
throw "Message cannot be empty!";
}
$('msg').value = "";
$('msg').focus();
socket.send(msg);
}
catch (e) {
echo(e);
}
} // send
function quit()
{
echo('Goodbye!');
socket.close();
socket = null;
} // quit
function $(id)
{
return document.getElementById(id);
} // $
function echo(msg)
{
$('viewport').innerHTML += msg + "<br />";
} // echo
function onkey(e)
{
if (e.keyCode == 13) {
send();
}
} // onkey
</script>
</head>
<body onload="init()">
<h1>WebSocket Client</h1>
<div id="viewport"></div>
<input id="msg" type="text" onkeypress="onkey(event)"/>
<button onclick="send()">Send</button>
<button onclick="quit()">Quit</button>
</body>
</html>