我有一个应用程序,带有一个在端口 8080 上运行的简单套接字 io 服务器。
const io = new Server(socketServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: false,
},
transports: ["polling", "websocket"],
pingTimeout: 5000,
pingInterval: 5000,
});
应用程序部署到Azure Web App,我将其部署到Linux 20 LTS(B1计划)。由于它基于 Linux,因此默认情况下启用 Web 套接字支持。
我在端口 8080 上运行服务器,因此我还在配置中传递了 WEBSITES_PORT=8080。但我在日志流中看到的内容很奇怪 - 有应用程序正在运行的日志,但它好像无论如何都不可用,所以我不确定该设置是否正确。下面是日志,我可以看到来自已启动的套接字服务器的消息,但随后一切都停止了(我替换了应用程序名称):
2024-12-13T13:31:03 Startup Request, url: /webssh/socket.io/?EIO=4&transport=polling&t=PF0DioW&sid=U74M1zL11K-SaBJHAAAA, method: POST, type: request, pid: 87,1,5, ScmType: None/home/LogFiles/2024_12_13_lw0sdlwk0002AT_default_docker.log (https://MYAPP-chat.scm.azurewebsites.net/api/vfs/LogFiles/2024_12_13_lw0sdlwk0002AT_default_docker.log)
2024-12-13T14:04:23.950468637Z cd "/home/site/wwwroot"
2024-12-13T14:04:23.950471337Z
2024-12-13T14:04:23.950473737Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2024-12-13T14:04:23.950476437Z if [ -z "$PORT" ]; then
2024-12-13T14:04:23.950479037Z export PORT=8080
2024-12-13T14:04:23.950481837Z fi
2024-12-13T14:04:23.950484237Z
2024-12-13T14:04:23.950486537Z PATH="$PATH:/home/site/wwwroot" node ./server.js
############ WebSocket server for CHAT is running on port 8080 - this comes from the running server ##############
2024-12-13T14:04:29.440177474Z WebSocket server for CHAT is running on port 8080/home/LogFiles/2024_12_13_lw0sdlwk0002AT_default_scm_docker.log (https://MYAPP-chat.scm.azurewebsites.net/api/vfs/LogFiles/2024_12_13_lw0sdlwk0002AT_default_scm_docker.log)
2024-12-13T14:00:13.4133537Z Fri Dec 13 14:00:13 UTC 2024 running .net core
2024-12-13T14:00:15.3614556Z Startup : 02.00.15.293858
2024-12-13T14:00:15.3889868Z Configure Services : 02.00.15.388858
2024-12-13T14:00:16.8018705Z Configure : 02.00.16.801752
2024-12-13T14:00:17.5251302Z Setting Up Routes : 02.00.17.525013
2024-12-13T14:00:18.1064158Z Exiting Configure : 02.00.18.106309
2024-12-13T14:00:18.4782376Z Hosting environment: Production
2024-12-13T14:00:18.4788463Z Content root path: /opt/Kudu
2024-12-13T14:00:18.4795422Z Now listening on: http://0.0.0.0:8181
2024-12-13T14:00:18.4798312Z Application started. Press Ctrl+C to shut down./home/LogFiles/2024_12_13_lw0sdlwk0002AT_docker.log (https://MYAPP-chat.scm.azurewebsites.net/api/vfs/LogFiles/2024_12_13_lw0sdlwk0002AT_docker.log)
2024-12-13T14:04:14.1309786Z Container is terminated. Total time elapsed: 939 ms.
2024-12-13T14:04:14.1313689Z Site container: MYAPP-chat terminated during site startup.
2024-12-13T14:04:14.8327528Z Site: MYAPP-chat stopped.
2024-12-13T14:04:17.534Z INFO - Starting container for site
2024-12-13T14:04:17.536Z INFO - docker run -d --expose=8080 --name MYAPP-chat_0_cd509984 -e WEBSITE_USE_DIAGNOSTIC_SERVER=true -e WEBSITES_PORT=8080 -e WEBSITE_SITE_NAME=MYAPP-chat -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=MYAPP-chat.azurewebsites.net -e WEBSITE_INSTANCE_ID=123 -e HTTP_LOGGING_ENABLED=1 appsvc/node:20-lts_20240819.2.tuxprod REDACTED
2024-12-13T14:04:19.678Z INFO - Initiating warmup request to container MYAPP-chat_0_cd509984 for site MYAPP-chat
2024-12-13T14:07:36.019Z INFO - Waiting for response to warmup request for container MYAPP-chat_0_cd509984. Elapsed time = 196.3403018 sec
2024-12-13T14:08:37.931Z ERROR - Container MYAPP-chat_0_cd509984 for site MYAPP-chat did not start within expected time limit. Elapsed time = 258.2538017 sec
2024-12-13T14:08:37.944Z ERROR - Container MYAPP-chat_0_cd509984 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2024-12-13T14:08:37.987Z INFO - Stopping site MYAPP-chat because it failed during startup.Ending Log Tail of existing logs ---Starting Live Log Stream ---
2024-12-13T14:10:08 No new trace in the past 1 min(s).
2024-12-13T14:11:08 No new trace in the past 2 min(s).
2024-12-13T14:12:08 No new trace in the past 3 min(s).
2024-12-13T14:13:08 No new trace in the past 4 min(s).
2024-12-13T14:14:08 No new trace in the past 5 min(s).
2024-12-13T14:15:08 No new trace in the past 6 min(s).
当然,与客户端连接时出现超时错误。
此页面,并添加了一个带有运行状况检查端点的 Express 应用程序。它就像一个魅力。
const app = express();
const port = process.env.PORT || 8080;
app.use(express.json());
app.get("/health", (req, res) => res.status(200).send("OK"));
const httpServer = createServer(app);
const io = new Server(httpServer, {
// ...
});
这样httpServer
可以同时处理HTTP和WEBSOCKET协议。