From cba46b77c3c44ab00153bfcb89c482a75b69275b Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Mon, 17 Apr 2023 09:53:26 +0800 Subject: [PATCH] 123 --- .env | 6 +++++- package.json | 3 ++- src/main.js | 33 ++++++++++++++++++++++++++++++--- src/websocket/client.js | 17 +++++++++++++++++ yarn.lock | 5 +++++ 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/websocket/client.js diff --git a/.env b/.env index 36a480e..4f7e988 100644 --- a/.env +++ b/.env @@ -18,4 +18,8 @@ MYSQL_PWD = root MYSQL_DB = chicken_breeding # salt -JWT_SECRET = 'eyJ0e2223XAiOiJKV1Q33iLCJhbGciOiJsdJ9.eyJqd4GkiOiJjYjc4M2I5Yi1kMGYxLTRjMWEtYmQyMC0zOTM' \ No newline at end of file +JWT_SECRET = 'eyJ0e2223XAiOiJKV1Q33iLCJhbGciOiJsdJ9.eyJqd4GkiOiJjYjc4M2I5Yi1kMGYxLTRjMWEtYmQyMC0zOTM' + +# websocket +WEB_SOCKET_PORT = 4000 +WEB_SOCKET_SERVER_HOST = 127.0.0.1 \ No newline at end of file diff --git a/package.json b/package.json index ab8d443..c155896 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "moment": "^2.29.4", "mysql2": "^3.2.0", "sequelize": "^6.29.1", - "uuid": "^9.0.0" + "uuid": "^9.0.0", + "ws": "^8.13.0" }, "devDependencies": { "nodemon": "^2.0.21" diff --git a/src/main.js b/src/main.js index 1197a68..5d43cbd 100644 --- a/src/main.js +++ b/src/main.js @@ -2,15 +2,42 @@ const { APP_PORT, TCP_PORT, TCP_SERVER_HOST, + WEB_SOCKET_PORT, + WEB_SOCKET_SERVER_HOST, } = require("./config/config.default"); +const WebSocket = require("ws"); const app = require("./app/"); const server = require("./tcp/server"); app.listen(APP_PORT, () => { - console.log(`app server is running on http://localhost:${APP_PORT}`); + console.log(`nodejs server is running on http://localhost:${APP_PORT}`); }); -server.listen(TCP_PORT, TCP_SERVER_HOST, () => { - console.log(`tcp server is running on http://localhost:${TCP_PORT}`); +// server.listen(TCP_PORT, TCP_SERVER_HOST, () => { +// console.log(`tcp server is running on http://localhost:${TCP_PORT}`); +// }); + +// websocket server +const WebSocketServer = WebSocket.Server; + +//在4000端口上打开了一个WebSocket Server,该实例由变量wss引用。 +const wss = new WebSocketServer({ + port: WEB_SOCKET_PORT, +}); + +wss.on("connection", function (ws) { + //在connection事件中,回调函数会传入一个WebSocket的实例,表示这个WebSocket连接。 + console.log( + `websocket server is connected on ws:/${WEB_SOCKET_SERVER_HOST}:${WEB_SOCKET_PORT}` + ); + ws.on("message", function (message) { + console.log(`[SERVER] Received:${message}`); + ws.send(`server received : ${message}`, (err) => { + // 对接收来的消息进行处理分发 + if (err) { + console.log(`[SERVER] error:${err}`); + } + }); + }); }); diff --git a/src/websocket/client.js b/src/websocket/client.js new file mode 100644 index 0000000..125f4fb --- /dev/null +++ b/src/websocket/client.js @@ -0,0 +1,17 @@ +const WebSocket = require("ws"); +let ws = new WebSocket("ws://localhost:4000"); + +// 打开WebSocket连接后立刻发送一条消息: +ws.on("open", function () { + console.log(`[CLIENT] open()`); + ws.send( + JSON.stringify({ + a: 1, + }) + ); +}); + +// 响应收到的消息: +ws.on("message", function (message) { + console.log(`[CLIENT] Received: ${message}`); +}); diff --git a/yarn.lock b/yarn.lock index 240c7fe..5c90d5f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1043,6 +1043,11 @@ wrappy@1: resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ== +ws@^8.13.0: + version "8.13.0" + resolved "https://registry.npmmirror.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0" + integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA== + yallist@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"