You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<button onclick="sendMessage()">发送消息</button>
</div>
<script>
var socket;
//首先判断是否支持WebSocket
if (window.WebSocket) {
socket = new WebSocket("ws://127.0.0.1:9000/FengHuang/mysocket.ws");
//接受消息后的操作
socket.onmessage = function (event) {
message(event.data);
};
//打开连接后的操作
socket.onopen = function (event) {
console.log(event);
};
//关闭连接后的操作
socket.onclose = function (event) {
console.log(event);
};
//发生异常时的操作
socket.onerror = function (event) {
console.log(event);
};
} else {
alert("NO");
}
function message(data) {
console.log(data)
}
//向服务器发送消息
function sendMessage() {
socket.send('Hello world!!!')
}
</script>
</body>
</html>