-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-client.html
114 lines (101 loc) · 3.42 KB
/
ws-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
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
<html>
<head>
<title>WebSocket TEST</title>
<style>
html,body{font:normal 0.9em arial,helvetica;}
#log {width:440px; height:200px; border:1px solid #7F9DB9; overflow:auto;}
#msg {width:330px;}
</style>
<script>
String.prototype.repeat = function(num)
{
return new Array(num + 1).join(this);
}
var socket;
function createSocket(host) {
if (window.WebSocket)
return new WebSocket(host);
else if (window.MozWebSocket)
return new MozWebSocket(host);
}
function init() {
if(socket!=null)
{
socket.close();
socket = null;
}
var host = "wss://" + window.location.host + ":12345/echo";
try {
socket = createSocket(host);
log('WebSocket - status ' + socket.readyState);
socket.onopen = function(msg) {
log("Welcome - status " + this.readyState);
};
socket.onmessage = function(msg) {
log("Received (" + msg.data.length + " bytes): " + msg.data);
};
socket.onclose = function(msg) {
log("Disconnected - status " + this.readyState);
};
}
catch (ex) {
log(ex);
}
}
function send(msg) {
try {
socket.send(msg);
log('Sent (' + msg.length + " bytes): " + msg);
} catch (ex) {
log(ex);
}
}
function sendMsg(cmd,type,id)
{
var str='{"cmd":"'+cmd+'","type":"'+type+'","id":'+id+'}';
send(str);
}
function quit() {
log("Goodbye!");
socket.close();
socket = null;
}
function register()
{
sendMsg('register','user','0');
}
function request()
{
var type = document.getElementById('type').value;
var id = document.getElementById('id').value;
sendMsg('request',type,id);
}
function ignore()
{
var type = document.getElementById('type').value;
var id = document.getElementById('id').value;
sendMsg('ignore',type,id);
}
// Utilities
function $(id) {
return document.getElementById(id);
}
function log(msg) {
$("log").innerHTML += "<br>" + msg;
}
//
</script>
</head>
<body>
<h3>User site</h3>
<button onclick="init()">connect</button>
<button onclick="register()">register</button>
<button onclick="quit()">quit</button>
<div id="log"></div>
<br>
<input id="type" type="textbox" value="tag"/>
<input id="id" type="textbox" value="3"/>
<button onclick="request()">request</button>
<button onclick="ignore()">ignore</button>
</body>
</html>