-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
156 lines (135 loc) · 3.62 KB
/
server.js
File metadata and controls
156 lines (135 loc) · 3.62 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
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
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3000 });
let LocationMap = {};
let MethodMap = {};
// Publisher methods
MethodMap['publish'] = publish;
MethodMap['endPublish'] = endPublish;
MethodMap['emitLocation'] = emitLocation;
MethodMap['getHistory'] = getHistory;
MethodMap['subscribe'] = subscribe;
MethodMap['unsubscribe'] = unsubscribe;
wss.on('connection', function connection(ws) {
ws.on('message', data => {
let jsonData;
try {
jsonData = JSON.parse(data);
}
catch (error) {
console.log('Invalid JSON');
}
const method = MethodMap[jsonData.method];
if (!method) {
console.log('Invalid method');
}
try {
method(ws, jsonData);
}
catch (error) {
console.log('Internal error handling client message:', error);
}
});
});
function subscribe(ws, jsonData) {
console.log('subscribe');
const locationID = jsonData.locationID;
let subscription = LocationMap[locationID];
if (subscription)
subscription.subscribe(ws);
}
function unsubscribe(ws, jsonData) {
console.log('unsubscribe');
}
function getHistory(ws, jsonData) {
console.log('getHistory');
const locationID = jsonData.locationID;
let subscription = LocationMap[locationID];
if (subscription) {
subscription.sendHistory(ws);
}
}
function publish(ws, jsonData) {
console.log('publish');
const locationID = jsonData.locationID;
LocationMap[locationID] = new Subscription(ws, locationID);
}
function emitLocation(ws, jsonData) {
console.log('emitLocation');
const locationID = jsonData.locationID;
let subscription = LocationMap[locationID];
if (subscription) {
subscription.addNewLocation(jsonData.location);
subscription.broadcast();
}
}
function endPublish(ws, jsonData) {
console.log('endPublish');
}
class Subscription {
constructor(publisherSocket, locationID) {
this.publisherSocket = publisherSocket;
this.locationID = locationID;
this.subscribers = [];
this.locationHistory = [];
this.latestLocation = {};
}
addNewLocation(location) {
this.latestLocation = location;
this.locationHistory.push(location);
}
subscribe(subscriberSocket) {
this.subscribers.push(subscriberSocket);
const message = new SubscriptionSuccessMessage([subscriberSocket]);
message.send();
}
unsubscribe(subscriberSocket) {
}
sendHistory(ws) {
const message = new LocationHistoryMessage([ws], this.locationHistory);
message.send();
}
broadcast() {
const message = new LatestLocationMessage(this.subscribers, this.latestLocation);
message.send();
}
}
class Message {
constructor(receivers, method, payload, ack) {
this.receivers = receivers;
this.method = method;
this.payload = payload;
this.ack = ack;
}
send() {
const data = {
method: this.method,
payload: this.payload
};
const message = JSON.stringify(data);
if (this.ack) {
for (let receiver of this.receivers)
receiver.send(message, this.ack);
}
else {
for (let receiver of this.receivers)
receiver.send(message);
}
}
}
class SubscriptionSuccessMessage extends Message {
constructor(receivers) {
super(receivers, 'SubscriptionSuccess');
}
}
class LatestLocationMessage extends Message {
constructor(receivers, location) {
// Don't pass in an ack (callback) because for now the server
// doesn't care if the client gets the message or not
super(receivers, 'LatestLocation', location);
}
}
class LocationHistoryMessage extends Message {
constructor(receivers, locations) {
super(receivers, 'LocationHistory', locations)
}
}