Skip to content

Commit 4e436e3

Browse files
committed
1 parent cdab1fe commit 4e436e3

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.log
3+
.DS_Store

JavaScript/index.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<h1>Live table</h1>
5+
<table id="table"></table>
6+
<style>
7+
input { border: 1px solid green; width: 30px; }
8+
</style>
9+
<script>
10+
11+
let table = document.getElementById('table');
12+
let cells = {};
13+
let letters = ['A', 'B', 'C', 'D', 'E', 'F'];
14+
15+
let tr;
16+
tr = document.createElement('tr');
17+
tr.innerHTML = '<td></td>' + letters.map(
18+
(col) => '<td>' + col + '</td>'
19+
).join('');
20+
table.appendChild(tr);
21+
22+
for (let i = 1; i <= 5; i++) {
23+
tr = document.createElement('tr');
24+
tr.innerHTML = '<td>' + i + '</td>' + letters.map(
25+
(col) => '<td><input id="' + col + i + '" type="text"></td>'
26+
).join('');
27+
table.appendChild(tr);
28+
letters.forEach((col) => {
29+
let cell = col + i;
30+
let input = document.getElementById(cell);
31+
input.addEventListener('keyup', keyup);
32+
cells[cell] = input;
33+
});
34+
}
35+
36+
function keyup(event) {
37+
socket.send(JSON.stringify({
38+
cell: event.target.id,
39+
value: event.target.value
40+
}));
41+
}
42+
43+
let socket = new WebSocket('ws://127.0.0.1/');
44+
45+
socket.onmessage = (event) => {
46+
let change = JSON.parse(event.data);
47+
let cell = cells[change.cell];
48+
cell.value = change.value;
49+
};
50+
51+
</script>
52+
</body>
53+
</html>

JavaScript/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "chatExample",
3+
"version": "0.1.0",
4+
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",
5+
"description": "Websocket Chat Example",
6+
"license": "MIT",
7+
"dependencies": {
8+
"websocket": "1.0.x"
9+
},
10+
"readmeFilename": "README.md"
11+
}

JavaScript/server.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
global.api = {};
4+
api.fs = require('fs');
5+
api.http = require('http');
6+
api.websocket = require('websocket');
7+
8+
let index = api.fs.readFileSync('./index.html');
9+
10+
let server = api.http.createServer((req, res) => {
11+
res.writeHead(200);
12+
res.end(index);
13+
});
14+
15+
server.listen(80, () => {
16+
console.log('Listen port 80');
17+
});
18+
19+
let ws = new api.websocket.server({
20+
httpServer: server,
21+
autoAcceptConnections: false
22+
});
23+
24+
let clients = [];
25+
26+
ws.on('request', (req) => {
27+
let connection = req.accept('', req.origin);
28+
clients.push(connection);
29+
console.log('Connected ' + connection.remoteAddress);
30+
connection.on('message', (message) => {
31+
let dataName = message.type + 'Data',
32+
data = message[dataName];
33+
console.log('Received: ' + data);
34+
clients.forEach((client) => {
35+
if (connection !== client) {
36+
client.send(data);
37+
}
38+
});
39+
});
40+
connection.on('close', (reasonCode, description) => {
41+
console.log('Disconnected ' + connection.remoteAddress);
42+
});
43+
});

0 commit comments

Comments
 (0)