{"id":2068,"date":"2025-09-25T06:53:42","date_gmt":"2025-09-25T06:53:42","guid":{"rendered":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068"},"modified":"2025-09-25T06:53:42","modified_gmt":"2025-09-25T06:53:42","slug":"h11-vestlusruum-serveri-ja-klientrakenduse-vahel","status":"publish","type":"page","link":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068","title":{"rendered":"H11: Vestlusruum serveri ja klientrakenduse vahel"},"content":{"rendered":"\n<p>T\u00e4na loome serveri, mis t\u00f6\u00f6tab HTTP ja WebSocketiga, v\u00f5imaldades saata s\u00f5numeid \u00fchendatud kasutajatele.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Loomine<\/strong><\/p>\n\n\n\n<p>Avame terminal ja luuame uus kaust<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"494\" height=\"83\" src=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png\" alt=\"\" class=\"wp-image-2071\" srcset=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png 494w, https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4-300x50.png 300w, https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4-150x25.png 150w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<p>Seal avame VS code ja kirjutame kood<\/p>\n\n\n\n<p><strong>index.html<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;!doctype html&gt;\n&lt;form name=&quot;publish&quot;&gt;\n    &lt;input type=&quot;text&quot; name=&quot;message&quot; maxlength=&quot;50&quot;\/&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Send&quot;\/&gt;\n&lt;\/form&gt;\n \n&lt;div id=&quot;messages&quot;&gt;&lt;\/div&gt;\n \n&lt;script&gt;\n    let url = location.host == &#39;localhost&#39; ?\n        &#39;ws:\/\/localhost:8080\/ws&#39; : location.host == &#39;javascript.local&#39; ?\n            `ws:\/\/javascript.local\/article\/websocket\/chat\/ws` : \/\/ dev integration with local site\n            `wss:\/\/javascript.info\/article\/websocket\/chat\/ws`; \/\/ prod integration with javascript.info\n    \/\/\u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u043e\u043c\n    let socket = new WebSocket(url);\n \n    \/\/ \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u0444\u043e\u0440\u043c\u0443\n    document.forms.publish.onsubmit = function() {\n        let outgoingMessage = this.message.value;\n \n        socket.send(outgoingMessage);\n        return false;\n    };\n \n    \/\/ \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\n    socket.onmessage = function(event) {\n        let incomingMessage = event.data;\n        showMessage(incomingMessage);\n    };\n    \/\/\u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u043a\u0440\u044b\u0442 \u0441\u043e\u043a\u0435\u0442, \u0442\u043e \u043f\u0438\u0448\u0435\u043c \u043e\u0431 \u044d\u0442\u043e\u043c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438\n    socket.onclose = event =&gt; console.log(`Closed ${event.code}`);\n \n    \/\/ \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432 div#messages\n    function showMessage(message) {\n        let messageElem = document.createElement(&#39;div&#39;);\n        messageElem.textContent = message;\n        document.getElementById(&#39;messages&#39;).prepend(messageElem);\n    }\n&lt;\/script&gt;<\/code><\/pre><\/div>\n\n\n\n<p><strong>index.js<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>const  ws = require(&#39;ws&#39;); \nconst http = require(&#39;http&#39;);\nconst fs = require(&#39;fs&#39;);\n\nconst wss = new ws.Server({ noServer: true });\n\nfunction accept(req, res) {\n  if (req.url === &#39;\/ws&#39; && req.headers.upgrade &&\n    req.headers.upgrade.toLowerCase() === &#39;websocket&#39; &&\n    req.headers.connection.match(\/\\bupgrade\\b\/i)) {\n    wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onSocketConnect);\n  } else if (req.url === &#39;\/&#39;) { \n    fs.createReadStream({ path: &#39;.\/index.html&#39; }).pipe(res);\n  } else { \n    res.writeHead(404);\n    res.end();\n  }\n}\nconst clients = new Set();\n\nfunction onSocketConnect(ws) {\n  clients.add(ws);\n  \n  ws.on(&#39;message&#39;, function(message) {\n    message = message.slice(0, 50); \n    for (let client of clients) {\n      client.send(message);\n    }\n  });\n\n  ws.on(&#39;close&#39;, function() {\n    console.log(&#39;connection closed&#39;);\n    clients.delete(ws);\n  });\n}\n\n\nlet log;\nif (!module[&quot;parent&quot;]){\n    log = console.log; \n    http.createServer(accept).listen(8080);\n}\nelse {\n    log = function() {}; \n    exports.accept = accept;\n\n}<\/code><\/pre><\/div>\n\n\n\n<p>Siin me t\u00f6\u00f6tame nii WebSocketi kui ka HTTP-ga. Kui kasutaja k\u00fclastab lehte, saadab server talle faili <code>index.html<\/code>. Kui keegi \u00fchendub WebSocketi kaudu, v\u00f5tab server vastu s\u00f5numeid, piirab need 50 t\u00e4hem\u00e4rgiga ja saadab k\u00f5ik s\u00f5numid teistele \u00fchendatud kasutajatele. Server t\u00f6\u00f6tab pordil 8080 ja haldab \u00fchendusi: lisab need nimekirja ja eemaldab, kui \u00fchendus suletakse.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"600\" height=\"78\" src=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/4601B2C9-32F3-429D-A5CD-4319C482DFBE.png\" alt=\"\" class=\"wp-image-2070\" srcset=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/4601B2C9-32F3-429D-A5CD-4319C482DFBE.png 600w, https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/4601B2C9-32F3-429D-A5CD-4319C482DFBE-300x39.png 300w, https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/4601B2C9-32F3-429D-A5CD-4319C482DFBE-150x20.png 150w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"261\" height=\"329\" src=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/2004CC9D-7DF2-4DF6-B261-CD6081313E86.png\" alt=\"\" class=\"wp-image-2072\" srcset=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/2004CC9D-7DF2-4DF6-B261-CD6081313E86.png 261w, https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/2004CC9D-7DF2-4DF6-B261-CD6081313E86-238x300.png 238w, https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/2004CC9D-7DF2-4DF6-B261-CD6081313E86-119x150.png 119w\" sizes=\"(max-width: 261px) 100vw, 261px\" \/><\/figure>\n\n\n\n<p>k\u00e4ivitame seda terminalis ja vaatame tulemus<\/p>\n","protected":false},"excerpt":{"rendered":"<p>T\u00e4na loome serveri, mis t\u00f6\u00f6tab HTTP ja WebSocketiga, v\u00f5imaldades saata s\u00f5numeid \u00fchendatud kasutajatele. Loomine Avame terminal ja luuame uus kaust Seal avame VS code ja kirjutame kood index.html index.js Siin me t\u00f6\u00f6tame nii WebSocketi kui ka HTTP-ga. Kui kasutaja k\u00fclastab lehte, saadab server talle faili index.html. Kui keegi \u00fchendub WebSocketi kaudu, v\u00f5tab server vastu s\u00f5numeid, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2068","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>H11: Vestlusruum serveri ja klientrakenduse vahel - Marija Gorbunova portfoolio TARpv23<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068\" \/>\n<meta property=\"og:locale\" content=\"et_EE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"H11: Vestlusruum serveri ja klientrakenduse vahel - Marija Gorbunova portfoolio TARpv23\" \/>\n<meta property=\"og:description\" content=\"T\u00e4na loome serveri, mis t\u00f6\u00f6tab HTTP ja WebSocketiga, v\u00f5imaldades saata s\u00f5numeid \u00fchendatud kasutajatele. Loomine Avame terminal ja luuame uus kaust Seal avame VS code ja kirjutame kood index.html index.js Siin me t\u00f6\u00f6tame nii WebSocketi kui ka HTTP-ga. Kui kasutaja k\u00fclastab lehte, saadab server talle faili index.html. Kui keegi \u00fchendub WebSocketi kaudu, v\u00f5tab server vastu s\u00f5numeid, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068\" \/>\n<meta property=\"og:site_name\" content=\"Marija Gorbunova portfoolio TARpv23\" \/>\n<meta property=\"og:image\" content=\"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png\" \/>\n\t<meta property=\"og:image:width\" content=\"494\" \/>\n\t<meta property=\"og:image:height\" content=\"83\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutit\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068\",\"url\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068\",\"name\":\"H11: Vestlusruum serveri ja klientrakenduse vahel - Marija Gorbunova portfoolio TARpv23\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png\",\"datePublished\":\"2025-09-25T06:53:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068#breadcrumb\"},\"inLanguage\":\"et\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"et\",\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068#primaryimage\",\"url\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png\",\"contentUrl\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png\",\"width\":494,\"height\":83},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?page_id=2068#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"H11: Vestlusruum serveri ja klientrakenduse vahel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/#website\",\"url\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/\",\"name\":\"Marija Gorbunova portfoolio TARpv23\",\"description\":\"J&auml;rgmine WordPress veebileht\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/marijagorbunova23.thkit.ee\\\/wp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"et\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"H11: Vestlusruum serveri ja klientrakenduse vahel - Marija Gorbunova portfoolio TARpv23","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068","og_locale":"et_EE","og_type":"article","og_title":"H11: Vestlusruum serveri ja klientrakenduse vahel - Marija Gorbunova portfoolio TARpv23","og_description":"T\u00e4na loome serveri, mis t\u00f6\u00f6tab HTTP ja WebSocketiga, v\u00f5imaldades saata s\u00f5numeid \u00fchendatud kasutajatele. Loomine Avame terminal ja luuame uus kaust Seal avame VS code ja kirjutame kood index.html index.js Siin me t\u00f6\u00f6tame nii WebSocketi kui ka HTTP-ga. Kui kasutaja k\u00fclastab lehte, saadab server talle faili index.html. Kui keegi \u00fchendub WebSocketi kaudu, v\u00f5tab server vastu s\u00f5numeid, [&hellip;]","og_url":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068","og_site_name":"Marija Gorbunova portfoolio TARpv23","og_image":[{"width":494,"height":83,"url":"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutit"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068","url":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068","name":"H11: Vestlusruum serveri ja klientrakenduse vahel - Marija Gorbunova portfoolio TARpv23","isPartOf":{"@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/#website"},"primaryImageOfPage":{"@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068#primaryimage"},"image":{"@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068#primaryimage"},"thumbnailUrl":"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png","datePublished":"2025-09-25T06:53:42+00:00","breadcrumb":{"@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068#breadcrumb"},"inLanguage":"et","potentialAction":[{"@type":"ReadAction","target":["https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068"]}]},{"@type":"ImageObject","inLanguage":"et","@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068#primaryimage","url":"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png","contentUrl":"https:\/\/marijagorbunova23.thkit.ee\/wp\/wp-content\/uploads\/2025\/09\/C6C4B0E7-DA67-4E48-BCD1-43FA393246C4.png","width":494,"height":83},{"@type":"BreadcrumbList","@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?page_id=2068#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/marijagorbunova23.thkit.ee\/wp\/"},{"@type":"ListItem","position":2,"name":"H11: Vestlusruum serveri ja klientrakenduse vahel"}]},{"@type":"WebSite","@id":"https:\/\/marijagorbunova23.thkit.ee\/wp\/#website","url":"https:\/\/marijagorbunova23.thkit.ee\/wp\/","name":"Marija Gorbunova portfoolio TARpv23","description":"J&auml;rgmine WordPress veebileht","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/marijagorbunova23.thkit.ee\/wp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"et"}]}},"_links":{"self":[{"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2068"}],"version-history":[{"count":1,"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2068\/revisions"}],"predecessor-version":[{"id":2073,"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=\/wp\/v2\/pages\/2068\/revisions\/2073"}],"wp:attachment":[{"href":"https:\/\/marijagorbunova23.thkit.ee\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}