node.js - Socket.io + Azure Web 套接字问题

标签 node.js azure socket.io azure-web-app-service

我正在与 NodeJS 一起开发一款多人国际象棋游戏和 socket.IO .

我在 Azure 上托管它时遇到问题..我尝试了许多不同的方法,其中提到了一些:

通过添加以下代码强制应用程序仅使用 WebSocket:

io.configure(function() {
  io.set('transports', ['websocket']);
}); 

已添加<webSocket enabled="false"/>web.config文件..

Note: This disables the IIS WebSockets module, which includes its own implementation of WebSockets and conflicts with Node.js specific WebSocket modules such as Socket.IO. If this line is not present, or is set to true, this may be the reason that the WebSocket transport is not working for your application.

匹配原始协议(protocol)以确保没有 SSL 问题。

io.configure(function() {
  io.set('match origin protocol', true);
});

我现在从头开始,因为我认为我的服务器端部分已损坏,并尝试了 Socket.io chat example相反。

  • 我按照步骤操作。
  • 在 Azure 上创建了新的 Web 应用。
  • 通过 FileZilla FTP 发布了我的文件。
  • 在 Azure 上为我的应用启用 Web 套接字(默认禁用)。

enter image description here


仍然是同样的错误!见下图。

enter image description here

有人吗?我不确定这是客户端问题还是服务器端问题。看起来它正在尝试 XHR 轮询而不是使用网络套接字..

提前致谢。

最佳答案

我成功了,谢谢 Chris Anderson-MSFT寻求您的帮助。

使用 FTP 部署时发生的奇怪事情是我的 node_modules 文件夹与 package.json 中指定的版本不同。

我通过将 Azure 上的 Web 应用程序连接到本地 Git 存储库并通过 git 部署应用程序解决了这个问题。这会递归连接我的包并匹配正确的版本。

我还需要通过指定传输方法强制客户端套接字 io 使用 Web 套接字:

var socket = io({transports:['websocket']});

这就是我的服务器端文件最终的样子:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
app.use(express.static('public'));

app.get('/', function(req, res) {
 res.sendFile(__dirname + '/public/default.html');
});

io.on('connection', function(socket) {
    io.set('transports', ['websocket']);
    console.log('new connection on socket.io');
    socket.on('move', function(msg) {
        socket.broadcast.emit('move', msg);
    });
});

server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

关于node.js - Socket.io + Azure Web 套接字问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439173/

相关文章:

node.js - ExpressJs : Istanbul coverage report with mocha

node.js - AWS lambda 中的 Google Drive API

node.js - Node 应用程序在网络请求后不会终止?

azure - 分配给单个虚拟机的多个网络接口(interface)的 Terraform azure 输出

node.js - 简单对等。 ("signal") 事件重复多次

javascript - 从 NPM( Node 模块)解析 nunjucks 模板?

java - Microsoft Translator API 检索翻译时出错

azure - 为什么直接使用 Azure SendGrid 而不是 SendGrid

javascript - socket.emit 不适用于移动 chrome(但它适用于隐身模式)

java - 从映射对象中获取键和值并将它们发送到不同的服务器