From ac429e4cbf740b2c6e4dd20a890d7300033917e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Fri, 5 Apr 2024 14:51:20 +0800 Subject: [PATCH] =?UTF-8?q?tools=20=E6=B7=BB=E5=8A=A0=20websocket=20server?= =?UTF-8?q?=20(nodejs=20&=20python)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 29 ++++++++++++++- package.json | 3 +- .../server-client/websocket-server-nginx.conf | 21 +++++++++++ tools/server-client/websocket-server.js | 25 +++++++++++++ tools/server-client/websocket-server.py | 36 +++++++++++++++++++ 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 tools/server-client/websocket-server-nginx.conf create mode 100644 tools/server-client/websocket-server.js create mode 100644 tools/server-client/websocket-server.py diff --git a/package-lock.json b/package-lock.json index c3da6a8..f230206 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,8 @@ "path": "^0.12.7", "play-sound": "^1.1.5", "request": "^2.88.2", - "solarlunar": "^2.0.7" + "solarlunar": "^2.0.7", + "ws": "^8.16.0" } }, "node_modules/@tokenizer/token": { @@ -2468,6 +2469,26 @@ "node": ">=10" } }, + "node_modules/ws": { + "version": "8.16.0", + "resolved": "https://registry.npmmirror.com/ws/-/ws-8.16.0.tgz", + "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xregexp": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/xregexp/-/xregexp-2.0.0.tgz", @@ -4441,6 +4462,12 @@ "strip-ansi": "^6.0.0" } }, + "ws": { + "version": "8.16.0", + "resolved": "https://registry.npmmirror.com/ws/-/ws-8.16.0.tgz", + "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", + "requires": {} + }, "xregexp": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/xregexp/-/xregexp-2.0.0.tgz", diff --git a/package.json b/package.json index e724ce3..78bb73c 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "path": "^0.12.7", "play-sound": "^1.1.5", "request": "^2.88.2", - "solarlunar": "^2.0.7" + "solarlunar": "^2.0.7", + "ws": "^8.16.0" } } diff --git a/tools/server-client/websocket-server-nginx.conf b/tools/server-client/websocket-server-nginx.conf new file mode 100644 index 0000000..6862e47 --- /dev/null +++ b/tools/server-client/websocket-server-nginx.conf @@ -0,0 +1,21 @@ + +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + server { + listen 80; + server_name localhost; + + location /ws { + proxy_pass http://localhost:5003; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + } + + } +} diff --git a/tools/server-client/websocket-server.js b/tools/server-client/websocket-server.js new file mode 100644 index 0000000..b137995 --- /dev/null +++ b/tools/server-client/websocket-server.js @@ -0,0 +1,25 @@ +/** + * WebSocket Server + */ +const WebSocket = require('ws'); + +// 创建 WebSocket 服务器 +const wss = new WebSocket.Server({ port: 8080 }); + +// 客户端连接时触发 +wss.on('connection', (ws) => { + console.log('客户端已连接'); + + // 每隔 5 秒发送一条消息给客户端 + const interval = setInterval(() => { + if (ws.readyState === WebSocket.OPEN) { + ws.send('这是来自服务器的定时消息!'); + } + }, 1000); + + // 客户端断开连接时触发 + ws.on('close', () => { + console.log('客户端已断开连接'); + clearInterval(interval); + }); +}); diff --git a/tools/server-client/websocket-server.py b/tools/server-client/websocket-server.py new file mode 100644 index 0000000..8b8200b --- /dev/null +++ b/tools/server-client/websocket-server.py @@ -0,0 +1,36 @@ +# +# WebSocket Server +# pip install websockets +# +import asyncio +import websockets + +# Define the WebSocket server logic +async def websocket_server_logic(websocket, path): + print('客户端已连接') + + # Function to send a message every 5 seconds + async def send_message_every_5_seconds(): + while True: + await asyncio.sleep(5) + if websocket.open: + await websocket.send('这是来自服务器的定时消息!') + else: + break + + # Start sending messages every 5 seconds + send_task = asyncio.create_task(send_message_every_5_seconds()) + + # Wait for the client to disconnect + try: + await websocket.wait_closed() + finally: + send_task.cancel() + print('客户端已断开连接') + +# Start the WebSocket server +start_server = websockets.serve(websocket_server_logic, "localhost", 8080) + +# Run the server until it is stopped +asyncio.get_event_loop().run_until_complete(start_server) +asyncio.get_event_loop().run_forever()