[英]io.emit fails to emit to all clients
I am trying to implement a "leave game" feature in a simple socket.io game and I cannot figure out why io.emit
notifies only the socket of the client leaving the game. 我试图在一个简单的socket.io游戏中实现“离开游戏”功能,但我不明白为什么
io.emit
仅通知离开游戏的客户端套接字。 Here is my socket.js
code: 这是我的
socket.js
代码:
io.on("connection", sock => {
sock.on('joinGame', name => {
inc++
if(name === 'guest') name = name + inc.toString()
addToGame(inc, name) // adds player to a new Map()
io.emit('joinedGame', name)
})
sock.on('findPlayersInGame', () => {
getAllPlayersInGame(io, threeOrMore)
// check to see if the client is notified when a new user joins
io.emit('newPlayerJoined', 'new player joined')
})
sock.on('leaveGame', name => {
io.emit('leftGame', uniquePlayers)
})
On the client, I am handling the socket communication along with my state management in a MobX store. 在客户端上,我正在MobX存储中处理套接字通信以及状态管理。 Here is my
GameStore.js
code: 这是我的
GameStore.js
代码:
export class GameStore {
constructor(aGame) {
extendObservable(this, {
players: [],
game: aGame,
menuVisibility: true,
play: action((id, username) => {
this.menuVisibility = false
username === undefined ? this.game.setName("guest") : this.game.setName(username)
// join game with given username
sock.emit('joinGame', this.game.playerName)
// after joining, if the username is 'guest' change name to unique guest name provided by server
sock.on('joinedGame', name => {
if(this.game.playerName === 'guest') this.game.setName(name)
console.log(this.game.playerName + " joined the game")
})
// populate player list with all players in game room
this.loadPlayers()
}),
quitGame: action(() => {
//this.menuVisibility = true
sock.emit('leaveGame', this.game.playerName)
sock.on('leftGame', players => { // this should be logged to all clients
console.log('updated player list', players)
this.players = players
})
}),
loadPlayers: action(() => {
sock.emit('findPlayersInGame', this.game.playerName)
sock.on('loadPlayers', players => {
console.log('loading players...')
this.players = players
})
sock.on('newPlayerJoined', player => {
console.log(player)
})
})
})
}
}
When I dispatch the quitGame
action, the socket only emits to the client that is leaving the game. 当我调度
quitGame
动作时,套接字只会向退出游戏的客户端发出信号。 I need to update the player list in my store after someone leaves the game, but I cannot figure out why the other clients are not getting the message that someone left the game. 有人离开游戏后,我需要更新商店中的玩家列表,但是我无法弄清楚为什么其他客户端没有收到有人离开游戏的消息。
io.emit
seems to be working fine when a player joins a game. 玩家加入游戏时,
io.emit
似乎工作正常。
It looks like you don't register the leftGame
message handler until this client leaves the game. 看起来您直到该客户端离开游戏之前都没有注册
leftGame
消息处理程序。 Thus, none of the other clients who are still in the game yet have a handler for that message. 因此,仍然在游戏中的其他客户端都没有该消息的处理程序。 They are probably receiving the message, but don't yet have a handler for it so you don't see it.
他们可能正在接收该消息,但是还没有相应的处理程序,因此您看不到它。
Move this code: 移动此代码:
sock.on('leftGame', players => { // this should be logged to all clients
console.log('updated player list', players)
this.players = players
})
so that it registers the event handler when the client wants to start receiving these messages (likely at startup). 因此,当客户端希望开始接收这些消息时(可能在启动时),它将注册事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.