/
server.js
164 lines (137 loc) · 4.01 KB
/
server.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8081 });
var AWS = require("aws-sdk");
AWS.config.update({
region: "ca-central-1",
endpoint: "https://dynamodb.ca-central-1.amazonaws.com",
accessKeyId: "",
secretAccessKey: ""
});
const redis = require('redis'); // used for redis
const client = redis.createClient(6379, 'place.tvsqkd.0001.cac1.cache.amazonaws.com'); //connecting to our redis server
var docClient = new AWS.DynamoDB.DocumentClient();
/* var dim = 1000; // note: this is not the right dimensions!!
var board=new Array(dim);
for(var x=0;x<dim;x++){
board[x]=new Array(dim);
for(var y=0;y<dim;y++){
board[x][y]={ 'r':255, 'g':255, 'b':255 };
}
} */
var dim = 1000;
wss.on('close', function() {
console.log('disconnected');
});
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
// for heartbeat to make sure connection is alive
function noop() {}
function heartbeat() {
this.isAlive = true;
}
function isValidSet(o){
var isValid=false;
try {
isValid =
Number.isInteger(o.x) && o.x!=null && 0<=o.x && o.x<dim &&
Number.isInteger(o.y) && o.y!=null && 0<=o.y && o.y<dim &&
Number.isInteger(o.colour) && o.colour != null && 0 <= o.colour && o.colour <= 15
} catch (err){
isValid=false;
}
return isValid;
}
wss.on('connection', function(ws) {
// heartbeat
console.log("A user is connected.");
ws.isAlive = true;
ws.on('pong', heartbeat);
client.get("board", function(err, reply) {
if(!err){
ws.send(JSON.stringify(Buffer.from(reply)));
}
});
// when we get a message from the client
ws.on('message', function(message) {
console.log(message);
var clientMessage = JSON.parse(message);
if(isValidSet(clientMessage)) {
currentDate = new Date();
console.log(String(currentDate));
var params = {
TableName: "user",
KeyConditionExpression: "username = :username",
ExpressionAttributeValues: {
":username": clientMessage["username"]
}
};
myTransaction = {
TransactItems: [
{
Put: {
TableName: "user",
Item: {'username' : {S:clientMessage["username"]},
'userTimeStamp' : {S:String(currentDate)}}
}
},
{
Put: {
TableName: "place",
Item: {'coordinate': {S: "(" + clientMessage["x"] + "," + clientMessage["y"] + ")"},
'colour': {S: String(clientMessage["colour"])},
'tileTimeStamp': {S: String(currentDate)},
'username': {S: clientMessage["username"]}}
}
}
]};
myPromise = docClient.query(params).promise();
myPromise.then((data) => {
console.log(JSON.stringify(data));
console.log(new Date());
if(data.Count == 0 || currentDate - (new Date(data.Items[0]["userTimeStamp"])) >= 30000) {
var dynamodb = new AWS.DynamoDB();
return dynamodb.transactWriteItems(myTransaction).promise();
}
else {
throw new Error("Can't update right now.");
}
}).then((data) => {
console.log("We did it!");
console.log(clientMessage["x"]);
console.log(clientMessage["y"]);
client.bitfield("board","set", "u4", String(4*parseInt(clientMessage["x"]) + 4000*parseInt(clientMessage["y"]) ), parseInt(clientMessage["colour"]));
wss.broadcast(message);
}).catch((err) => {
console.log(err);
});
}
});
});
// heartbeat (ping) sent to all clients
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping(noop);
});
}, 30000);
function getind(x,y) {
client.bitfield("board","get", "u8", String(4*x + 1000*y), function(err, reply) {
// reply is null when the key is missing
const buf1 = Buffer.from(reply);
//console.log(reply.charCodeAt(7));
console.log(buf1);
var bits = buf1.readUInt8(0) ;
if (x%2==0){
return (bits >> 4) & 15
}else{
return bits & 15
}
//
});
}