我一直在阅读大量关于不同人如何在多人游戏中实现 socket.io 的文章,以便我可以在我的游戏中使用它。当然,我是网络层方面的菜鸟,我迫切需要学习。
代码上下文:
-使用 Phaser3
-多人游戏
-加载所有现有玩家
-广播玩家加入
-更新玩家位置<---我的问题源自这里
问题:
-似乎只更新加入并广播它的最新套接字的位置
-可能并不重要,但传入的 socket.id 似乎取代了它设置为 id 的最后一个 socket.id,我认为这是因为“如果传入套接字不等于您的套接字则运行此参数”的参数确实如此未运行
-一般情况下,不更新游戏中其他玩家的状态(不移动其他玩家)
服务器.js:
const http = require('http');
const cors = require('cors');
const express = require('express');
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server,{cors:{origin:'http://localhost:8080',methods:['POST','GET']}});
const universe = {0:{},1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{},9:{}}
io.on('connection',(socket)=>
{
console.log(`${socket.id} joined the universe!`);
universe['0'][socket.id] = {playerID:socket.id};
console.log(`Online players: ${JSON.stringify(universe['0'])}`)
io.emit('join',socket.id, null); //sending to all clients and client
socket.emit('loadUniverse', universe['0']); //sending to client only
socket.on('updatePos',(player,vx,vy)=>
{
socket.broadcast.emit("updatePos1", player,vx,vy);
})
socket.on('disconnect',()=>
{
delete universe['0'][socket.id]
//emit a delete event
console.log(`${socket.id}player left the universe!`)
})
})
server.listen(3000,()=>{console.log('listening on port 3000')})
相关客户:
scene.socket.on('connect',()=> //needs to chain from the connect
{
this.scene.socket.emit('join',this.socketID)
console.log(`connected`);
})
scene.socket.on('updatePos1',(socketID1,vx,vy)=>
{ //this is a callback
console.log(`Message Recieved: ${socketID1}: ${vx} ${vy}`)
console.log(`players: ${JSON.stringify(this.scene.gameHandler.players)}`)
if(this.socketID!==socketID1)
{
console.log('socket was not yours!');
this.scene.gameHandler.players[socketID1].sprite.body.setVelocity(vx,vy);
}
})
scene.socket.on('join',(socketID0,skin)=>
{
console.log(`Player ${socketID0} joined`);
this.socketID = socketID0;
this.scene.gameHandler.loadPlayer(socketID0,skin);
})
scene.socket.on('loadUniverse',(universe)=>
{
for(const property in universe)
{
console.log(`propery:${property}`);
this.scene.gameHandler.loadPlayer(property,null);//make this dynamic
}
})
updatePos()
{
console.log(`${this.socketID} for updatePos()`)
this.scene.socket.emit(
'updatePos',this.socketID, //might have to do with how broadcasted
this.scene.gameHandler.players[this.socketID].sprite.body.velocity.x, //not updating individual position
this.scene.gameHandler.players[this.socketID].sprite.body.velocity.y,
)
}
在 game.js update() 函数(phaser3 库的标准事件循环)中,我使用上面的“updatePos()”函数更新客户端位置。
问题:
我到底做错了什么?为什么是错误的?
最佳答案
在客户端的此处理程序中,客户端会收到另一个客户端已加入的通知:
scene.socket.on('join',(socketID0,skin)=>
{
console.log(`Player ${socketID0} joined`);
this.socketID = socketID0;
this.scene.gameHandler.loadPlayer(socketID0,skin);
})
您正在设置this.socketID = socketID0
。我认为你不想这样做,因为你正在用最后加入的客户端的 id 覆盖你自己的 socketID。这会弄乱来自客户端的所有 future 通知,因为您将假装拥有最后加入的玩家的套接字 ID。我认为你应该完全删除该行。
如果您需要在某处初始化您自己的 this.socketID
,那么您将需要在其他地方执行此操作,其中它只是您自己的 socketID,并且仅在连接时完成一次。
关于javascript - Socket.io,广播发射,似乎仅使用最新连接的套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70600472/