-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathserver.js
156 lines (142 loc) · 4.11 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
var WS_PORT = 8080;
var PROXY_PORT = 8081;
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: WS_PORT});
// Associations of token:PairRequest.
var pairRequests = {};
// Associations of conn_id:Connection.
var connections = {};
wss.on('connection', function(socket) {
console.log('Opening socket');
socket.on('message', function(message) {
console.log('received: %s', message);
try {
// Parse the message.
var json = JSON.parse(message);
} catch (err) {
// If the message isn't JSON, ignore but log it.
error('Message must be in JSON format.', err);
return;
}
// Initialize a pairing.
// q: {token: ...}
// a: {error: ...}
if (json.type == 'start') {
handleStart(socket, json);
}
// Confirm a pairing.
// q: {token: ...}
// a: {type: connected, conn_id: ...} or {error: ...}
if (json.type == 'confirm') {
handleConfirm(socket, json);
}
// Send a message.
// q: {conn_id: ..., message: ...}
// a: {error: ...}
//
// Also sends message in format a: {type: message, message: ...}
if (json.type == 'message') {
handleMessage(socket, json);
}
});
// When a client closes the socket.
socket.on('close', function() {
handleClose(socket);
});
});
function handleStart(socket, json) {
var token = json.token;
// If there's no token, error out.
if (token) {
// Add this token:ws pair to the list of pair requests.
pairRequests[token] = new PairRequest({
token: token,
socket: socket
});
socket.send(info('Got a pair request.'));
} else {
socket.send(error('No token provided.'));
}
}
function handleConfirm(socket, json) {
var token = json.token;
// Check if this token is already in the list of pair requests.
if (token in pairRequests) {
var pairRequest = pairRequests[token];
// If yes, create a connection between the two sockets.
var conn = new Connection({
socketA: socket,
socketB: pairRequest.socket
})
// Save the connection for later.
connections[conn.id] = conn;
// Notify both sockets that the connection is established.
var response = JSON.stringify({type: 'connected', conn_id: conn.id});
conn.socketA.send(response);
conn.socketB.send(response);
socket.send(info('Opened connection #' + conn.id));
} else {
// Otherwise, error.
socket.send(error('No pair request found for token.'));
}
}
function handleMessage(socket, json) {
var conn_id = json.conn_id;
var message = json.message;
// Check to see if this connection is valid.
if (conn_id in connections) {
// If yes, relay the message to the other socket
var conn = connections[conn_id];
var response = JSON.stringify({type: 'message', message: message});
if (conn.socketA == socket) {
conn.socketB.send(response);
} else if (conn.socketB == socket) {
conn.socketA.send(response);
}
} else {
// Otherwise, error.
socket.send(error('No connection found for connection ID.'));
}
}
function handleClose(socket) {
// Check if there was a connection associated with the socket.
var otherSocket;
var conn;
for (var connId in connections) {
conn = connections[connId];
if (conn.socketA == socket) {
otherSocket = conn.socketB;
break;
}
if (conn.socketB == socket) {
otherSocket = conn.socketA;
break;
}
}
// If yes, also disconnect the other client.
if (otherSocket) {
var response = JSON.stringify({type: 'disconnected', conn_id: conn.id});
console.log('Closing connection #' + conn.id);
otherSocket.send(response);
delete connections[conn.id];
} else {
console.log('Closing socket (no connection).');
}
}
function error(msg) {
return JSON.stringify({error: msg});
}
function info(msg) {
return JSON.stringify({info: msg});
}
function PairRequest(params) {
this.created = new Date();
this.token = params.token;
this.socket = params.socket;
}
var lastConnectionId = 0;
function Connection(params) {
this.id = lastConnectionId++;
this.socketA = params.socketA;
this.socketB = params.socketB;
}