Socket.io + Azure 网络套接字问题
Socket.io + Azure web sockets issue
一起开发一款多人国际象棋游戏
我在 Azure 上托管它时遇到问题。我尝试了很多不同的方法,其中提到了一些:
通过添加以下代码强制应用程序仅使用 WebSockets:
io.configure(function() {
io.set('transports', ['websocket']);
});
在 web.config
文件中添加了 <webSocket enabled="false"/>
..
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.
匹配源协议以确保没有 SSL 问题。
io.configure(function() {
io.set('match origin protocol', true);
});
我现在从头开始,因为我认为我的 server-side 部分已损坏,并改为尝试 Socket.io chat example。
- 我按照步骤操作。
- 在 Azure 上创建了一个新的网络应用程序。
- 通过 FileZilla FTP 发布了我的文件。
- 为我的应用程序在 Azure 上启用 Web 套接字(默认情况下禁用)。
还是一样的错误!见下图。
有人吗?我不确定这是 client-side 还是 server-side 问题。它似乎试图 XHR-poll 而不是使用网络套接字..
提前致谢。
我成功了,谢谢你Chris Anderson-MSFT的帮助。
我在使用 FTP 部署时发生的奇怪事情是我的 node_modules
文件夹与我的 package.json
.
中指定的版本不同
我通过将我在 Azure 上的 Web 应用程序连接到本地 Git 存储库并通过 git 部署该应用程序解决了这个问题。这会递归地连接我的包并匹配正确的版本。
我还需要通过指定传输方法强制我的客户端 socket-io 使用网络套接字:
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);
});
我在 Azure 上托管它时遇到问题。我尝试了很多不同的方法,其中提到了一些:
通过添加以下代码强制应用程序仅使用 WebSockets:
io.configure(function() {
io.set('transports', ['websocket']);
});
在 web.config
文件中添加了 <webSocket enabled="false"/>
..
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.
匹配源协议以确保没有 SSL 问题。
io.configure(function() {
io.set('match origin protocol', true);
});
我现在从头开始,因为我认为我的 server-side 部分已损坏,并改为尝试 Socket.io chat example。
- 我按照步骤操作。
- 在 Azure 上创建了一个新的网络应用程序。
- 通过 FileZilla FTP 发布了我的文件。
- 为我的应用程序在 Azure 上启用 Web 套接字(默认情况下禁用)。
还是一样的错误!见下图。
有人吗?我不确定这是 client-side 还是 server-side 问题。它似乎试图 XHR-poll 而不是使用网络套接字..
提前致谢。
我成功了,谢谢你Chris Anderson-MSFT的帮助。
我在使用 FTP 部署时发生的奇怪事情是我的 node_modules
文件夹与我的 package.json
.
我通过将我在 Azure 上的 Web 应用程序连接到本地 Git 存储库并通过 git 部署该应用程序解决了这个问题。这会递归地连接我的包并匹配正确的版本。
我还需要通过指定传输方法强制我的客户端 socket-io 使用网络套接字:
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);
});