-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (80 loc) · 2.56 KB
/
index.js
File metadata and controls
89 lines (80 loc) · 2.56 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
/**
* Created by Peter on 9-8-2015.
*/
var io = require('socket.io')(3100);
var cvObject = {"EntityType":"CV","Id":"petermoolenaar","Version":0};
cvObject.base = {"Peter Moolenaar": "Rotterdam"};
cvObject.personalia = {
"Place of Birth": "'s - Gravendeel",
"Date of Birth": "August 20, 1983",
"Current home town": "Rotterdam",
"Contact": ["peter@moolenaar.info", "06-13070009"]
};
cvObject.workhistory = [
"President @ student society",
"Student assistant @ TU Delft",
"Software engineer @ Bitbybit",
"Software engineer @ Poolmanager",
"The future brings new challenges!"
];
var myStore = {
fns: {
// The update contains the new definition of an entity
updateModel: function (update) {
var validUpdate = true;
var newModel = update;
if(validUpdate) {
io.emit('fn',{fn:'updateModel',parameters:newModel});
};
},
// Data updates come in the form of an identifier and a payload.
updateData: function (update, originatingSocket) {
var validUpdate = false;
var id = update.id;
var payload = update.payload;
if(!myStore.data[id]) {
myStore.data[id] = payload;
validUpdate = true;
} else if(myStore.data[id].Version == payload.Version)
{
validUpdate = true;
myStore.data[id] = payload;
myStore.data[id].Version++;
}
if(validUpdate)
{
updateSubscribedClients(id, payload);
} else {
// We reject this update and send the truth back.
originatingSocket.emit('fn',{fn:'updateData',parameters:{id:id, payload: myStore.data[id]}});
}
},
subscribe: function (query, originatingSocket) {
},
findEntity: function(id, originatingSocket) {
if(myStore.data[id])
originatingSocket.emit('fn',{fn:'updateData',parameters:{id:id, payload: myStore.data[id]}});
}
},
model: {},
data: {"petermoolenaar": cvObject}
};
var updateSubscribedClients = function(id, payload) {
// Look up the clients that are currently doing something with object with id = id
io.emit('fn',{fn:'updateData',parameters:{id:id, payload: payload}});// For the sake of this demo we don't filter (yet)
}
// Basic handler for the socket
io.on('connection', function(socket){
console.log('a user connected');
socket.emit('hello',{ehlo: "Welcome."});
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('fn',function(body){
var fnName = body.fn;
if(typeof myStore.fns[fnName] == 'function')
{
myStore.fns[fnName](body.parameters, socket);
}
});
});