-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathws.js
216 lines (187 loc) · 6.24 KB
/
ws.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function() {
var WebSocketServer = require('ws').Server;
var _ = require('underscore');
var ObjectId = require('mongojs').ObjectId;
var api = require('./api');
var db = require('./db');
var clients = [];
var Client = function (webSocket, sessionId, fileId) {
this._webSocket = webSocket;
this._fileId = fileId;
this._sessionId = sessionId;
this._apiPatchListener = this._onApiPatch.bind(this);
this._webSocket.on('message', this._onWebSocketMessage.bind(this));
this._webSocket.on('close', this._onWebSocketClose.bind(this));
api.on("patch", this._apiPatchListener);
};
Client.prototype = Object.create(null, {
constructor: {
value: Client,
enumerable: false
},
getSessionId: {
value: function () {
return this._sessionId;
}
},
_handlePatchMessage: {
value: function (data) {
api.filePatch(this._fileId, data, function (err, code) {
if (err) {
switch (code) {
case 409:
this._webSocket.send(JSON.stringify({
type: 'patchRejected',
data: {
code: code,
message: err
}
}));
break;
default:
this._webSocket.send(JSON.stringify({
type: 'patchError',
data: {
code: code,
message: err
}
}));
break;
}
}
}.bind(this));
}
},
sendPatch: {
value: function (patch) {
this._webSocket.send(JSON.stringify({
type: 'update',
data: patch
}));
}
},
_onApiPatch: {
value: function (data) {
this.sendPatch(data);
},
},
_onWebSocketMessage: {
value: function (data, flags) {
// flags.binary will be set if a binary data is received
// flags.masked will be set if the data was masked
if (!flags.binary) {
var message = JSON.parse(data);
if (message && message.type) {
switch (message.type) {
case 'patch':
this._handlePatchMessage(message.data);
break;
default:
console.log("Unknown WebSocket message '" + message.type + "' received");
break;
}
} else {
console.log("invalid WebSocket message received");
}
} else {
console.log("binary WebSocket message received");
}
}
},
_onWebSocketClose: {
value: function () {
api.closeSession(this._fileId, this._sessionId);
api.removeListener("patch", this._apiPatchListener);
}
}
});
function onWebSocketServerConnection(webSocket) {
var url = webSocket.upgradeReq.url;
var urlParts = url.split('/');
if (urlParts.length < 4) {
return;
}
var fileId = urlParts[2];
if (!fileId) {
console.log("Failed to open WebSocket: FileId not found");
webSocket.close(1000, "Not found");
return;
}
var sessionId = urlParts[3];
if (!sessionId) {
console.log("Failed to open WebSocket: SessionId not found");
webSocket.close(1000, "Not found");
return;
}
db.files.findOne({ _id: new ObjectId( fileId ) }, function (err, file) {
if (err) {
console.log("Failed to open WebSocket: File error: " + err);
webSocket.close(1011, err);
} else {
if (file) {
db.sessions.findOne({ _id: new ObjectId( sessionId ) }, function (sessionErr, session) {
if (sessionErr) {
console.log("Failed to open WebSocket: Session error: " + sessionErr);
webSocket.close(1011, sessionErr);
} else {
if (!session) {
console.log("Failed to open WebSocket: Session not found");
webSocket.close(1000, "Not found");
} else {
db.filesessions.update(
{ fileId: new ObjectId(file._id.toString()), sessionId: new ObjectId(sessionId ) },
{ $set: { type: 'WS' } }
);
var client = new Client(webSocket, session._id.toString(), file._id.toString());
clients.push(client);
if (session.joinRevision < file.revisionNumber) {
api.fileUpdate(session._id.toString(), session.joinRevision, file._id.toString(), function (err, code, patches) {
if (err) {
console.log("Could not retrieve missing patches while opening socket.");
webSocket.close(1011, "Internal Error");
} else {
if (code === 200) {
for (var i = 0, l = patches.length; i < l; i++) {
client.sendPatch(patches[i]);
}
}
}
});
}
}
}
});
} else {
console.log("Failed to open WebSocket: File not found");
webSocket.close(1000, "Not found");
}
}
});
}
module.exports = {
configure: function (httpServer, httpsServer) {
if (httpServer) {
var unsecureWebSocketServer = new WebSocketServer({
server: httpServer
});
unsecureWebSocketServer.on('connection', onWebSocketServerConnection);
console.log("Unsecure WebSocketServer listening");
}
if (httpsServer) {
var secureWebSocketServer = new WebSocketServer({
server: httpsServer
});
secureWebSocketServer.on('connection', onWebSocketServerConnection);
console.log("Secure WebSocketServer listening");
}
api.on("sessionClose", function (data) {
for (var i = clients.length - 1; i >= 0; i--) {
if (clients[i].getSessionId() === data.sessionId) {
clients.splice(i, 1);
break;
}
}
});
}
};
}).call(this);