Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (116 loc) · 4.01 KB
/
Copy pathapp.js
File metadata and controls
137 lines (116 loc) · 4.01 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
import http from 'http';
import EventEmitter from 'events';
import { Cache } from './cache.js';
import {
PORT,
LETTERS,
SEND_LETTER,
idGenerator,
STYLE_TEXT,
INTRO_HTML,
PRESS_EVENT
} from './util.js';
const cache = new Cache();
const msgList = [];
const event = new EventEmitter();
const getId = idGenerator();
function createPanel(id, current = '') {
const prev = cache.getHistory(id) || '';
const prevMsg = cache.getMsg(id) || '';
const now = prev + current;
const createKey = letter => {
const prefix = now ? `${now}-${letter}` : letter;
return `
<style>.btn_${prefix}:active { background-image: url('/keys/${prefix}_${id}_${letter}') }</style>
<button class="btn btn_${prefix}" style="grid-area: ${letter};">${letter}</button>
`;
};
const msgListHtml = () => msgList.reduce((prev, item) => `${prev}<li><span class="user" style="background:#${item.id}">#${item.id}</span>: ${item.msg}</li>`, '');
const sendBtnHTML = `
<div class="send-block">
<style>.send_${now}:active { background-image: url('/send-${now}_${id}') }</style>
<button class="send send_${now}">💬 Send</button>
</div>
`;
const keysStyle = current ? `<style>.main_${prev}{display: none;}</style>` : '';
const currentText = `${prevMsg}${current}`;
const msgHTML = msgListHtml();
return `
${keysStyle}
<div class="main main_${now}">
<div class="keyboard">
${LETTERS.map(createKey).join('')}
</div>
${sendBtnHTML}
<div>
<h2>
Current Message:
</h2>
<p>
${currentText === SEND_LETTER ? '' : currentText}
</p>
</div>
${msgHTML ? '<h2>Message List:</h2>' : ''}
<ul style="max-width: 300px; text-align: left; margin: auto;">
${msgHTML}
</ul>
</div>
`;
}
const app = http.createServer((req, res) => {
if (/^\/$/.test(req.url)) {
req.url = '/index';
}
if (/^\/index$/.test(req.url) && req.method === 'GET') {
const clientId = getId();
cache.add(clientId, '');
const send = ({id, letter}) => clientId === id && res.write(createPanel(id, letter));
res.statusCode = 200;
res.setHeader('connection', 'keep-alive');
res.setHeader('content-type', 'text/html; charset=utf-8');
res.write(INTRO_HTML.replaceAll('%id%', `#${clientId}`));
res.write(createPanel(clientId));
event.addListener(PRESS_EVENT, send);
req.socket.on('close', () => event.removeListener(PRESS_EVENT, send));
// 3) The connection NEVER ENDS
// res.end();
return;
}
if (/^\/send-/.test(req.url)) {
// 2) ==============
// console.log("2) CSS :active on send sends the message", req.url);
// =================
const clientId = req.url.split('_')[1];
const msg = cache.getMsg(clientId);
cache.clear(clientId);
msgList.push({id: clientId, msg});
cache.ids().forEach(id => {
event.emit(PRESS_EVENT, {id, letter: SEND_LETTER});
cache.add(id, SEND_LETTER);
});
res.statusCode = 204;
res.end();
return
}
if (/^\/keys/.test(req.url)) {
// 1) ==============
// console.log("1) CSS :active triggers request ", req.url);
// =================
const segments = req.url.split('_');
const letter = segments[2];
const id = segments[1];
event.emit(PRESS_EVENT, {id, letter});
cache.add(id, letter);
res.statusCode = 204;
res.end();
return;
}
if (/^\/style.css$/.test(req.url) && req.method === 'GET') {
res.setHeader('content-type', 'text/css');
res.end(STYLE_TEXT);
return;
}
res.statusCode = 404;
res.end('404');
});
app.listen(PORT, () => console.log('listening on %s port', PORT));