这是一个demo,常常让我用来测试这个 WebSocket 是否能正常连接:

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
<html>
<head>
<title>socket测试</title>
<meta charset="utf-8">
</head>
<style>
#ci{border:1px solid blue;}
</style>
<body>
<div id="ci"></div>
</body>

<script type="text/javascript">
var wsServer = "ws://***.**.**.**:****"; // 这里填写WebSocket地址和端口号
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log(evt)
console.log("开始连接.");
};

websocket.onclose = function (evt) {
console.log(evt)
console.log("连接关闭");
};

websocket.onmessage = function (evt) {
console.log(evt)
console.log('接收数据: ' + evt.data);
};

websocket.onerror = function (evt, e) {
console.log(evt)
console.log('连接错误: ' + evt.data);
};
</script>
</html>