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