在azure上部署node.js + socket.io后端/服务器

时间:2017-03-13 21:59:22

标签: javascript node.js azure socket.io azure-web-sites

D:\home\LogFiles\Application\logging-errors.txt中的网络服务上运行我的服务器时出现以下错误:

Mon Mar 13 2017 21:36:58 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
TypeError: server.listeners is not a function
    at Server.attach (D:\home\site\wwwroot\node_modules\socket.io\node_modules\engine.io\lib\server.js:424:26)
    at Function.attach (D:\home\site\wwwroot\node_modules\socket.io\node_modules\engine.io\lib\engine.io.js:124:10)
    at Server.listen.Server.attach (D:\home\site\wwwroot\node_modules\socket.io\lib\index.js:240:21)
    at new Server (D:\home\site\wwwroot\node_modules\socket.io\lib\index.js:52:17)
    at Server (D:\home\site\wwwroot\node_modules\socket.io\lib\index.js:40:41)
    at Object.<anonymous> (D:\home\site\wwwroot\server.js:1:92)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)

这是我的服务器端代码(开头):

var io = require('socket.io')(process.env.PORT || 3000);
var AI = require('./AI');
var gamelogic = require('./gamelogic');
var shortid = require('shortid');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

io.on('connection', function(socket){
        socket.emit("Testing");

在客户端(C#,带有socket.io模块):

socket.On ("Testing", Test);

public void Test(SocketIOEvent e)
{
    Debug.Log ("Server is running and communicating");

}

这是我到服务器的连接字符串:

ws://MyAppname.azurewebsites.net:3000/socket.io/?EIO=4&transport=websocket

游戏将在Android手机上播放,我用统一游戏引擎制作游戏。

当我在本地运行服务器时,它运行完美!多个实例连接到服务器和连接房间,ai正在播放,分数正在更新等。但我不能让它在服务器端进行通信/运行。

1 个答案:

答案 0 :(得分:1)

希望以下内容有所帮助。

1)在Azure Web Apps中,只有端口80和443面向公众。所以你的连接字符串应该类似于:

ws://MyAppname.azurewebsites.net/socket.io/?EIO=4&transport=websocket

2)Socket.IO使用WebSockets,Azure上默认不启用它。要启用网络套接字,请按照my earlier post

中的步骤操作

3)用以下代码行替换行var io = require('socket.io')(process.env.PORT || 3000);

var server = require('http').createServer((req, res) => res.end());
var io = require('socket.io')(server);
server.listen(process.env.PORT || 3000);