forked from m8rge/cwebsocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.html
81 lines (73 loc) · 3.4 KB
/
client.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
<!DOCTYPE html>
<html>
<head>
<title>Websocket client</title>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery.js"></script>
</head>
<body>
<div class="container">
<h1 class="page-header">Websocket client</h1>
<form action="" class="form-inline" id="connectForm">
<div class="input-append">
<input type="text" class="input-large" value="ws://localhost:8088/echo" id="wsServer">
<button class="btn" type="submit" id="connect">Connect</button>
<button class="btn" disabled="disabled" id="disconnect">Disconnect</button>
</div>
</form>
<form action="" id="sendForm">
<div class="input-append">
<input class="input-large" type="text" placeholder="message" id="message" disabled="disabled">
<button class="btn btn-primary" type="submit" id="send" disabled="disabled">send</button>
</div>
</form>
<hr>
<ul class="unstyled" id="log"></ul>
</div>
<script type="text/javascript">
$(document).ready(function() {
var ws;
$('#connectForm').on('submit', function() {
if ("WebSocket" in window) {
ws = new WebSocket($('#wsServer').val());
ws.onopen = function() {
$('#log').append('<li><span class="badge badge-success">websocket opened</span></li>');
$('#wsServer').attr('disabled', 'disabled');
$('#connect').attr('disabled', 'disabled');
$('#disconnect').removeAttr('disabled');
$('#message').removeAttr('disabled').focus();
$('#send').removeAttr('disabled');
};
ws.onerror = function() {
$('#log').append('<li><span class="badge badge-important">websocket error</span></li>');
};
ws.onmessage = function(event) {
$('#log').append('<li>recieved: <span class="badge">' + event.data + '</span></li>');
};
ws.onclose = function() {
$('#log').append('<li><span class="badge badge-important">websocket closed</span></li>');
$('#wsServer').removeAttr('disabled');
$('#connect').removeAttr('disabled');
$('#disconnect').attr('disabled', 'disabled');
$('#message').attr('disabled', 'disabled');
$('#send').attr('disabled', 'disabled');
};
} else {
$('#log').append('<li><span class="badge badge-important">WebSocket NOT supported in this browser</span></li>');
}
return false;
});
$('#sendForm').on('submit', function() {
var message = $('#message').val();
ws.send(message);
$('#log').append('<li>sended: <span class="badge">' + message + '</span></li>');
return false;
});
$('#disconnect').on('click', function() {
ws.close();
return false;
});
});
</script>
</body>
</html>