简体   繁体   中英

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{}
        }
}


});

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.

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