在iisnode上部署nodejs webapp时,Socket get net :: ERR_CONNECTION_TIMED_OUT

时间:2017-04-10 04:29:10

标签: node.js azure express azure-virtual-machine iisnode

我是nodeJs.的新手。感谢我提出一些愚蠢的问题。

我正在尝试使用iisnode在Azure服务器上部署实时聊天室webapp。 webapp在localhost上运行良好,但是当我上传它以在服务器上运行时,套接字无法从客户端连接到服务器。我正在使用nodejs + express + jade。

server.js
var express = require("express"), http = require('http');
var app = express();
var server = http.createServer(app);
var port  = process.env.PORT || 3700;
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");

app.engine('jade', require('jade').__express);

var deployPath = process.env.deployPath || "";

app.get(deployPath+ "/", function(req, res){
    res.render("page",{deployPath: deployPath});
});

app.use(deployPath, express.static(__dirname + '/public'));


var io = require('socket.io')(server);
server.listen(port, 'http://visafe-paltform.cloudapp.net');
io.sockets.on('connection', function (socket) {
    console("connected");
    socket.emit('message', { message: 'welcome to the chat' });
    socket.on('send', function (data) {
        io.sockets.emit('message', data);
    });
});

console.log("Listening on port " + port);


Client.js


window.onload = function() {

    var messages = [];
    var address = window.location.protocol + '//' + window.location.host; 
    console.log(address);
    var details = {  
        resource: (window.location.pathname.split('/').slice(0, -1).join('/') + '/socket.io').substring(1)  
    };  
    console.log(details);

    //var socket = io.connect(address, details);
    var socket = io.connect('http://visafe-paltform.cloudapp.net:3700');
    //var socket = io.connect();
    var field = document.getElementById("field");
    var sendButton = document.getElementById("send");
    var content = document.getElementById("content");

    socket.on('message', function (data) {
        if(data.message) {
            messages.push(data.message);
            var html = '';
            for(var i=0; i<messages.length; i++) {
                html += messages[i] + '<br />';
            }
            content.innerHTML = html;
        } else {
            console.log("There is a problem:", data);
        }
    });

    sendButton.onclick = function() {
        var text = field.value;
        console.log("click:", field.value);
        socket.emit('send', { message: text });
    };

}

当我尝试请求网址时,它会给我错误:

socket.io.js:4948 GET http://visafe-paltform.cloudapp.net:3700/socket.io/?EIO=3&transport=polling&t=LjM71Yc net::ERR_CONNECTION_TIMED_OUT

任何人都可以帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

对于经典VM,您应该在Azure Endpoint和Windows防火墙上打开端口3700。

有关如何在端点上打开端口的更多信息,请参阅此link

如果您想使用公共IP访问您的服务,请确保您的服务正在0.0.0.0上收听,并且您可以在您的虚拟机上telnet 127.0.0.1 3700成功。