-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossword.js
57 lines (48 loc) · 1.56 KB
/
crossword.js
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
var http = require('http'),
faye = require('faye'),
PORT = 8008;
// Can access the "current" puzzle at |crosswords.puzzle|.
var crosswords = require("./puzzle.js");
var bayeux = new faye.NodeAdapter({
mount: '/crossword',
timeout: 45,
});
// mergeProgress: takes two arrays of strings and copies all entries from |arrB|
// into |arrA| where they do not exist. Defaults to |arrA| for most choices.
function mergeProgress(progA, progB) {
if (progA.length != progB.length)
return progA;
var toRet = progA.slice();
for (var i = 0; i < progA.length; i++) {
if ((progB[i]) && !(progA[i]))
toRet[i] = progB[i];
}
return toRet;
}
var mergeCrosswordData = {
incoming: function(message, callback) {
console.log(message);
if (message.channel != "/room1")
return callback(message);
var oldProgress = message.data.progress;
var newProgress = mergeProgress(crosswords.puzzle.progress, oldProgress);
newProgress = newProgress.map(function(x,i) {
return crosswords.puzzle.grid[i] == x ? x : '';
});
crosswords.puzzle.progress = newProgress;
message.data.progress = newProgress;
callback(message);
},
};
bayeux.addExtension(mergeCrosswordData);
var client = bayeux.getClient();
client.subscribe("/room1", function(message) {
console.log("Received message: " + message.command);
});
var server = http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello, non-Bayeux request');
response.end();
});
bayeux.attach(server);
server.listen(PORT);