Nodejs socket.io 与 azure 应用程序服务的连接

问题描述 投票:0回答:1

我在连接到部署在 Azure Web 应用程序上的 Socket.IO 服务器时遇到问题。以下是我的设置和我面临的问题的概述:

设置:

我有一个 Node.js 应用程序部署到 Azure Web 应用程序。 该应用程序包括一个 Socket.IO 服务器,配置为侦听自定义端口 (8444)。 我已设置 CORS 以允许来自特定域的连接。 服务器代码如下所示:

const { createServer } = require('http');
const { Server } = require('socket.io');
const express = require('express');

const app = express();
const httpServer = createServer(app);

const io = new Server(httpServer, {
  serveClient: false,
  path: '/api/socket/',
  cors: {
    origin: [
      'http://localhost:3000',

    ]
  },
});

const port = process.env.PORT || 8444;

httpServer.listen(port, () => {
  console.log(`Socket.IO server listening on port ${port}`);
});

问题: 尽管服务器运行没有错误,但我无法从客户端应用程序建立 WebSocket 连接。

我尝试了各种故障排除步骤,包括检查网络配置并确保端口可访问,但仍无法解决问题。

任何有关可能导致问题的原因的建议或见解将不胜感激。预先感谢您的帮助!

socket.io azure-web-app-service
1个回答
0
投票

我尝试将带有 Socket.IO 的 Node.js 应用程序部署到 Azure 应用程序服务(linux),没有任何问题。这是我的项目结构:

node_modules/
public/
  └── index.html
package-lock.json
package.json
server.js

我对你的 server.js 文件做了一些更改

我从您的代码中删除了这些行,并且能够获得成功的输出:

serveClient: false,
  path: '/api/socket/',

这些选项用于自定义 Socket.IO 如何为其客户端库提供服务并设置 WebSocket 连接的路径。但是,它们是可选的,如果它们引起问题,我们可以将其删除。

这是修改后的Server.js

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path'); 
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
  cors: {
    origin: [
      'http://localhost:3000',
    ]
  }
});
const PORT = process.env.PORT || 8445; 
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'public/index.html'));
});
io.on('connection', (socket) => {
  console.log('A user connected');
  socket.on('chat message', (msg) => {
    console.log('message: ' + msg);

    io.emit('chat message', msg);
  });
  socket.on('disconnect', () => {
    console.log('User disconnected');
  });
});
server.listen(PORT, () => {
  console.log(`Socket.IO server listening on port ${PORT}`);
});

这是我的index.html:

<!DOCTYPE  html>
<html  lang="en">
<head>
<meta  charset="UTF-8">
<meta  name="viewport"  content="width=device-width, initial-scale=1.0">
<title>Socket.IO Chat</title>
</head>
<body>
<ul  id="messages"></ul>
<form  id="form"  action=""
<input  id="input"  autocomplete="off"  /><button>Send</button>
</form>
<script  src="http://localhost:8445/socket.io/socket.io.js"></script>
<script>
var  socket = io();
var  form = document.getElementById('form');
var  input = document.getElementById('input');
form.addEventListener('submit', function(e) {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});
socket.on('chat message', function(msg) {
var  item = document.createElement('li');
item.textContent = msg;
document.getElementById('messages').appendChild(item);
});
</script>
</body>
</html>

本地输出:

enter image description here

enter image description here

这是部署后的输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.