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.
21 lines
561 B
21 lines
561 B
const WebSocket = require('ws');
|
|
|
|
// 创建WebSocket服务器实例
|
|
const wss = new WebSocket.Server({ port: 8080 });
|
|
|
|
wss.on('connection', function connection(ws) {
|
|
// 当客户端连接时触发
|
|
|
|
ws.on('message', function incoming(message) {
|
|
// 当服务器接收到客户端发来的消息时触发
|
|
console.log('received: %s', message);
|
|
});
|
|
|
|
// 发送消息到客户端
|
|
ws.send(JSON.stringify({
|
|
type: '2',
|
|
message: 'message',
|
|
}));
|
|
});
|
|
|
|
console.log('WebSocket server is running on ws://localhost:8080');
|