44 lines
1.4 KiB
HTML
44 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
||
|
||
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title></title>
|
||
<script type="text/javascript">
|
||
var socket;
|
||
if (typeof (WebSocket) == "undefined") {
|
||
console.log("很遗憾:您的浏览器不支持WebSocket");
|
||
} else {
|
||
console.log("恭喜您:您的浏览器支持WebSocket");
|
||
|
||
//实现化WebSocket对象
|
||
//指定要连接的服务器地址与端口建立连接
|
||
socket = new WebSocket("ws://10.103.1.8:7119/ws/server");
|
||
//连接打开事件
|
||
socket.onopen = function () {
|
||
console.log("Socket 已打开");
|
||
socket.send("消息发送测试(From Client)");
|
||
};
|
||
//收到消息事件
|
||
socket.onmessage = function (msg) {
|
||
console.log(msg.data);
|
||
};
|
||
//连接关闭事件
|
||
socket.onclose = function () {
|
||
console.log("Socket已关闭");
|
||
};
|
||
//发生了错误事件
|
||
socket.onerror = function () {
|
||
alert("Socket发生了错误");
|
||
}
|
||
//窗口关闭时,关闭连接
|
||
window.unload = function () {
|
||
socket.close();
|
||
};
|
||
}
|
||
</script>
|
||
</head>
|
||
<body>
|
||
|
||
</body>
|
||
</html> |