├── .github └── FUNDING.yml ├── .gitignore ├── eslint.config.js ├── prettier.config.js ├── .editorconfig ├── README.md ├── package.json ├── server.js ├── LICENSE └── index.html /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: tshemsedinov 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const init = require('eslint-config-metarhia'); 4 | 5 | module.exports = init; 6 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | printWidth: 80, 5 | singleQuote: true, 6 | trailingComma: 'all', 7 | tabWidth: 2, 8 | useTabs: false, 9 | semi: true, 10 | }; 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | end_of_line = lf 6 | charset = utf-8 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | 10 | [{*.js,*.mjs,*.ts,*.json,*.yml}] 11 | indent_size = 2 12 | indent_style = space 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Чат на Websocket 2 | 3 | [![WebSocket сервер на Node.js (электронные таблицы и чат)](https://img.youtube.com/vi/Sf7ln3n16ws/0.jpg)](https://www.youtube.com/watch?v=Sf7ln3n16ws) 4 | 5 | * `server.js` - серверная часть, слушает TCP порт 8000 (ждет подключений) 6 | * Клиентская часть `index.html`: открыть в браузере через `127.0.0.1:8000` 7 | 8 | ## Задания 9 | 10 | ## Дополнительные задания 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "patterns", 3 | "version": "1.0.0", 4 | "scripts": { 5 | "lint": "eslint . && prettier -c \"**/*.js\"", 6 | "fix": "eslint . --fix && prettier --write \"**/*.js\"" 7 | }, 8 | "author": "Timur Shemsedinov", 9 | "private": true, 10 | "dependencies": { 11 | "eslint": "^9.12.0", 12 | "eslint-config-metarhia": "^9.1.1", 13 | "prettier": "^3.3.3" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('node:fs'); 4 | const http = require('node:http'); 5 | const WebSocket = require('ws'); 6 | 7 | const index = fs.readFileSync('./index.html', 'utf8'); 8 | 9 | const server = http.createServer((req, res) => { 10 | res.writeHead(200); 11 | res.end(index); 12 | }); 13 | 14 | server.listen(8000, () => { 15 | console.log('Listen port 8000'); 16 | }); 17 | 18 | const ws = new WebSocket.Server({ server }); 19 | 20 | ws.on('connection', (connection, req) => { 21 | const ip = req.socket.remoteAddress; 22 | console.log(`Connected ${ip}`); 23 | connection.on('message', (message) => { 24 | console.log('Received: ' + message); 25 | for (const client of ws.clients) { 26 | if (client.readyState !== WebSocket.OPEN) continue; 27 | if (client === connection) continue; 28 | client.send(message, { binary: false }); 29 | } 30 | }); 31 | connection.on('close', () => { 32 | console.log(`Disconnected ${ip}`); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2025 How.Programming.Works contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Chat

5 |
6 | 7 | 10 | 47 | 48 | 49 | --------------------------------------------------------------------------------