forked from mikolalysenko/patcher.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
131 lines (103 loc) · 3.21 KB
/
Copy pathexample.js
File metadata and controls
131 lines (103 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//An example server-client use case for the data model
var patcher = require("./patcher.js");
//----------------------------------------------------
//The server object
//----------------------------------------------------
function Server() {
this.data = {};
this.clients = [];
this.client_models = [];
};
//Adds a client to the server
Server.prototype.addClient = function(client) {
console.log("Client connected");
this.clients.push(client);
this.client_models.push({});
}
//Updates each of the clients
Server.prototype.broadcastChanges = function() {
console.log("Broadcasting");
for(var i=0; i<this.clients.length; ++i) {
//The flag causes the patcher to update the client model in place
var patch = patcher.computePatch(this.client_models[i], this.data, true);
//If there were any changes, send the patch to the client
if(patch) {
console.log("Updating client[" + i + "], patch = " + JSON.stringify(patch));
this.clients[i].sendUpdate(patch);
} else {
console.log("No changes for client[" + i + "]");
}
}
}
//Helper method, prints out the state of the server and all clients
Server.prototype.printState = function() {
console.log("server.data: " + JSON.stringify(this.data));
for(var i=0; i<this.clients.length; ++i) {
console.log("clients["+i+"].data: " + JSON.stringify(this.clients[i].data));
}
};
//----------------------------------------------------
//A client object
//----------------------------------------------------
function Client() {
this.data = {};
};
//Called when the client recieves an update from the server
Client.prototype.sendUpdate = function(patch) {
//Simply apply the patch to the data, and that's it!
patcher.applyPatch(this.data, patch);
};
//----------------------------------------------------
// A hypothetical scenario:
//----------------------------------------------------
//1.
console.log("\n\n");
console.log("1. A server is created....")
var server = new Server();
server.data["foo"] = "bar";
server.data["someobj"] = { a : 1, b:2, c:3 }
server.printState();
console.log("\n\n");
//2.
console.log("2. A new client connects....");
server.addClient(new Client());
server.printState();
console.log("\n\n");
//3.
console.log("3. The server broadcasts the state!");
server.broadcastChanges();
server.printState();
console.log("\n\n");
//4.
console.log("4. Suddenly, new clients connect..");
server.addClient(new Client());
server.addClient(new Client());
console.log("\n\n");
//5.
console.log("5. And the server updates their state");
server.broadcastChanges();
server.printState();
console.log("\n\n");
//6.
console.log("6. The server goes crazy and does a ton of updates");
delete server.data.someobj.a;
server.data.someobj.c = 4;
server.data.someobj["e"] = 6;
server.data["qqqq"] = true;
server.data["arr"] = [2,3,4];
server.broadcastChanges();
server.printState();
console.log("\n\n");
//7.
console.log("7. Later on, the server tweaks a single variable");
server.data.someobj["x"] = null;
server.printState();
server.broadcastChanges();
server.printState();
console.log("\n\n");
//8.
console.log("8. And then removes an object");
delete server.data.someobj;
server.broadcastChanges();
server.printState();
console.log("\n");