【问题标题】:Deploying node.js + socket.io backend/server on azure在 azure 上部署 node.js + socket.io 后端/服务器
【发布时间】:2017-03-14 10:54:06
【问题描述】:

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

游戏会在安卓手机上玩,我是用unity游戏引擎制作的。

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

【问题讨论】:

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


    【解决方案1】:

    希望以下内容有所帮助。

    1) 在 Azure Web 应用程序中,只有端口 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);
    

    【讨论】:

    • 是的! , 现在正在运行,非常感谢 Aaron 的帮助!
    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2021-10-27
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多