i have problem with my js canvas game. project seems mad god game. (rotmg in google) i use php socket to communication. when player change possition i send updated x and y:
function keysPressed(e) {
keys[e.keyCode] = true;
if (socket_open) {
var msgg = {
type : 'char_pos',
px : pixX,
py : pixY
};
websocket.send(JSON.stringify(msgg));
}
}
server part:
//check for any incomming data
while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
{
$received_text = unmask($buf); //unmask data
$msg = json_decode($received_text); //json decode
$id=array_search($changed_socket,$clients);
$pos[$id]['x']=$msg->px;
$pos[$id]['y']=$msg->py;
//prepare data to be sent to client
$response_text = mask(json_encode(array('type'=>'rys_inni', 'id'=>$id, 'pos'=>$pos)));
send_message($response_text); //send data
break 2; //exist this loop
}
next, when data come back i do this:
websocket.onmessage = function (ev) {
var msg = JSON.parse(ev.data);
var type = msg.type;
if (type == 'rys_inni') {
tab_innych_charow = [];
for (key in msg.pos)
if (typeof(msg.pos[key]) !== 'undefined')
tab_innych_charow.push(msg.pos[key]);
}
};
and this is in interval:
function rysuj() {
// clear canvas and bckground
for (i = 0; i < tab_innych_charow.length; i++) {
var x = tab_innych_charow[i].x; // +...;
var y = tab_innych_charow[i].y; // +...;
draw(ctx, something, x, y);
}
}
the problem is that the different players drawing with lags and my game look poor. where i do mistake ? is the problem lies in php? i read about socket.io but i would rather php cause it is easier for me, i think. in my model (realm of the mad god) move is very fast and smooth. i don't know what to do. you are my last chance. thanks.