[英]How to socket.emit 2x arrays via socket.io?
在向客戶端發送2個數組時遇到問題。 我有2個對象,所有實例都在服務器端創建。 socket.emit是根據我的理解在服務器25s / s和每個存在的套接字上所做的事情。 現在,我在每艘船上創建了子彈,並希望將其發送給客戶。 讓我頭疼的是,每艘船上我都在客戶端端使用socket.on,這是服務器上每個發出的套接字1:1。 現在,每個正在發射子彈的服務器上的每個插槽都是1:n。 我是否可以實際發射2個數組並執行2個發射執行,還是需要從每個套接字中將所有數據都放在一個數組中發射? 我的問題是,陣列項目符號在客戶端不存在!
因此,無論如何,我的戰艦價格都在客戶端,我在客戶端沒有子彈陣列,所以我用固定的x和y進行了測試
for (var i in bullet){
ctx.fillText("X",100,100);
}
但是客戶端什么也沒畫,這意味着客戶端沒有數組。 如果我有10枚以上的子彈,但即使沒有彈出,也發出了警報
感謝任何幫助
App.js
for(var q in SOCKET_LIST){
var socket = SOCKET_LIST[q];
var f =0;
for(var k = 0;k<allbullets.length;k++)
{
if (allbullets[k].user_id === q)
{
packbul= {
x:allbullets[k].xtype.x,
y:allbullets[k].xtype.y,
userid:allbullets[k].user_id,
}// array end
}
f++;
} //end for
socket.broadcast.emit('newBullet',packbul);
if (allobjects[q] === undefined)
{
}
else{
console.log("q:"+q);
pack[q] = {
x:allobjects[q].xtype.x,
y:allobjects[q].xtype.y,
userid:q,
}// array end
socket.broadcast.emit('newClientship',pack[q]);
} // else end
} // For ebf.
},1000/25); // Set Interval End
客戶
var ship = Array();
var bullet = Array();
socket.on('newClientship',function(data){
ship[data.userid]= data;
});
socket.on('newBullet',function(data){
bullet= data;
});
var previous; // var for renderworld
renderWorld = function(timestamp){
//setInterval(function(){
if (!previous) previous = timestamp;
elapsed = timestamp - previous;
updateFps();
ctx.font="150px Arial";
ctx.fillStyle = "white";
ctx.clearRect(0,0,canvas.width,canvas.height);
for ( var i in ship){ ctx.fillText(ship[i].userid,ship[i].x,ship[i].y);
}
if (bullet.length > 10)
{
alert("ted");
}
for (var i in bullet){
ctx.fillText("X",100,100);
}
drawFps(200,20) ;
previous = timestamp;
window.requestAnimationFrame(renderWorld);
//},1000/25);
}
剛剛發現服務器端的for循環不起作用
如果我console.log(allbullets.length),它給我一個“ undefined”
如果我執行console.log(allbullets),則表明它存在。
Bulletobj {
user_id: 47,
type: 'Bullet',
radius: 2,
basespeed: 1,
speed: 1,
velX: 0.2979165941720021,
velY: 0.9545919038609927,
targetX: 863,
targetY: 2429,
xtype: Obj { x: 153, y: 154, radius: 3, selected: 0 },
angleDeg: 1.268286927738952,
realAngle: 72.66748817105554 }
現在找到一個公式來計算obj的項目
console.log("length:"+Object.keys(allbullets).length);
但是即使如此,它也只計算其中一個對象中的項目,並且始終顯示12
我想擁有一個實例的所有子彈的數量
僅供參考我的船上有一個程序可以創建新子彈
ClientObj.prototype.fire =function (x,y){
allbullets = new Bulletobj(this.xtype.x,this.xtype.y,x,y,1,1, this.user_id);
}
//將這個var放在了global外部函數之外
allbullets = Array();
App.js
var allbullets = [];
Bulletobj = function(x,y,targetX,targetY,shipid,speed,user_id){
this.user_id = user_id;
this.type = 'Bullet';
this.radius = 2;
this.basespeed = speed;
this.speed = 4;
this.velX = 0;
this.velY = 0;
this.targetX = targetX;
this.targetY = targetY;
this.xtype = new Obj(x,y,3);
w.objects.push(this);
Bulletobj.prototype.update =function (){
tx = this.targetX - this.xtype.x;
ty = this.targetY - this.xtype.y;
dist = Math.sqrt(tx * tx + ty * ty);
this.angleDeg = Math.atan2(ty,tx) ;
this.realAngle = (Math.atan2(ty,tx) * 180/Math.PI + 360 ) % 360 ;
this.velX = (tx / dist) * this.speed ;
this.velY = (ty / dist) * this.speed ;
if (parseInt(dist) > parseInt(this.radius / 2)) {
this.xtype.x += parseInt(this.velX);
this.xtype.y += parseInt(this.velY);
} // if distance schleife end
} // Bulletobj update end
} // Bulletobj end
setInterval(function(){
var pack = Array();
var packbul = Array();
var packbularray = Array();
var spliceArray = Array();
objcnt = w.objects.length;
var i=0;
while (i < objcnt)
{
w.objects[i].update();
if(w.objects[i].hitpoints <= 0 )
spliceArray.push(i);
i++;
} // endwhile
for(var k = 0;k<spliceArray.length;k++)
{
w.objects.splice(spliceArray[k],1);
}
for(var q in SOCKET_LIST){
var socket = SOCKET_LIST[q];
if (allbullets === undefined)
{
}
else {
for(var k = 0;k<allbullets.length;k++)
{
if(allbullets[k].user_id == q)
{
packbul= {
x:allbullets[k].xtype.x,
y:allbullets[k].xtype.y,
userid:allbullets[k].user_id,
}// array end
} // if end
//obj end
} // end else undefined objects
} //end for
console.log(packbul);
socket.emit('newBullet',packbul);
if (allobjects[q] === undefined)
{
}
else{
console.log("q:"+q);
pack[q] = {
x:allobjects[q].xtype.x,
y:allobjects[q].xtype.y,
userid:q,
}// array end
socket.broadcast.emit('newClientship',pack[q]);
} // else end
} // For socket
},1000/25); // Set Interval End
Index.html
var ship = Array();
var bullet= Array();
var bulletdata =Array();
socket.on('newClientship',function(data){
ship[data.userid]= data;
});
socket.on('newBullet',function(data){
;
bulletdata.push(data);
});
var previous; // var for renderworld
renderWorld = function(timestamp){
if (!previous) previous = timestamp;
elapsed = timestamp - previous;
updateFps();
ctx.font="150px Arial";
ctx.fillStyle = "white";
ctx.clearRect(0,0,canvas.width,canvas.height);
for ( var i in ship){ ctx.fillText(ship[i].userid,ship[i].x,ship[i].y);
}
for (var i in bulletdata){
ctx.fillText(".",bulletdata[i].x,bulletdata[i].y);
}
bulletdata = [];
drawFps(200,20) ;
previous = timestamp;
window.requestAnimationFrame(renderWorld);
}
因此,我設法向客戶傳達了一顆子彈。 現在的問題是,每次玩家發射子彈,舊的子彈就會消失,而新的彈頭將從另一個客戶開始。 第一客戶筍。 如果第二個客戶端開始射擊,則刪除玩家1的子彈。 通常,我希望每個客戶端都有自己的項目符號,並將所有在服務器端作為對象創建的項目符號繪制在客戶端上。 船只運轉完美,但子彈忽隱忽現,不適合帆布😔謝謝
解決了 !!! 經過三天的挫敗
如果沒有它,我會再次為推而死
問題在於混合對象數組和標准數組。 例如var x = {} —>對象數組var x = []。 —>標准陣列
人們一直說你不應該發射物體,但是atm我很高興子彈到達客戶身上。 同樣,對於標准數組,我需要在選擇索引后進行拆分,並且必須自己設置數據集點。 使用對象數組可以更輕松地訪問它們。
也許有人可以告訴我在服務器端將對象數組推入標准數組時發出的信息。 這是對象還是標准數組? 我發送給客戶端的數組中是否包含產生性能問題的對象標頭(這是在某處讀取的內容)? 我的意思是,在客戶端我會收到一個數組,但是在ctx的for循環中,我又有一個對象數組,它位於標准數組內
if(allbullets[k].user_id == q)
{
packbul= {
x:allbullets[k].xtype.x,
y:allbullets[k].xtype.y,
userid:allbullets[k].user_id,
}// array end
packbularray.push(packbul);
} // if end
Index.html
var ship = Array();
var bullet= Array();
var bulletdata =Array();
socket.on('newClientship',function(data){
ship[data.userid]= data;
//ship.x = data.x;
//ship.y = data.y;
//ship.user = data.userid;
});
socket.on('newBullet',function(data){
bullet= data;
//bulletdata.push(bullet);
//ship.x = data.x;
//ship.y = data.y;
//ship.user = data.userid;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.