【问题标题】:How to use socket.io in Azure Worker Role?如何在 Azure Worker Role 中使用 socket.io?
【发布时间】:2016-09-08 16:38:17
【问题描述】:

我一直在尝试在 Azure Worker Role 中设置套接字服务器,但似乎没有任何效果。我试图让这个简单的教程发挥作用。这是 server.js 文件:

var port = process.env.port || 81;

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)

app.listen(port);
console.log('socket.io server started on port: ' + port + '\n');

function handler (req, res) {
  res.writeHead(200);
  res.end('socket.io server started on port: ' + port + '\n');
}

// io.configure(function () { 
//   io.set("transports", ["xhr-polling"]); 
//   io.set("polling duration", 10); 
// });

io.sockets.on('connection', function (socket) {
  console.log('user connected');

  socket.on('sendMessage', function(data){
    console.log('user sent the message: ' + data.message + '\n');
    socket.emit('helloBack', { message: 'Hello back!' });
  });
});

使用以下客户端:

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8">

    <title>Socket Test</title>
    <meta name="description" content="The azure socket tutorial">


    <script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>
    <script   src="https://code.jquery.com/jquery-3.1.0.min.js"   integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="   crossorigin="anonymous"></script>

    <script type="text/javascript">
        var socket;
        $(document).ready(function () {
            $("#startButton").click(function () {
                $("#returnMessageLabel").empty();
                if (!socket) {
                    socket = io.connect("http://127.0.0.1:80/");
                    socket.on('helloBack', function (data) {
                        $("#returnMessageLabel").text(data.message);
                    });
                }
                socket.emit('sendMessage', { message: 'Hello there!' });
            });
        });  
    </script>
</head>

<body>
    <h1>Click to introduce yourself</h1>
    <button id="startButton">Say Hello</button>
    <label id="returnMessageLabel"></label>
</body>
</html>

它在端口 80 上的 AzureEmulator 中运行良好,但是当我发布角色时,我收到消息“socket.io server started on port: 80”但客户端无法连接以发送 sendMessage。我从https://dzone.com/articles/windows-azure-web-worker-roles得到这个教程

以下是我的 ServiceDefinition.csdef 文件:

<?xml version="1.0" encoding="utf-16"?>
<ServiceDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="dzoned" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WorkerRole name="WorkerRole1">
    <Startup>
      <Task commandLine="setup_worker.cmd &gt; log.txt" executionContext="elevated">
        <Environment>
          <Variable name="EMULATED">
            <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
          </Variable>
          <Variable name="RUNTIMEID" value="node" />
          <Variable name="RUNTIMEURL" value="http://az413943.vo.msecnd.net/node/0.6.20.exe" />
        </Environment>
      </Task>
      <Task commandLine="node.cmd .\startup.js" executionContext="elevated" />
    </Startup>
    <Endpoints>
      <InputEndpoint name="HttpIn" protocol="tcp" port="80" />
      <InputEndpoint name="SocketIn" protocol="http" port="81" />
    </Endpoints>
    <Runtime>
      <Environment>
        <Variable name="PORT">
          <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='HttpIn']/@port" />
        </Variable>
        <Variable name="EMULATED">
          <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
        </Variable>
      </Environment>
      <EntryPoint>
        <ProgramEntryPoint commandLine="node.cmd .\server.js" setReadyOnProcessStart="true" />
      </EntryPoint>
    </Runtime>
  </WorkerRole>
</ServiceDefinition>

【问题讨论】:

  • 如果您在计算机浏览器中打开该客户端 html,则 127.0.0.1:80 不是您想要的地址。你想要 azure worker 的 IP / 端口。
  • 嗯,是的,当我使用 URL 和端口时,它不起作用。我只是在端口 80 上开始消息。我注释掉的 io.configuration 部分似乎也阻止了它。
  • 您是否设置了一个映射外部到内部端口的端点?
  • 不,我不这么认为。我不知道你在说什么。
  • @Brian 端点是工作人员(和 Web)角色的基础,如果您不知道它们是什么,那么快速的 Web 搜索对您来说非常有价值。没有端点,您就没有来自外部世界的通信。

标签: node.js azure socket.io


【解决方案1】:

请尝试在您的云服务中配置工作者角色的端点。在您的云服务项目中打开ServiceDefinition.csdef,在worker角色定义下添加端点配置。

例如

<WorkerRole name="WorkerRole1" vmsize="Small">
    <Endpoints>
      <InputEndpoint name="HttpIn" protocol="http" port="81" />
    </Endpoints>
...
</WorkerRole> 

然后打包您的项目并部署到 Azure。更多信息请参考https://azure.microsoft.com/en-us/documentation/articles/cloud-services-enable-communication-role-instances/

如有任何更新,请随时告诉我。

【讨论】:

  • 添加了该行,但它仍然无法正常工作。我附上了ServiceDefinition.csdef文件的内容。
  • 我最终使用了 Azure Web App
猜你喜欢
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 2015-03-17
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多