game1.jsp
3.7 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户发消息</title>
<style type="text/css">
#connect-container {
float: left;
width: 400px
}
#connect-container div {
padding: 5px;
}
#console-container {
float: left;
margin-left: 15px;
width: 400px;
}
#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}
#console p {
padding: 0;
margin: 0;
}
</style>
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
<script type="text/javascript">
var ws = null;
var url = null;
var transports = [];
connect();
function connect() {
if (ws != null && (ws.readyState == 1 || ws.readyState == 2))
return;//判断是否已成功连接,已成功连接不在执行
if (ws != null)
ws.close();
ws = new WebSocket('ws://mini.weiyisz.com/zzhnc/web/socket/77/1');
ws.onopen = function() {
console.log("连接服务器成功");
};
ws.onmessage = function(event) {
log('Received: ' + event.data);
};
ws.onclose = function() {
};
}
function disconnect() {
if (ws != null) {
ws.close();
ws = null;
}
}
//查询数据条件
function getSearchJson() {
var id = Math.ceil(Math.random() * 10);
var message = document.getElementById('message').value;
var askfrom = document.getElementById('askfrom').value;
var salesid = document.getElementById('salesid').value;
var json = {
"fansid" : 77,
"ask" : message,
"reply" : "",
"askfrom" : askfrom,
"salesid":salesid,
"readed" : false
};
json = JSON.stringify(json);
return json;
}
window.setInterval("connect()", 5000);
function echo() {
if (ws != null) {
if (ws.readyState == 1) {
var json = getSearchJson();
ws.send(json);
} else {
//connect();
}
} else {
alert('connection not established, please connect.');
}
}
function updateUrl(urlPath) {
if (urlPath.indexOf('sockjs') != -1) {
url = urlPath;
document.getElementById('sockJsTransportSelect').style.visibility = 'visible';
} else {
if (window.location.protocol == 'http:') {
url = 'ws://' + window.location.host + urlPath;
} else {
url = 'wss://' + window.location.host + urlPath;
}
document.getElementById('sockJsTransportSelect').style.visibility = 'hidden';
}
}
function updateTransport(transport) {
transports = (transport == 'all') ? [] : [ transport ];
}
function log(message) {
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(message));
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop = console.scrollHeight;
}
</script>
</head>
<body>
<noscript>
<h2 style="color: #ff0000">Seems your browser doesn't support
Javascript! Websockets rely on Javascript being enabled. Please
enable Javascript and reload this page!</h2>
</noscript>
<div>
<div id="connect-container">
<div>
<textarea id="message" style="width: 350px">Here is a message!</textarea>
</div>
<div>
万小二 :<textarea id="askfrom" style="width: 350px; height: 10">1</textarea>
</div>
<div>
销售id:<textarea id="salesid" style="width: 350px; height: 10">1</textarea>
</div>
<div>
<button id="echo" onclick="echo();">Echo message</button>
</div>
</div>
<div id="console-container">
<div id="console"></div>
</div>
</div>
</body>
</html>