我创建了一个使用Java,Socket.IO和NodeJS服务器的简单游戏。这个游戏在我的本地机器上托管时效果很好。
我在Java游戏客户端托管服务器时无法连接到服务器。
Heroku是否阻止来自桌面客户端的连接?
非常感谢任何帮助:)
Java游戏客户端连接
public void connectSocket(){
try{
socket = IO.socket("https://galaga1v1.herokuapp.com/");
socket.connect();
}catch (Exception e){
System.out.println(e);
}
}
服务器正在运行
Heroku记录
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...
服务器代码
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;
}