1
0
Fork 0

tools 添加 websocket server (nodejs & python)

This commit is contained in:
程序员小墨 2024-04-05 14:51:20 +08:00
parent 4f4917a940
commit ac429e4cbf
5 changed files with 112 additions and 2 deletions

29
package-lock.json generated
View File

@ -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",

View File

@ -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"
}
}

View File

@ -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';
}
}
}

View File

@ -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);
});
});

View File

@ -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()