简体   繁体   English

Azure上具有node.js的Websocket服务器

[英]Websocket server on Azure with node.js

I'm trying to create a websocket server with Node.js to run on a Windows Azure. 我正在尝试使用Node.js创建一个Websocket服务器以在Windows Azure上运行。 This should be simple, but I have run into problems I haven't been able to find solutions for anywhere. 这应该很简单,但是我遇到了无法在任何地方找到解决方案的问题。

This is my serverside code: 这是我的服务器端代码:

var ws = require('websocket.io')
    , http = require('http').createServer().listen(process.env.PORT)
    , server = ws.attach(http)

server.on('connection', function (socket) {
    socket.on('message', function () { });
    socket.on('close', function () { });
});

How do I know which port I need to connect to? 我怎么知道我需要连接哪个端口? My client code is: 我的客户代码是:

var ws = new WebSocket("ws://socketiopuge.azurewebsites.net");

ws.onopen = function () {
    alert("opened");
};

ws.onmessage = function(msg) {
    alert(msg);
};

ws.onclose = function () {
    alert("closed");
};

When I run the code I get an 501 error code, and the onclode event is fired. 当我运行代码时,我得到501错误代码,并且触发了onclode事件。 I believe the problem is that I need to specify a port number when I create the WebSocket. 我相信问题是创建WebSocket时需要指定端口号。 Can any of you point me in the right direction? 你们中的任何人都能指出我正确的方向吗? Thanks! 谢谢!

About your question "How do I know which port I need to connect to", you would need to create an Input Endpoint and set proper PORT for it. 关于您的问题“我怎么知道我需要连接到哪个端口”,您需要创建一个输入端点并为其设置适当的PORT。 Once you configured it, you will be using the same port in your code to bind and use. 配置完毕后,您将在代码中使用相同的端口进行绑定和使用。

Here is an example about Running socket.io on Windows Azure Web and Worker roles 这是有关在Windows Azure Web和Worker角色上运行socket.io的示例

If you host socket.io in a Windows Azure Web Role, please disable the WebSockets transport on the server because Windows Azure Web role runs on IIS7, web sockets are not supported on IIS7 yet. 如果您在Windows Azure Web角色中托管socket.io,请禁用服务器上的WebSockets传输,因为Windows Azure Web角色在IIS7上运行,而IIS7尚不支持Web套接字。 With Worker Role you dont need to worry about it and you can directly use Web Sockets. 使用工作者角色,您无需担心,可以直接使用Web套接字。

If you're using the new Azure web sites, they do not support websockets yet. 如果您使用的是新的Azure网站,则它们尚不支持websocket。 The same goes for all web sites hosted through IIS - node.js or not. 对于通过IIS托管的所有网站(是否为node.js)也是如此。

Your only way of currently supporting websockets in Azure is by using worker roles and supplying your own node.js executable. 当前在Azure中支持websocket的唯一方法是使用工作人员角色并提供您自己的node.js可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM