Sunday, 9 April 2017

Socket get net::ERR_CONNECTION_TIMED_OUT when deploy nodejs webapp on iisnode

I am new to nodeJs.Forgive me for asking some silly question.

I am trying to deploy a real-time chatroom webapp on Azure server with iisnode. The webapp works well on localhost, but when I upload it to run on the server, the socket cannot connect from client to server. I am using 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 });
    };

}

when i try to request the url, it gives me error:

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

Can anyone help me? Thanks in advance.



via Janice Zhan

No comments:

Post a Comment