├── .eslintignore ├── .gitignore ├── knowledge.map ├── .editorconfig ├── LiveTable ├── package.json ├── server.js └── index.html ├── LICENSE ├── README.md └── .eslintrc.json /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /knowledge.map: -------------------------------------------------------------------------------- 1 | { 2 | dependencies: ['EventEmitter', 'NodeServer', 'WebsocketChat'] 3 | } 4 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /LiveTable/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatExample", 3 | "version": "0.1.0", 4 | "author": "Timur Shemsedinov ", 5 | "description": "Websocket Chat Example", 6 | "license": "MIT", 7 | "dependencies": { 8 | "websocket": "1.0.x" 9 | }, 10 | "readmeFilename": "README.md" 11 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2022 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 | -------------------------------------------------------------------------------- /LiveTable/server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('node:fs'); 4 | const http = require('node:http'); 5 | const Websocket = require('websocket').server; 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(80, () => { 15 | console.log('Listen port 80'); 16 | }); 17 | 18 | const ws = new Websocket({ 19 | httpServer: server, 20 | autoAcceptConnections: false 21 | }); 22 | 23 | const clients = []; 24 | 25 | ws.on('request', (req) => { 26 | const connection = req.accept('', req.origin); 27 | clients.push(connection); 28 | console.log('Connected ' + connection.remoteAddress); 29 | connection.on('message', (message) => { 30 | const dataName = message.type + 'Data'; 31 | const data = message[dataName]; 32 | console.log('Received: ' + data); 33 | clients.forEach((client) => { 34 | if (connection !== client) { 35 | client.send(data); 36 | } 37 | }); 38 | }); 39 | connection.on('close', (reasonCode, description) => { 40 | console.log('Disconnected ' + connection.remoteAddress); 41 | console.dir({ reasonCode, description }); 42 | }); 43 | }); 44 | -------------------------------------------------------------------------------- /LiveTable/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Live table 4 | 7 | 8 |

Live table

9 |
10 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Событийно-ориентированное программирование 2 | 3 | * Каталог `/EventEmitter` 4 | - `simpleEventEmitter.js` - простейшая реализация EventEmitter 5 | - `enhancedEventEmitter.js` - расширенная версию EventEmitter для Node.js 6 | * Каталог 'LiveTable' - пример таблицы, которая синхронизируется 7 | между несколькими браузерами через отправку событий по Websocket. 8 | 9 | Перед запуском `LiveTable` нужно запустить `npm install` из этого каталога, 10 | npm устаносит зависимости, указанные в `package.json`, после этого можно 11 | запускать `sudo node server`. Внимание, если другая программа заняла 80 порт, 12 | то нужно ее остановить, это может быть Apache, Skype, nginx, IIS и др. Если 13 | не удается освободить 80 порт, то заменить его на любой другой порт, поправив 14 | номер порта в коде сервера и допишите его в URL вебсокета в коде клиента. 15 | 16 | ## Задания 17 | 18 | 1. Реализовать расширенный EventEmitter, как в 'enhancedEventEmitter.js' на базе 19 | простого из `simpleEventEmitter.js`, а не на базе встроенной в Node.js библиотеки 20 | `events`. Адаптировать расширенный EventEmitter для одинаковой работы в Node.js 21 | и в браузере. 22 | 2. Модифицировать 'LiveTable' так, чтоб в ячейках можно было использовать 23 | формулы, как в электронных таблицах, например: `= A1 + B2`, но сделать это без 24 | использования циклов, а на событийной модели, т.е. при помощи подписки на 25 | изменения, с применением EventEmitter. 26 | 3. Транслировать события не только внутри приложения, но и по сети через 27 | Websocket и отправлять их в EventEmitter. 28 | 29 | ## Дополнительные задания 30 | 31 | 4. Сделать сетевую реализацию EventEmitter, делающую взаимодействие по сети 32 | прозрачным между клиентом и сервером. 33 | 34 | 5. Модифицировать сетевую реализацию EventEmitter, чтоб взаимодействие по сети 35 | было прозрачным между несколькими клиентами, через сервером. Реализовать 36 | ретрансляцию событий. 37 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaVersion": "latest" 10 | }, 11 | "globals": { 12 | "BigInt": true 13 | }, 14 | "rules": { 15 | "indent": [ 16 | "error", 17 | 2 18 | ], 19 | "linebreak-style": [ 20 | "error", 21 | "unix" 22 | ], 23 | "quotes": [ 24 | "error", 25 | "single" 26 | ], 27 | "semi": [ 28 | "error", 29 | "always" 30 | ], 31 | "no-loop-func": [ 32 | "error" 33 | ], 34 | "block-spacing": [ 35 | "error", 36 | "always" 37 | ], 38 | "camelcase": [ 39 | "error" 40 | ], 41 | "eqeqeq": [ 42 | "error", 43 | "always" 44 | ], 45 | "strict": [ 46 | "error", 47 | "global" 48 | ], 49 | "brace-style": [ 50 | "error", 51 | "1tbs", 52 | { 53 | "allowSingleLine": true 54 | } 55 | ], 56 | "comma-style": [ 57 | "error", 58 | "last" 59 | ], 60 | "comma-spacing": [ 61 | "error", 62 | { 63 | "before": false, 64 | "after": true 65 | } 66 | ], 67 | "eol-last": [ 68 | "error" 69 | ], 70 | "func-call-spacing": [ 71 | "error", 72 | "never" 73 | ], 74 | "key-spacing": [ 75 | "error", 76 | { 77 | "beforeColon": false, 78 | "afterColon": true, 79 | "mode": "minimum" 80 | } 81 | ], 82 | "keyword-spacing": [ 83 | "error", 84 | { 85 | "before": true, 86 | "after": true, 87 | "overrides": { 88 | "function": { 89 | "after": false 90 | } 91 | } 92 | } 93 | ], 94 | "max-len": [ 95 | "error", 96 | { 97 | "code": 80, 98 | "ignoreUrls": true 99 | } 100 | ], 101 | "max-nested-callbacks": [ 102 | "error", 103 | { 104 | "max": 7 105 | } 106 | ], 107 | "new-cap": [ 108 | "error", 109 | { 110 | "newIsCap": true, 111 | "capIsNew": false, 112 | "properties": true 113 | } 114 | ], 115 | "new-parens": [ 116 | "error" 117 | ], 118 | "no-lonely-if": [ 119 | "error" 120 | ], 121 | "no-trailing-spaces": [ 122 | "error" 123 | ], 124 | "no-unneeded-ternary": [ 125 | "error" 126 | ], 127 | "no-whitespace-before-property": [ 128 | "error" 129 | ], 130 | "object-curly-spacing": [ 131 | "error", 132 | "always" 133 | ], 134 | "operator-assignment": [ 135 | "error", 136 | "always" 137 | ], 138 | "operator-linebreak": [ 139 | "error", 140 | "after" 141 | ], 142 | "semi-spacing": [ 143 | "error", 144 | { 145 | "before": false, 146 | "after": true 147 | } 148 | ], 149 | "space-before-blocks": [ 150 | "error", 151 | "always" 152 | ], 153 | "space-before-function-paren": [ 154 | "error", 155 | { 156 | "anonymous": "never", 157 | "named": "never", 158 | "asyncArrow": "always" 159 | } 160 | ], 161 | "space-in-parens": [ 162 | "error", 163 | "never" 164 | ], 165 | "space-infix-ops": [ 166 | "error" 167 | ], 168 | "space-unary-ops": [ 169 | "error", 170 | { 171 | "words": true, 172 | "nonwords": false, 173 | "overrides": { 174 | "typeof": false 175 | } 176 | } 177 | ], 178 | "no-unreachable": [ 179 | "error" 180 | ], 181 | "no-global-assign": [ 182 | "error" 183 | ], 184 | "no-self-compare": [ 185 | "error" 186 | ], 187 | "no-unmodified-loop-condition": [ 188 | "error" 189 | ], 190 | "no-constant-condition": [ 191 | "error", 192 | { 193 | "checkLoops": false 194 | } 195 | ], 196 | "no-console": [ 197 | "off" 198 | ], 199 | "no-useless-concat": [ 200 | "error" 201 | ], 202 | "no-useless-escape": [ 203 | "error" 204 | ], 205 | "no-shadow-restricted-names": [ 206 | "error" 207 | ], 208 | "no-use-before-define": [ 209 | "error", 210 | { 211 | "functions": false 212 | } 213 | ], 214 | "arrow-parens": [ 215 | "error", 216 | "always" 217 | ], 218 | "arrow-body-style": [ 219 | "error", 220 | "as-needed" 221 | ], 222 | "arrow-spacing": [ 223 | "error" 224 | ], 225 | "no-confusing-arrow": [ 226 | "error", 227 | { 228 | "allowParens": true 229 | } 230 | ], 231 | "no-useless-computed-key": [ 232 | "error" 233 | ], 234 | "no-useless-rename": [ 235 | "error" 236 | ], 237 | "no-var": [ 238 | "error" 239 | ], 240 | "object-shorthand": [ 241 | "error", 242 | "always" 243 | ], 244 | "prefer-arrow-callback": [ 245 | "error" 246 | ], 247 | "prefer-const": [ 248 | "error" 249 | ], 250 | "prefer-numeric-literals": [ 251 | "error" 252 | ], 253 | "prefer-rest-params": [ 254 | "error" 255 | ], 256 | "prefer-spread": [ 257 | "error" 258 | ], 259 | "rest-spread-spacing": [ 260 | "error", 261 | "never" 262 | ], 263 | "template-curly-spacing": [ 264 | "error", 265 | "never" 266 | ], 267 | "consistent-return": [ 268 | "error", 269 | { "treatUndefinedAsUnspecified": true } 270 | ] 271 | } 272 | } 273 | --------------------------------------------------------------------------------