mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
some more hardcore network testing
This commit is contained in:
parent
5a3d3d0764
commit
26aa471b14
7 changed files with 123 additions and 31 deletions
|
|
@ -18,20 +18,26 @@ setInterval(updateClients, 500);
|
||||||
function updateClients() {
|
function updateClients() {
|
||||||
var body = world.GetBodyList();
|
var body = world.GetBodyList();
|
||||||
var update = {};
|
var update = {};
|
||||||
|
var isUpdateNeeded = false;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
var userData = body.GetUserData();
|
var userData = body.GetUserData();
|
||||||
|
|
||||||
if(userData && userData.bodyId){
|
if(userData && userData.bodyId && body.IsAwake()){
|
||||||
|
console.log(body.IsAwake());
|
||||||
update[userData.bodyId] = {
|
update[userData.bodyId] = {
|
||||||
p: body.GetPosition(),
|
p: body.GetPosition(),
|
||||||
a: body.GetAngle(),
|
a: body.GetAngle(),
|
||||||
v: body.GetLinearVelocity(),
|
v: body.GetLinearVelocity(),
|
||||||
};
|
};
|
||||||
|
isUpdateNeeded = true;
|
||||||
}
|
}
|
||||||
} while (body = body.GetNext());
|
} while (body = body.GetNext());
|
||||||
|
|
||||||
sendToClients('world-update', update);
|
|
||||||
|
if(isUpdateNeeded) {
|
||||||
|
sendToClients('world-update', update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendToClients(message, data) {
|
function sendToClients(message, data) {
|
||||||
|
|
@ -41,7 +47,7 @@ function sendToClients(message, data) {
|
||||||
}
|
}
|
||||||
//console.log(JSON.stringify(packet));
|
//console.log(JSON.stringify(packet));
|
||||||
for (var i = 0; i < clients.length; i++) {
|
for (var i = 0; i < clients.length; i++) {
|
||||||
clients[i].send(JSON.stringify(packet));
|
clients[i].send( JSON.stringify(packet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,6 +92,7 @@ socket.on('connection', function(client) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateClients();
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('disconnect', function(){
|
client.on('disconnect', function(){
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
var id = null;
|
var id = null;
|
||||||
|
|
||||||
|
//window.setInterval(ping, 1000);
|
||||||
|
|
||||||
|
function ping(){
|
||||||
|
var packet = {
|
||||||
|
m: 'ping',
|
||||||
|
d: new Date().getTime()
|
||||||
|
}
|
||||||
|
socket.send(JSON.stringify(packet));
|
||||||
|
}
|
||||||
|
|
||||||
function setupCanvas() {
|
function setupCanvas() {
|
||||||
var debugDraw = new b2DebugDraw();
|
var debugDraw = new b2DebugDraw();
|
||||||
|
|
||||||
|
|
@ -13,12 +23,13 @@ function setupCanvas() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _jump() {
|
function _jump() {
|
||||||
|
console.log('---jump---');
|
||||||
jump();
|
jump();
|
||||||
|
|
||||||
var data = {
|
var packet = {
|
||||||
m: 'jump'
|
m: 'jump'
|
||||||
};
|
};
|
||||||
socket.send(JSON.stringify(data));
|
socket.send(JSON.stringify(packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
|
@ -63,11 +74,18 @@ function updateWorld(data) {
|
||||||
var body = world.GetBodyList();
|
var body = world.GetBodyList();
|
||||||
do {
|
do {
|
||||||
var userData = body.GetUserData();
|
var userData = body.GetUserData();
|
||||||
if(userData && userData.bodyId){
|
if(userData && userData.bodyId && data[userData.bodyId]){
|
||||||
var update = data[userData.bodyId];
|
var update = data[userData.bodyId];
|
||||||
|
|
||||||
|
console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y);
|
||||||
|
|
||||||
|
/*body.SetAwake(true);
|
||||||
body.SetPosition(update.p);
|
body.SetPosition(update.p);
|
||||||
body.SetAngle(update.a);
|
body.SetAngle(update.a);
|
||||||
body.SetLinearVelocity(update.v);
|
body.SetLinearVelocity(update.v);
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} while (body = body.GetNext());
|
} while (body = body.GetNext());
|
||||||
}
|
}
|
||||||
|
|
@ -91,6 +109,9 @@ socket.on('message', function(packet) {
|
||||||
case 'world-update':
|
case 'world-update':
|
||||||
updateWorld(packet.d);
|
updateWorld(packet.d);
|
||||||
break;
|
break;
|
||||||
|
case 'pong':
|
||||||
|
console.log('pong', new Date().getTime() - packet.d);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
var id = null;
|
var id = null;
|
||||||
|
|
||||||
|
//window.setInterval(ping, 1000);
|
||||||
|
|
||||||
|
function ping(){
|
||||||
|
var packet = {
|
||||||
|
m: 'ping',
|
||||||
|
d: new Date().getTime()
|
||||||
|
}
|
||||||
|
socket.send(JSON.stringify(packet));
|
||||||
|
}
|
||||||
|
|
||||||
function setupCanvas() {
|
function setupCanvas() {
|
||||||
var debugDraw = new b2DebugDraw();
|
var debugDraw = new b2DebugDraw();
|
||||||
|
|
||||||
|
|
@ -13,12 +23,13 @@ function setupCanvas() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _jump() {
|
function _jump() {
|
||||||
|
console.log('---jump---');
|
||||||
|
jump();
|
||||||
|
|
||||||
var data = {
|
var packet = {
|
||||||
m: 'jump'
|
m: 'jump'
|
||||||
};
|
};
|
||||||
|
socket.send(JSON.stringify(packet));
|
||||||
socket.send(JSON.stringify(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
|
@ -63,11 +74,18 @@ function updateWorld(data) {
|
||||||
var body = world.GetBodyList();
|
var body = world.GetBodyList();
|
||||||
do {
|
do {
|
||||||
var userData = body.GetUserData();
|
var userData = body.GetUserData();
|
||||||
if(userData && userData.bodyId){
|
if(userData && userData.bodyId && data[userData.bodyId]){
|
||||||
var update = data[userData.bodyId];
|
var update = data[userData.bodyId];
|
||||||
|
|
||||||
|
console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y);
|
||||||
|
|
||||||
|
body.SetAwake(true);
|
||||||
body.SetPosition(update.p);
|
body.SetPosition(update.p);
|
||||||
body.SetAngle(update.a);
|
body.SetAngle(update.a);
|
||||||
body.SetLinearVelocity(update.v);
|
body.SetLinearVelocity(update.v);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} while (body = body.GetNext());
|
} while (body = body.GetNext());
|
||||||
}
|
}
|
||||||
|
|
@ -91,6 +109,9 @@ socket.on('message', function(packet) {
|
||||||
case 'world-update':
|
case 'world-update':
|
||||||
updateWorld(packet.d);
|
updateWorld(packet.d);
|
||||||
break;
|
break;
|
||||||
|
case 'pong':
|
||||||
|
console.log('pong', new Date().getTime() - packet.d);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ var bodiesNum = 3;
|
||||||
var world;
|
var world;
|
||||||
|
|
||||||
var xport = 8003;
|
var xport = 8003;
|
||||||
var xhost = '127.0.0.1';
|
var xhost = 'fuuuuu.de';
|
||||||
|
|
||||||
var b2Vec2 = Box2D.Common.Math.b2Vec2,
|
var b2Vec2 = Box2D.Common.Math.b2Vec2,
|
||||||
b2AABB = Box2D.Collision.b2AABB,
|
b2AABB = Box2D.Collision.b2AABB,
|
||||||
|
|
@ -22,8 +22,8 @@ function setupWorld() {
|
||||||
|
|
||||||
var fixDef = new b2FixtureDef;
|
var fixDef = new b2FixtureDef;
|
||||||
fixDef.density = 1.0;
|
fixDef.density = 1.0;
|
||||||
fixDef.friction = 0.5;
|
fixDef.friction = 0.99;
|
||||||
fixDef.restitution = 0.2;
|
fixDef.restitution = 0.01;
|
||||||
|
|
||||||
var bodyDef = new b2BodyDef;
|
var bodyDef = new b2BodyDef;
|
||||||
|
|
||||||
|
|
@ -56,7 +56,9 @@ function setupWorld() {
|
||||||
bodyDef.position.x = (i + 1) * 4;
|
bodyDef.position.x = (i + 1) * 4;
|
||||||
bodyDef.position.y = 3;
|
bodyDef.position.y = 3;
|
||||||
|
|
||||||
bodyDef.userData = {"bodyId" : parseInt(i) };
|
bodyDef.userData = {
|
||||||
|
'bodyId': i + ''
|
||||||
|
};
|
||||||
|
|
||||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ var bodiesNum = 3;
|
||||||
var world;
|
var world;
|
||||||
|
|
||||||
var xport = 8003;
|
var xport = 8003;
|
||||||
var xhost = '127.0.0.1';
|
var xhost = 'fuuuuu.de';
|
||||||
|
|
||||||
var b2Vec2 = Box2D.Common.Math.b2Vec2,
|
var b2Vec2 = Box2D.Common.Math.b2Vec2,
|
||||||
b2AABB = Box2D.Collision.b2AABB,
|
b2AABB = Box2D.Collision.b2AABB,
|
||||||
|
|
@ -23,7 +23,7 @@ function setupWorld() {
|
||||||
var fixDef = new b2FixtureDef;
|
var fixDef = new b2FixtureDef;
|
||||||
fixDef.density = 1.0;
|
fixDef.density = 1.0;
|
||||||
fixDef.friction = 0.5;
|
fixDef.friction = 0.5;
|
||||||
fixDef.restitution = 0.2;
|
fixDef.restitution = 0.01;
|
||||||
|
|
||||||
var bodyDef = new b2BodyDef;
|
var bodyDef = new b2BodyDef;
|
||||||
|
|
||||||
|
|
@ -56,7 +56,9 @@ function setupWorld() {
|
||||||
bodyDef.position.x = (i + 1) * 4;
|
bodyDef.position.x = (i + 1) * 4;
|
||||||
bodyDef.position.y = 3;
|
bodyDef.position.y = 3;
|
||||||
|
|
||||||
bodyDef.userData = {"bodyId" : parseInt(i) };
|
bodyDef.userData = {
|
||||||
|
'bodyId': i + ''
|
||||||
|
};
|
||||||
|
|
||||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||||
}
|
}
|
||||||
|
|
@ -65,7 +67,7 @@ function setupWorld() {
|
||||||
function jump() {
|
function jump() {
|
||||||
var body = findBody(1);
|
var body = findBody(1);
|
||||||
body.SetAwake(true);
|
body.SetAwake(true);
|
||||||
body.ApplyImpulse(new b2Vec2(0, -5), body.GetPosition());
|
body.ApplyImpulse(new b2Vec2(0, -9), body.GetPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
function findBody(index) {
|
function findBody(index) {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Box2dWeb Demo</title>
|
<title>Box2dWeb Demo</title>
|
||||||
<script src="http://localhost:8003/socket.io/socket.io.js"></script>
|
<script src="http://fuuuuu.de:8003/socket.io/socket.io.js"></script>
|
||||||
<script src="box2d.js"></script>
|
<script src="box2d.js"></script>
|
||||||
<script src="common.js"></script>
|
<script src="common.js"></script>
|
||||||
<script src="client.js"></script>
|
<script src="client.js"></script>
|
||||||
|
|
|
||||||
|
|
@ -13,25 +13,30 @@ function update() {
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(update, 1000 / 60);
|
setInterval(update, 1000 / 60);
|
||||||
setInterval(updateClients, 500);
|
setInterval(updateClients, 100);
|
||||||
|
|
||||||
function updateClients() {
|
function updateClients() {
|
||||||
var body = world.GetBodyList();
|
var body = world.GetBodyList();
|
||||||
var update = {};
|
var update = {};
|
||||||
|
var isUpdateNeeded = false;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
var userData = body.GetUserData();
|
var userData = body.GetUserData();
|
||||||
|
|
||||||
if(userData && userData.bodyId){
|
if(userData && userData.bodyId && body.IsAwake()){
|
||||||
update[userData.bodyId] = {
|
update[userData.bodyId] = {
|
||||||
p: body.GetPosition(),
|
p: body.GetPosition(),
|
||||||
a: body.GetAngle(),
|
a: body.GetAngle(),
|
||||||
v: body.GetLinearVelocity(),
|
v: body.GetLinearVelocity(),
|
||||||
};
|
};
|
||||||
|
isUpdateNeeded = true;
|
||||||
}
|
}
|
||||||
} while (body = body.GetNext());
|
} while (body = body.GetNext());
|
||||||
|
|
||||||
sendToClients('world-update', update);
|
|
||||||
|
if(isUpdateNeeded) {
|
||||||
|
sendToClients('world-update', update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendToClients(message, data) {
|
function sendToClients(message, data) {
|
||||||
|
|
@ -45,7 +50,13 @@ function sendToClients(message, data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function pong(client, data) {
|
||||||
|
var packet = {
|
||||||
|
m: 'pong',
|
||||||
|
d: data
|
||||||
|
}
|
||||||
|
client.send(JSON.stringify(packet));
|
||||||
|
}
|
||||||
|
|
||||||
setupWorld();
|
setupWorld();
|
||||||
//update();
|
//update();
|
||||||
|
|
@ -74,21 +85,49 @@ socket.on('connection', function(client) {
|
||||||
|
|
||||||
client.send(JSON.stringify({"startId" : clients.length}));
|
client.send(JSON.stringify({"startId" : clients.length}));
|
||||||
|
|
||||||
client.on('message', function(data){
|
client.on('message', function(packet){
|
||||||
data = JSON.parse(data);
|
packet = JSON.parse(packet);
|
||||||
|
|
||||||
if(data && data.m){
|
if(packet && packet.m){
|
||||||
switch(data.m){
|
switch(packet.m){
|
||||||
case 'jump':
|
case 'jump':
|
||||||
jump();
|
jump();
|
||||||
break;
|
break;
|
||||||
|
case 'ping':
|
||||||
|
pong(client, packet.d);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateClients();
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('disconnect', function(){
|
client.on('disconnect', function(){
|
||||||
console.log("disconnect");
|
console.log("disconnect");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/******* collision ********/
|
||||||
|
|
||||||
|
function createCollisionDetector() {
|
||||||
|
var listener = new b2ContactListener();
|
||||||
|
|
||||||
|
listener.BeginContact = function(contact){
|
||||||
|
|
||||||
|
}
|
||||||
|
listener.PostSolve = function(contact, impulse){
|
||||||
|
|
||||||
|
}
|
||||||
|
listener.EndContact = function(contact){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue