简体   繁体   中英

Websocket server on Azure with node.js

I'm trying to create a websocket server with Node.js to run on a 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. I believe the problem is that I need to specify a port number when I create the 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. 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

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. With Worker Role you dont need to worry about it and you can directly use Web Sockets.

If you're using the new Azure web sites, they do not support websockets yet. The same goes for all web sites hosted through IIS - node.js or not.

Your only way of currently supporting websockets in Azure is by using worker roles and supplying your own node.js executable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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