﻿
{"id":10509,"date":"2019-03-06T18:51:40","date_gmt":"2019-03-06T18:51:40","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=10509"},"modified":"2019-03-06T18:51:40","modified_gmt":"2019-03-06T18:51:40","slug":"node-js-websockette-socket-id-ile-hedef-clienta-veri-gonderme","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/node-js-websockette-socket-id-ile-hedef-clienta-veri-gonderme\/","title":{"rendered":"Node.js &#8211; Websocket&#8217;te Socket ID \u0130le Hedef Client&#8217;a Veri G\u00f6nderme"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Herhangi bir client, web socket protokol\u00fc \u00fczerinden sunucuya ba\u011flant\u0131 sa\u011flarsa aradaki ba\u011flant\u0131 i\u00e7in Socket ID dedi\u011fimiz benzersiz bir de\u011fer olu\u015fturulur. Haliyle bizler bu olu\u015fturulan id de\u011ferini kullanarak sadece o ba\u011flant\u0131 \u00fczerinden bilgi al\u0131\u015fveri\u015fi yapabiliriz.<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/Node.js-Websockette-Socket-ID-Nedir..png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/Node.js-Websockette-Socket-ID-Nedir..png\" alt=\"Node.js - Websocket&#039;te Socket ID Nedir.\" width=\"441\" height=\"431\" class=\"aligncenter size-full wp-image-10510\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/Node.js-Websockette-Socket-ID-Nedir..png 441w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/Node.js-Websockette-Socket-ID-Nedir.-300x293.png 300w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/a><\/p>\n<p>Yukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnden de g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere, client sayfay\u0131 her yeniledi\u011finde \u00f6nceki id imha edilir ve clienta \u00f6zel yeni bir id \u00fcretilir.<\/p>\n<p>\u015eimdi gelin bir sockete ba\u011flanan t\u00fcm clientlar\u0131n kendi aralar\u0131nda hedef client bazl\u0131 nas\u0131l haberle\u015fti\u011fini inceleyelim.<\/p>\n<h3>Hedef Client&#8217;a Veri G\u00f6nderme<\/h3>\n<p>\u0130lk olarak web socket i\u015flemlerimizi ger\u00e7ekle\u015ftirece\u011fimiz backend taraf\u0131n\u0131 haz\u0131rlayal\u0131m;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst http = require(&quot;http&quot;);\r\nconst socket = require(&quot;socket.io&quot;);\r\n\r\nconst server = http.createServer((request, response) =&gt; {\r\n    response.end(&quot;...&quot;);\r\n});\r\n\r\nconst io = socket.listen(server.listen(1453, () =&gt; console.log(&quot;Started&quot;)));\r\n\r\nconst allClients = &#x5B;];\r\nconst roomNameSpace = io.of(&quot;\/rooms&quot;);\r\nroomNameSpace.on(&quot;connection&quot;, socket =&gt; {\r\n    console.log(&quot;Started&quot;);\r\n    allClients.push(socket.id);\r\n\r\n    socket.emit(&quot;sendSocketId&quot;, socket.id);\r\n\r\n    socket.on(&quot;disconnect&quot;, () =&gt; {\r\n        let socketId = allClients.find(x =&gt; x == socket.id);\r\n        let indexNo = allClients.indexOf(socketId);\r\n        allClients.splice(indexNo, 1);\r\n        console.log(&quot;Closed&quot;);\r\n    });\r\n\r\n    socket.on(&quot;sendMessage&quot;, data =&gt; {\r\n        roomNameSpace.sockets&#x5B;data.socketId].emit(&quot;messages&quot;, {\r\n            socketId: socket.id,\r\n            message: data.message\r\n        });\r\n\r\n        socket.emit(&quot;messages&quot;, {\r\n            socketId: socket.id,\r\n            message: data.message,\r\n            self: true\r\n        });\r\n    });\r\n\r\n    roomNameSpace.emit(&quot;loginClient&quot;, allClients);\r\n});\r\n<\/pre>\n<p>Yukar\u0131da yap\u0131lan i\u015flemleri incelersek e\u011fer;<\/p>\n<ul>\n<li><strong>10. sat\u0131r<\/strong><br \/>\nOlu\u015fturulmu\u015f web sokete ba\u011flanan t\u00fcm clientlar\u0131n socket id de\u011ferlerini tutacak olan koleksiyon tan\u0131mlanmaktad\u0131r.\n<\/li>\n<li><strong>16. sat\u0131r<\/strong><br \/>\nClienta \u00f6zel olu\u015fturulmu\u015f socket id de\u011ferini g\u00f6nderecek eventt\u0131r. Client\u0131n kendine has id de\u011ferini bilmesi i\u00e7in olu\u015fturulmu\u015ftur.\n<\/li>\n<li><strong>25 &#8211; 36. sat\u0131r<\/strong><br \/>\nClientlardan gelecek olan mesaj\u0131 kar\u015f\u0131lamakla sorumlu olan eventt\u0131r.<\/p>\n<ul>\n<li><strong><em>26 &#8211; 29. sat\u0131r<\/em><\/strong><br \/>\nSocket id de\u011feri belirtilen hedef clienta mesaj g\u00f6nderecek eventt\u0131r.\n<\/li>\n<li><strong><em>31 &#8211; 35. sat\u0131r<\/em><\/strong><br \/>\nMesaj g\u00f6nderen clienta mesaj\u0131n\u0131 geri g\u00f6nderen eventt\u0131r.\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Ard\u0131ndan ilgili fonksiyonellikleri kullanacak olan clientlar\u0131n frontend tasar\u0131m\u0131n\u0131 ele alal\u0131m;<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=&quot;en&quot;&gt;\r\n\r\n&lt;head&gt;\r\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/3.3.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/socket.io\/2.2.0\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/js\/bootstrap.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css&quot;&gt;\r\n\r\n    &lt;script&gt;\r\n        const socket = io.connect(&quot;http:\/\/localhost:1453\/rooms&quot;);\r\n        $(() =&gt; {\r\n            let socketId = &quot;&quot;;\r\n            socket.on(&quot;loginClient&quot;, data =&gt; {\r\n                let html = &quot;&quot;;\r\n                $.each(data, (i, n) =&gt; html += `&lt;li style=&quot;cursor: pointer;&quot; class=&quot;client&quot;&gt;${n}&lt;\/li&gt;`);\r\n                $(&quot;#allClients&quot;).html(html);\r\n            });\r\n\r\n            socket.on(&quot;sendSocketId&quot;, data =&gt; $(&quot;#thisSocketId&quot;).html(data));\r\n\r\n            $(&quot;body&quot;).on(&quot;click&quot;, &quot;.client&quot;, () =&gt; {\r\n                $(&quot;#txtSocketId&quot;).val(`Target Client : ${$(event.target).html()}`);\r\n                socketId = $(event.target).html();\r\n            });\r\n\r\n            $(&quot;#btnSendMessage&quot;).click(() =&gt; {\r\n                socket.emit(&quot;sendMessage&quot;, {\r\n                    socketId,\r\n                    message: $(&quot;#txtMessage&quot;).val()\r\n                });\r\n            });\r\n\r\n            socket.on(&quot;messages&quot;, data =&gt; {\r\n                if (!data.self)\r\n                    $(&quot;#messages&quot;).append(`&lt;li&gt;&lt;strong style=&quot;color: green;&quot;&gt;${data.socketId}&lt;\/strong&gt; : ${data.message}&lt;\/li&gt;`);\r\n                else\r\n                    $(&quot;#messages&quot;).append(`&lt;li&gt;&lt;strong&gt;${data.socketId}&lt;\/strong&gt; : ${data.message}&lt;\/li&gt;`);\r\n                $(&quot;#txtMessage&quot;).val(&quot;&quot;);\r\n            });\r\n        });\r\n    &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;h3 id=&quot;thisSocketId&quot;&gt;&lt;\/h3&gt;\r\n    &lt;div class=&quot;row&quot;&quot;&gt;\r\n        &lt;div class=&quot; col-md-6&quot;&gt;\r\n        &lt;ul id=&quot;allClients&quot;&gt;\r\n\r\n        &lt;\/ul&gt;\r\n    &lt;\/div&gt;\r\n    &lt;div class=&quot;col-md-6&quot;&gt;\r\n        &lt;input type=&quot;text&quot; placeholder=&quot;Target Client&quot; id=&quot;txtSocketId&quot; disabled style=&quot;width: 350px;&quot;&gt; &lt;br&gt;\r\n        &lt;textarea name=&quot;&quot; id=&quot;txtMessage&quot; style=&quot;width: 350px;&quot; placeholder=&quot;Message&quot;&gt;&lt;\/textarea&gt;&lt;br&gt;\r\n        &lt;button id=&quot;btnSendMessage&quot;&gt;Send Message&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n    &lt;div class=&quot;col-md-12&quot;&gt;\r\n        &lt;ul id=&quot;messages&quot;&gt;\r\n\r\n        &lt;\/ul&gt;\r\n    &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Burada backend de &#8220;sendMessage&#8221; event\u0131n\u0131 dinleyen fonksiyona g\u00f6z atarsan\u0131z e\u011fer socket id \u00fczerinden hedef clienta mesaj g\u00f6nderilebilmektedir.<br \/>\nDolay\u0131s\u0131yla;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/Node.js-Websockette-Socket-ID-\u0130le-Hedef-Clienta-Veri-G\u00f6nderme.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/Node.js-Websockette-Socket-ID-\u0130le-Hedef-Clienta-Veri-G\u00f6nderme.gif\" alt=\"Node.js - Websocket&#039;te Socket ID \u0130le Hedef Client&#039;a Veri G\u00f6nderme\" width=\"800\" height=\"466\" class=\"aligncenter size-full wp-image-10518\" \/><\/a><br \/>\ni\u015fleyi\u015f yukar\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi ceyran etmekte ve b\u00f6ylece hedef clienta TCP portu \u00fczerinden web socket kullan\u0131larak mesaj iletilmektedir.<\/p>\n<p>\u00d6rnek uygulamay\u0131 indirmek i\u00e7in <a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/03\/NodejsHaberlesme.zip\" rel=\"noopener noreferrer\" target=\"_blank\">buraya<\/a> t\u0131klay\u0131n\u0131z. <span style=\"font-size:11px;color:green;\">Uygulamay\u0131 \u00e7al\u0131\u015ft\u0131rabilmek i\u00e7in <u>npm install<\/u> komutunu \u00e7al\u0131\u015ft\u0131rmay\u0131 unutmay\u0131n\u0131z&#8230;<\/span><\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Herhangi bir client, web socket protokol\u00fc \u00fczerinden sunucuya ba\u011flant\u0131 sa\u011flarsa aradaki ba\u011flant\u0131 i\u00e7in Socket ID dedi\u011fimiz benzersiz bir de\u011fer olu\u015fturulur. Haliyle bizler bu olu\u015fturulan id de\u011ferini kullanarak sadece o ba\u011flant\u0131 \u00fczerinden bilgi al\u0131\u015fveri\u015fi&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":8600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2145],"tags":[2147,2715,2776,2713],"class_list":["post-10509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-node-js","tag-node-js-websocket","tag-web-socket","tag-websocket"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=10509"}],"version-history":[{"count":11,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10509\/revisions"}],"predecessor-version":[{"id":10523,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10509\/revisions\/10523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/8600"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=10509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=10509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=10509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}