-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.ts
209 lines (174 loc) · 5.66 KB
/
chatserver.ts
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
import * as WebSocket from "ws";
import * as http from "http";
import { Message } from "./types";
// helpers
const filterMessages = (timestamp: string, messages: Message[]) => {
if (!timestamp) return messages; // return all messages if no timestamp was sent
return messages.filter(
(message) => Date.parse(message.timestamp) > Date.parse(timestamp)
);
};
const getTimeString = (dateString: string) => {
const date = new Date(dateString);
return date.getHours() + ":" + date.getMinutes();
};
const logMessage = (message: Message, type: string) => {
console.log(
`[${type}] ${getTimeString(message.timestamp)} | ` +
`${message.sender}: ${message.content.replace(/\n+/, " ")}`
);
};
// http/polling server
const pollingMessages: Message[] = [];
const pollingServer = http.createServer((req, res) => {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
if (req.url === "/polling") {
const timestamp = body;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify([...filterMessages(timestamp, pollingMessages)])); // return messages as json
} else if (req.url === "/polling/send") {
const message: Message = JSON.parse(body);
logMessage(message, "POLLING");
pollingMessages.push(message);
res.end(); // send 200 OK
} else {
res.end(); // ignore other requests
}
});
});
pollingServer.listen(8082, () =>
console.log(`Polling server started on port 8082`)
);
// long polling server
const longPollingMessages: Message[] = [];
const longPollingClients = new Set() as Set<http.ServerResponse>;
const longPollingServer = http.createServer((req, res) => {
let body = "";
req.on("data", (chunk) => {
body += chunk; // convert Buffer to string
});
req.on("end", () => {
if (req.url === "/long-polling") {
const timestamp = body;
const messages = [...filterMessages(timestamp, longPollingMessages)];
if (messages.length > 0) {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(messages));
} else {
longPollingClients.add(res); // register client
}
} else if (req.url === "/long-polling/send") {
const message: Message = JSON.parse(body);
logMessage(message, "LONG POLLING");
longPollingMessages.push(message);
res.end(); // send 200 OK
// send message to all clients
longPollingClients.forEach((client) => {
try {
// reserve client by removing it from the set immediately
longPollingClients.delete(client);
client.setHeader("Content-Type", "application/json");
client.end(JSON.stringify([message]));
} catch (_) {
// ignore errors
}
});
} else {
res.end(); // ignore other requests
}
});
});
longPollingServer.listen(8083, () =>
console.log(`Long polling server started on port 8083`)
);
// streaming server
const streamingMessages: Message[] = [];
const streamingClients = new Set() as Set<http.ServerResponse>;
const streamingServer = http.createServer((req, res) => {
let body = "";
req.on("data", (chunk) => {
body += chunk; // convert Buffer to string
});
req.on("end", () => {
if (req.url === "/streaming") {
const timestamp = body;
const messages = [...filterMessages(timestamp, longPollingMessages)];
res.setHeader("Content-Type", "application/json");
// return all avaialble messages, but keep the connection open
if (messages.length > 0) {
res.write(JSON.stringify(messages));
}
streamingClients.add(res); // register client
} else if (req.url === "/streaming/send") {
const message: Message = JSON.parse(body);
logMessage(message, "STREAMING");
streamingMessages.push(message);
res.end(); // send 200 OK
// send message to all clients
streamingClients.forEach((client) => {
client.write(JSON.stringify([message]));
});
} else {
res.end(); // ignore other requests
}
});
});
streamingServer.listen(8084, () =>
console.log(`Streaming server started on port 8084`)
);
// websocket server
const websocketServer = http.createServer();
const wss = new WebSocket.Server({ server: websocketServer });
wss.on("connection", (ws: WebSocket) => {
ws.on("message", (message: Message) => {
wss.clients.forEach((client: WebSocket) => {
if (client.readyState === WebSocket.OPEN) client.send(message);
});
});
});
websocketServer.listen(8081, () =>
console.log(`Websocket server started on port 8081`)
);
// server push server
/*
const lastServerPushMessages: Message[] = [];
const serverPushClients = new Set() as Set<http.ServerResponse>;
const serverPushServer = http.createServer((req, res) => {
if (req.url === "/server-push") {
let body = "";
req.on("data", (chunk) => {
body += chunk; // convert Buffer to string
});
req.on("end", () => {
// register client
serverPushClients.add(res);
res.end();
});
} else if (req.url === "/server-push/send") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
const message: Message = JSON.parse(body);
logMessage(message, "SERVER PUSH");
lastServerPushMessages.push(message);
// send message to all clients
serverPushClients.forEach((res) => {
res.setHeader("Content-Type", "application/json");
res.write(JSON.stringify(message));
});
res.end(); // send 200 OK
});
} else {
res.end(); // ignore other requests
}
});
serverPushServer.listen(8085, () =>
console.log(`Server push server started on port 8085`)
);
*/