-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
83 lines (76 loc) · 1.82 KB
/
test.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
test websocket
</title>
</head>
<style type="text/css">
div {
width: 100%;
height: 100px;
text-align: center;
padding-top: 20px;
border: 1px solid black;
}
</style>
<body>
<div>
<input id="content" type="text" name="" placeholder="消息">
<br>
<button onclick="sendMessageToUser()">发送</button>
</div>
</body>
<script type="text/javascript">
if (typeof(WebSocket) == 'undefined') {
alert('你的浏览器不支持 WebSocket ,推荐使用Google Chrome 或者 Mozilla Firefox');
}
var user_token = prompt("用户唯一TOKEN", "");
if (user_token == "") {
user_token = "user" + Date.parse(new Date());
}
var uri = 'ws://10.65.209.164:12345';
var so;
createSocket(uri);
/**
* socket建立
*/
function createSocket(uri) {
so = new WebSocket(uri);
console.log(so);
so.onopen = function() {
if (so.readyState == 1) {
console.log('===user register===', user_token);
so.send('{"Type":1,"From":"' + user_token + '","Target":[""],"Data":""}');
}
}
so.onclose = function() {
so = false;
}
so.onmessage = function(recv_msg) {
console.log('===recv_msg===', recv_msg);
// console.log('recv_msg.type------' + recv_msg.type);
}
}
/**
* 发送消息给用户
*/
function sendMessageToUser() {
console.log("这里的so", so);
var content = document.getElementById('content').value;
so.send('{"Type":2,"From":"' + user_token + '","Target":["0cc120124175285f80dc4b13e18730bb"],"Data":"' + content + '"}');
return false;
}
</script>
<!-- type Request struct {
From string
Target []string
Data string
}
type Content struct {
From *Client
Target []*Client
Data interface{}
} -->
</html>