node.js - 一段时间后Socket IO超时

时间:2015-09-23 19:59:10

标签: socket.io

我试图为我自己的多人游戏构建一个socket.io服务器,但由于某种原因,服务器在一段时间后停机而且我不知道为什么。我已经尝试了几种方法来运行服务器(nodemon和永远,有或没有屏幕的一切)。我不认为这是一个不活动的问题,因为我添加了一个随机的东西生成器来模拟服务器上的一些活动,但问题仍然存在。运行服务器的CPU负载保持在2-3%之间。我正在运行节点4.x和当前稳定的socket.io build(1.3.6)。

这是我的代码:

var shortId = require('shortid'),
    io = require('socket.io')(process.env.PORT || 4567),
    mysql = require('mysql'),
    connection = mysql.createConnection({
        host: 'localhost',
        user: 'xxx',
        password: 'xxx',
        database: 'xxx'
    });

var clients = [];
var clientLookup = {};

//Placed Components (builded stuff from players or enviroment)
var placed_components = 'Server1_PlacedC';


connection.connect(function (err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

});


setInterval(function () {
  var random = Math.random();
  //connection.query(
  //    'INSERT INTO '+placed_components+' SET ?',
  //    {data:countdown},
  //    function(err,result){
  //        if (err) throw err;
  //    }
  //);

  console.log(random);

}, 100);


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

  var currentClient;
  console.log('connected', socket.id);

  //////////////////////////////////////////////////////////////////////////////  
  socket.on('disconnect', function () {
    console.log('player disconnected', currentClient.id);
    socket.broadcast.emit('disconnected', currentClient)
    var index = clientLookup[currentClient.id];
    clients.splice(index, 1);
  })
  /////////////////////////////////////////////////////////////////////////////    /

  socket.on('register', function (data) {
    currentClient = {
      id: shortId.generate(),
      health: 100,
      isDead: false
    };

    console.log('registering', currentClient.id);
    clients.push(currentClient);
    clientLookup[currentClient.id] = currentClient;

    socket.emit('registerSuccess', {id: currentClient.id});
    socket.broadcast.emit('spawn', {id: currentClient.id});
    console.log('connected players', clients.length);

    clients.forEach(function (client) {
      if (currentClient.id == client.id)
        return;

      socket.emit('spawn', {id: client.id});
      console.log('sending spawn to new player for playerid', client.id);
    })

  });

  socket.on('beep', function () { // Beep Request
    socket.emit('boop');
    console.log("received some beep!");
  });

  socket.on('move', function (data) {
    data.id = currentClient.id;
    socket.broadcast.emit('move', data);
    //console.log(JSON.stringify(data));

  });

  socket.on('ShareObject', function (data) {
    data.id = currentClient.id;
    socket.broadcast.emit('ReveiveObject', data);
    console.log(JSON.stringify(data));
  });

  socket.on('SharePlayerAnimation', function (data) {
    data.id = currentClient.id;
    socket.broadcast.emit('BroadcastPlayerAnimation', data);
    console.log("a Player changed his animation" + JSON.stringify(data));
  });
  //////////////////////////////////////////////////////////////////////////////

  socket.on('benchmark', function (data) {
    console.log(data);
  });
  //////////////////////////////////////////////////////////////////////////////


})


console.log('server started');

0 个答案:

没有答案