{"id":24,"date":"2010-12-08T23:38:00","date_gmt":"2010-12-08T14:38:00","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=24"},"modified":"2017-03-09T10:16:28","modified_gmt":"2017-03-09T01:16:28","slug":"post-24","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/24","title":{"rendered":"[node.js]WebSocket \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u300csocket.io\u300d\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb"},"content":{"rendered":"<p><span class=\"removed_link\" title=\"http:\/\/socket.io\/\">socket.io<\/span> \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<pre class=\"lang:sh decode:true \" >$ npm install socket.io<\/pre>\n<p>socketserver.js \u3092\u4f5c\u6210<\/p>\n<pre class=\"lang:js decode:true \" >var app = require('express').createServer();\r\nvar socket_server = app.get('\/', function(req, res){\r\n  res.send('hello socket.io');\r\n});\r\nsocket_server.listen(8000);\r\n\r\nvar io = require('socket.io'); \r\nvar socket = io.listen(socket_server);\r\nsocket.on('connection', function(client){\r\n  client.on('message', function(data)\r\n  {\r\n    client.send(data);\r\n    client.broadcast(data);\r\n  })\r\n  client.on('disconnect', function()\r\n  {\r\n    client.broadcast(client.sessionId + 'disconnected');\r\n  })\r\n\r\n});<\/pre>\n<p>socketserver.js\u8d77\u52d5<\/p>\n<pre class=\"lang:sh decode:true \" >$ node socketserver.js<\/pre>\n<p>http:\/\/{\u30bd\u30b1\u30c3\u30c8\u30b5\u30fc\u30d0\u30fc\u30a2\u30c9\u30ec\u30b9}:8000\/ \u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u300chello socket.io\u300d\u3068\u8868\u793a\u3055\u308c\u308c\u3070\uff2f\uff2b<\/p>\n<p>index.html\u3092\u5909\u66f4\u3059\u308b<\/p>\n<pre class=\"lang:xhtml decode:true \" >\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=&quot;ja&quot;&gt;\r\n&lt;head&gt;\r\n  &lt;meta charset=&quot;utf-8&quot; \/&gt;\r\n  &lt;script src=&quot;http:\/\/{socket server}:8000\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n  &lt;script&gt;\r\n    socket = new io.Socket('{socket server}:8000',{port:8000});\r\n    socket.connect();\r\n    socket.send('hi!');\r\n    socket.on('message', function(data){\r\n      alert(data);\r\n    });\r\n  &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  socket test\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>\u300chi!\u300d\u3068\u30a2\u30e9\u30fc\u30c8\u304c\u51fa\u308c\u3070\uff2f\uff2b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>socket.io \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb $ npm in&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5090,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":6,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":389,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/24\/revisions\/389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/5090"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}