{"id":18,"date":"2016-09-21T06:14:51","date_gmt":"2016-09-21T06:14:51","guid":{"rendered":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/?page_id=18"},"modified":"2017-10-26T04:27:20","modified_gmt":"2017-10-26T04:27:20","slug":"%e3%83%86%e3%82%b9%e3%83%88","status":"publish","type":"page","link":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/?page_id=18","title":{"rendered":"\u5099\u5fd8\u9332\uff08websocket\uff09"},"content":{"rendered":"<p>\u5099\u5fd8\u9332<\/p>\n<p>http:\/\/www.atmarkit.co.jp\/ait\/articles\/1603\/14\/news015.html<br \/>\nwebsocket\u3000\u306e\u5c0e\u5165<\/p>\n<p>$ sudo apt install nodejs-legacy<br \/>\n$ sudo apt install npm<\/p>\n<p>~$ node -v<br \/>\nv4.2.6<br \/>\n~$ npm -v<br \/>\n3.5.2<\/p>\n<p>\u3092\u78ba\u8a8d<\/p>\n<p>~\/websocket-chat$ npm install socket.io<\/p>\n<p>~\/websocket-chat\/node_modules\/socket.io<br \/>\n\u304c\u51fa\u6765\u305f\u3053\u3068\u3092\u78ba\u8a8d<\/p>\n<p>websocket-chat\/app.js<br \/>\nwebsocket-chat\/index.html<\/p>\n<p>\u3092\u8ffd\u52a0<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\/\/index.html\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;title&gt;websocket-chat&lt;\/title&gt;\r\n  &lt;link rel=&quot;stylesheet&quot;\r\nhref=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.5\/css\/bootstrap.min.css&quot;&gt;\r\n  &lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.3\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n  &lt;script type=&quot;text\/javascript&quot; src=&quot;\/socket.io\/socket.io.js&quot;&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  &lt;div class=&quot;container&quot;&gt;\r\n    &lt;h1&gt;WebSocket-Chat&lt;\/h1&gt;\r\n      &lt;form class=&quot;form-inline&quot;&gt;\r\n        &lt;div class=&quot;form-group&quot;&gt;\r\n          &lt;label for=&quot;msgForm&quot;&gt;\u30e1\u30c3\u30bb\u30fc\u30b8\uff1a&lt;\/label&gt;\r\n          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;msgForm&quot;&gt;\r\n        &lt;\/div&gt;\r\n          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;\u9001\u4fe1&lt;\/button&gt;\r\n      &lt;\/form&gt;\r\n      &lt;div id=&quot;chatLogs&quot;&gt;&lt;\/div&gt;\r\n  &lt;\/div&gt;\r\n    \r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n        var socket = io.connect();\r\n    \r\n        socket.on(&quot;server_to_client&quot;, function(data){appendMsg(data.value)});\r\n    \r\n        function appendMsg(text) {\r\n            $(&quot;#chatLogs&quot;).append(&quot;&lt;div&gt;&quot; + text + &quot;&lt;\/div&gt;&quot;);\r\n        }\r\n    \r\n        $(&quot;form&quot;).submit(function(e){\r\n            var message = $(&quot;#msgForm&quot;).val();\r\n            $(&quot;#msgForm&quot;).val('');\r\n            socket.emit(&quot;client_to_server&quot;, {value : message});\r\n            e.preventDefault();\r\n        });\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/app.js\r\nvar http = require('http');\r\nvar socketio = require('socket.io');\r\nvar fs = require('fs');\r\nvar server = http.createServer(function(req, res) {\r\n    res.writeHead(200, {'Content-Type' : 'text\/html'});\r\n    res.end(fs.readFileSync(__dirname + '\/index.html', 'utf-8'));\r\n}).listen(3000);  \/\/ \u30dd\u30fc\u30c8\u7af6\u5408\u306e\u5834\u5408\u306f\u5024\u3092\u5909\u66f4\r\n \r\nvar io = socketio.listen(server);\r\n \r\nio.sockets.on('connection', function(socket) {\r\n    socket.on('client_to_server', function(data) {\r\n        io.sockets.emit('server_to_client', {value : data.value});\r\n    });\r\n});\r\n<\/pre>\n<p>~\/websocket-chat$ node app.js<br \/>\n\u306e\u5f8c<br \/>\nhttp:\/\/localhost:3000\/<br \/>\n\u306b\uff12\u3064\u306e\u753b\u9762\u304b\u3089\u30a2\u30af\u30bb\u30b9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5099\u5fd8\u9332 http:\/\/www.atmarkit.co.jp\/ait\/articles\/1603\/14\/news015.html websocket\u3000\u306e\u5c0e\u5165 $ sudo apt install nodejs-legacy &hellip; <a href=\"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/?page_id=18\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">\u5099\u5fd8\u9332\uff08websocket\uff09<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-18","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/pages\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":13,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/pages\/18\/revisions"}],"predecessor-version":[{"id":145,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/pages\/18\/revisions\/145"}],"up":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress_tamura\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}