为什么firefox连接后不发出(客户端)

时间:2018-03-21 14:36:42

标签: javascript node.js socket.io

我的游戏适用于其他浏览器,但使用Firefox,有一个小但很重要的问题。为什么firefox不会发出" new-player"套接字连接后的消息,其他浏览器工作正常,所有浏览器输出HELLO到控制台......并自行注册。

const newPlayer = (socket, player) => {
  socket.on('connect', () => {

  console.log("HELLO this row appears in all browsers"); 
    socket.emit('new-player', { 
      x: player.sprite.body.x,
      y: player.sprite.body.y,
      angle: player.sprite.rotation,
      playerName: {
        name: String(socket.id),
        x: player.playerName.x,
        y: player.playerName.y
      },
      speed: {
        value: player.speed,
        x: player.speed.x,
        y: player.speed.y
      }
    })
  })
}

export default newPlayer
服务器端的问题:firefox没有收到新玩家消息,其他浏览器没问题:

io.on('connection', socket => {   // When a player connects   
    socket.on('new-player', state => {
    console.log(socket.id+'New player joined with state:', state)
    players[socket.id] = state  //stones[socket.id] = state
    // Emit the update-players method in the client side
    io.emit('update-players', players)  //io.emit('update-stones', stones)   })

  //   note the following invalid move command  occurs  on firefox see logs
 socket.on('move-player', data => {
    // If the player is invalid, return
    if (players[socket.id] === undefined) {
        console.log(socket.id+' invalid move')
      return
    }

使用firefox的服务器输出:

> [nodemon] 1.14.10 [nodemon] to restart at any time, enter `rs`
> [nodemon] watching: *.* [nodemon] starting `babel-node
> server/index.js` Game server running on: 8000 O14Q_E5LXJi3oYmGAAAA
> invalid move O14Q_E5LXJi3oYmGAAAA invalid move O14Q_E5LXJi3oYmGAAAA
> invalid move O14Q_E5LXJi3oYmGAAAA invalid move O14Q_E5LXJi3oYmGAAAA
> invalid move

使用CHROME浏览器输出服务器或重新启动服务器(当firefox保持打开状态时,屏幕上的游戏)

> nodemon server/index.js --exec babel-node

[nodemon] 1.14.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node server/index.js`
Game server running on: 8000
55e5xgecEJ-QX_MqAAAANew player joined with state: { x: 2164.6852111816406,
  y: 298.809814453125,
  angle: 0,
  playerName:
   { name: '55e5xgecEJ-QX_MqAAAA',
     x: 2162.938232421875,
     y: 298.80773544311523 },
  speed: { value: 105 } }

1 个答案:

答案 0 :(得分:0)

似乎这使它起作用

const newPlayer = (socket, player) => {
  socket.on('connect', () => {

  console.log("do nothing here actually, just log "+socket);
})

    socket.emit('new-player', { 
      x: player.sprite.body.x,
      y: player.sprite.body.y,
      angle: player.sprite.rotation,
      playerName: {
        name: String(socket.id),
        x: player.playerName.x,
        y: player.playerName.y
      },
      speed: {
        value: player.speed,
        x: player.speed.x,
        y: player.speed.y
      }
    })

}

export default newPlayer