<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>webSocket小Demo</title>
</head>
<body>
<div class="container">
<div>
<input type="text" id="msg">
<button onclick="onOpen()">点击打开连接</button>
<button onclick="sendMsg()">发送报文</button>
<button onclick="onClose()">点击打开连接</button>
</div>
</div>
<script>
var ws;
onOpen()
function sendMsg() {
let msg = document.getElementById('msg').value;
ws.send(msg);
}
function onOpen() {
ws = new WebSocket('ws://localhost:4399');
ws.onopen = () => {
console.log('状态打开...');
}
ws.onmessage = (res) => {
console.log("数据:", res);
};
ws.onclose = () => {
console.log('状态关闭...');
}
}
function onClose() {
ws.close()
}
</script>
</body>
const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({ port: 4399 });
wss.on('connection', function (ws) {
console.log('状态已经链接');
ws.on('message', function (message) {
ws.send('收到数据并返回:' + message);
});
});