0

I have created a simple game that uses Java, Socket.IO, and a NodeJS server. This game works perfectly when hosted on my local machine.

I am having trouble connecting to the server from the Java game client when it's hosted on heroku .

Does Heroku block connections from desktop clients?

Any help is very much appreciated :)

Java game client connection

public void connectSocket(){

    try{
        socket = IO.socket("https://galaga1v1.herokuapp.com/");
        socket.connect();

    }catch (Exception e){

        System.out.println(e);
    }

}

The server is running

Heroku logs

2016-08-06T20:27:28.433806+00:00 heroku[web.1]: State changed from down to starting
2016-08-06T20:27:29.471418+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-08-06T20:27:31.181351+00:00 heroku[web.1]: State changed from starting to up
2016-08-06T20:27:31.131273+00:00 app[web.1]: Server is now running...

Server code

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var players = [];

server.listen(process.env.PORT || 8080, function(){

console.log("Server is now running..." + String(process.env.PORT) );

});

io.on('connection', function(socket){
console.log("Player Connected!");
socket.emit('socketID', { id: socket.id });
socket.emit('getPlayers', players);
socket.broadcast.emit('newPlayer', {id: socket.id});
socket.on('playerMoved', function(data){
    data.id = socket.id;
    socket.broadcast.emit('playerMoved', data);

    for(var i = 0; i < players.length; i++){

        if(players[i].id == data.id){
            players[i].x = data.x;
        }

    }
});

socket.on('playerShoot', function(data){
    data.id = socket.id;
    socket.broadcast.emit('playerShoot', data);

});

socket.on('playerHit',function(data){

    data.id = socket.id;
    socket.broadcast.emit('playerHit', data);
    console.log("player has been hit");


});

socket.on('disconnect', function(){
    console.log("Player Disconnected!");
    socket.broadcast.emit('playerDisconnected', {id: socket.id });
    for(var i = 0; i < players.length; i++){
        if(players[i].id == socket.id){
            players.splice(i,1);
        }
    }
});
players.push(new player(socket.id, 0));
});

function player(id, x){

this.x = x;
this.id = id;

}
6
  • 1
    What's the problem? Commented Aug 6, 2016 at 20:59
  • My game client isn't communicating with the server, but the server is running.
    – LaurentL
    Commented Aug 6, 2016 at 21:01
  • 1
    Does it give an error or anything? Or fail silently? What exactly is going on? Commented Aug 6, 2016 at 21:03
  • I guess it's failing silently. My game client normally has a socket connection with the server and they communicate back and forth sending data. But now the server doesn't seem to be receiving anything from the game client. (This works perfectly when hosted on localhost)
    – LaurentL
    Commented Aug 6, 2016 at 21:06
  • I suggest putting some logging statements around the socket.connect() call to find out the status of the connection (if there is no exception)
    – codefinger
    Commented Aug 7, 2016 at 14:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.