我正在建造一个游戏。每当第一个玩家连接到服务器时,我希望他看到一个模态,告诉他等待第二个玩家。为了触发模态,我试图告诉组件第一个玩家连接并向他显示一个告诉他等待的模态。问题是组件没有收到“第一个连接的玩家”事件,因此我无法触发模态。我可以在控制台中看到事件被发送到正确的房间,但它仍然无法正常工作。我的代码出了什么问题? 服务器:
var roomno = 0;
var players = 0;
io.on('connection', socket => {
players++;
if(io.nsps['/'].adapter.rooms["room-"+roomno] &&
io.nsps['/'].adapter.rooms["room-"+roomno].length > 1)
roomno++;
socket.join("room-"+roomno);
io.sockets.in("room-" + roomno).emit('connectToRoom', roomno);
if(players % 2 == 0) {
moves[roomno] = Array(9).fill(null);
socket.to('room-' + roomno).emit('friend connected')
}
if(players % 2 !== 0) {
socket.to('room-' + roomno).emit('first player connected')
console.log('sending event to room-' + roomno)
}
组件:
componentWillMount() {
this.socket.on('first player connected', () => {
alert('this alert doesnt even get triggered')
this.setState({
showModal:true
})
wrapper.classList.add('mask');
})
...bla-bla
}