Ad

Running Node.js On A Website But Not On Localhost

I made an online multiplayer game. I works perfectly when I run it with node.js command prompt on localhost:3000. But when I try to run it on the website it is not doing what my app.js file says it to do. my questions are;

How can I make my node.js project run on my website rather than on localhost? What will the port be instead of 3000? Can I do this by uploading some file into my website via ftp?

Here is my app.js file

var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

var usernumm = 0;
var usernum1 = [];

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

server.listen(3000);

app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket){


var endpoint = socket.manager.handshaken[socket.id].address;
console.log('***New connection from ' + endpoint.address + ':' +     endpoint.port);

usernumm++;
io.sockets.emit('usernum', usernumm);
usernum1[usernumm] =  endpoint.port;
console.log('usernum'+usernumm+'geldi'+findusernum());


socket.on('button1socket', function(){

    io.sockets.emit('button1f', findusernum() );
    console.log('user '+findusernum()+' pressed a button');

});
socket.on('buttonclickable', function(){

    io.sockets.emit('buttonclickable1', findusernum() );

});

socket.on('disconnect', function () {
    usernumm--;
    io.sockets.emit('usernum', usernumm);
    //sockets[usernum] = socket.port;
    console.log('***Client disconnected');
});


//finds number of online users
function findusernum(){
    for(var i = 0; i<9;i++){
        if(usernum1[i] == endpoint.port){return i;}else{}
        }
}


});
Ad

Answer

try:

var express = require('express');
var app = express();
var httpServer = require('http').Server(app);
var socketServer = require('socket.io')(httpServer);

var ip = 'iphere';
var port = 80;

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

app.get('/', function(req, res){
    res.sendfile(__dirname + '/index.html');
});

socketServer.on('connection', function(socket){
    console.log("A Client has connected.");
    socket.on('disconnect', function(){
        console.log("A Client has disconnected.");
    });
});

httpServer.listen(port, ip, function(){
    console.log("Listening to "+ip+":"+port);
});

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script type="text/javascript" src="http://ip:port/socket.io/socket.io.js"></script>
    <script type="text/javascript">
        var socket;
        try{
            socket = io("http://ip:port/", {'forceNew':true });
            socket.on('connect', function(error){
                if(error){

                }
            });
        }catch(e){

        }
    </script>
</head>
<body>

</body>
</html>

after you have specified your ip and port

port forward the port you have specified to live your website/game using your router

then you can visit it with http://yourpublicip:port/

if its port 80 then visit the page without the port.

Ad
source: stackoverflow.com
Ad