{"id":3408,"date":"2020-03-31T09:00:49","date_gmt":"2020-03-31T00:00:49","guid":{"rendered":"https:\/\/www.sambuichi.jp\/?p=3408"},"modified":"2020-03-31T13:14:19","modified_gmt":"2020-03-31T04:14:19","slug":"javascript%e3%82%af%e3%83%a9%e3%82%a4%e3%82%a2%e3%83%b3%e3%83%88%e3%82%92socket-io%e3%81%a7","status":"publish","type":"post","link":"https:\/\/www.sambuichi.jp\/?p=3408","title":{"rendered":"socket.io\u3067Javascript\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u958b\u767a"},"content":{"rendered":"<p>Views: 74<\/p><link rel=\"stylesheet\" href=\"https:\/\/use.fontawesome.com\/releases\/v5.6.3\/css\/all.css\"\n      integrity=\"sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s\/\"\n      crossorigin=\"anonymous\">\n<div class=\"index\" style=\"width: 90%; margin: auto; padding-left: 8px; border: solid 1px #d0d0d0;\">\n<div style=\"text-align: center;\">\u76ee\u6b21<\/div>\n<p style=\"margin: 0;\">\n  <a href=\"#S1\">\u6982\u8981<\/a><br \/>\n  <a href=\"#S2\">Nginx\u306econf\u30d1\u30e9\u30e1\u30bf\u8a2d\u5b9a<\/a><br \/>\n  <a href=\"#S3\">\u30d7\u30ed\u30b0\u30e9\u30e0\u4fee\u6b63<\/a>\n<\/p>\n<\/div>\n<p><span id=\"S1\"><\/span><\/p>\n<h4>\u6982\u8981<\/h4>\n<p>\nAmazon EC2\u30b5\u30fc\u30d0\u30fc\u306eNginx\u3092Node.js(Express)\u3078\u306e\u30d7\u30ed\u30ad\u30b7\u30fc\u3068\u3057\u3066\u8a2d\u5b9a\u3057\u3066GitHub\u306esocketio\/socket.io\u306b\u3042\u308bchat\u306e\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e9\u30e0\u3092\u518d\u73fe\u3059\u308b\u3002\u30b5\u30a4\u30c8\u306eHow to use\u306b\u306f\u3001\u6b21\u306e\u8a18\u8ff0\u304c\u3042\u308b\u304c\u3001\u74b0\u5883\u8a2d\u5b9a\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4e00\u90e8\u4fee\u6b63\u3057\u305f\u3002\n<\/p>\n<pre class=\"lang:default decode:true \" >$ cd socket.io\r\n$ npm install\r\n$ cd examples\/chat\r\n$ npm install\r\n$ npm start<\/pre>\n<p><i class=\"fas fa-quote-left fa-2x\"><\/i>And point your browser to http:\/\/localhost:3000. Optionally, specify a port by supplying the PORT env variable.<i class=\"fas fa-quote-right\"><\/i><\/p>\n<p>http:\/\/localhost:3000\u3067\u306f\u306a\u304fEC2\u30db\u30b9\u30c8\u3092\u6307\u5b9a\u3059\u308b\u305f\u3081\u306b\u4ee5\u964d\u306e\u5909\u66f4\u3092\u884c\u3063\u305f\u3002<\/p>\n<p><span id=\"S2\"><\/span><\/p>\n<h4>Nginx\u306econf\u30d1\u30e9\u30e1\u30bf\u8a2d\u5b9a<\/h4>\n<p><a href=\"https:\/\/www.sambuichi.jp\/?p=3334#S5\">NGINX\u3092WebSocket Proxy\u306b\u4f7f\u3046\uff08Amazon EC2\uff09<\/a>\u3067\u7d39\u4ecb\u3057\u305f\u8a2d\u5b9a\u306b\u9759\u7684\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u306elocation ~ ^\/chat\/(.*)$\u3092\u8ffd\u52a0\u3059\u308b\u3002\u30b5\u30f3\u30d7\u30eb\u306echat\/public\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u3001\/your\/web\/example\/chat\/\u306e\u4e0b\u306b\u914d\u7f6e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \" >map $http_upgrade $connection_upgrade {\r\n    default upgrade;\r\n    '' close;\r\n}\r\n\r\nupstream websocket {\r\n    server 127.0.0.1:3000;\r\n}\r\n   \r\nserver {\r\n    server_name example.host.com;\r\n\r\n    \u7565\r\n\r\n    location ~ ^\/chat\/(.*)$ {\r\n        alias \/your\/web\/example\/chat\/$1;\r\n    }\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/websocket;\r\n        proxy_http_version 1.1;\r\n        proxy_set_header Upgrade $http_upgrade;\r\n        proxy_set_header Connection $connection_upgrade;\r\n        proxy_set_header Host $host;\r\n    }\r\n}<\/pre>\n<p><span id=\"S3\"><\/span><\/p>\n<h4>GitHub\u30d7\u30ed\u30b0\u30e9\u30e0\u4fee\u6b63<\/h4>\n<p>chat\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001\u5909\u66f4\u3057\u305f\u306e\u306f\u6b21\u306e\u7b87\u6240\u3002socket.io\u30e9\u30a4\u30d6\u30e9\u30ea\u3092package.json\u306b\u8ffd\u52a0\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var server = require('http').Server(app);\r\nvar io = require('socket.io')(server);<\/pre>\n<pre class=\"lang:js decode:true \" title=\"package.json\" >{\r\n  \"name\": \"socket.io-chat\",\r\n  \"version\": \"0.0.0\",\r\n  \"description\": \"A simple chat client using socket.io\",\r\n  \"main\": \"index.js\",\r\n  \"author\": \"Grant Timmerman\",\r\n  \"private\": true,\r\n  \"license\": \"BSD\",\r\n  \"dependencies\": {\r\n    \"express\": \"4.13.4\",\r\n    \"socket.io\": \"^1.7.3\"\r\n  },\r\n  \"scripts\": {\r\n    \"start\": \"node index.js\"\r\n  }\r\n}<\/pre>\n<p>\u307e\u305f\u3001index.html\u306esocket.io\u3078\u306e\u30d1\u30b9\u3092\u5909\u66f4\u3057\u3066\u6b21\u306e\u3088\u3046\u306b\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \" >&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/socket.io-client@2\/dist\/socket.io.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"main.js\"&gt;<\/script><\/pre>\n<p>\u3053\u306e\u5f8c<\/p>\n<pre class=\"lang:default decode:true \" >$ npm install\r\n$ npm start<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001<a href=\"https:\/\/www.sambuichi.jp\/?p=3383\">Node\u30b5\u30fc\u30d0\u30fc\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u958b\u767a<\/a>\u306b\u5f93\u3063\u305f\u624b\u9806\u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u305f\u3002<br \/>\n<a  href=\"https:\/\/www.sambuichi.jp\/wp-content\/uploads\/2020\/03\/dd45f9b0fba490e835c8c28e98421109.png\" data-rel=\"lightbox-gallery-0\" data-rl_title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2020-03-31 11.52.38\" data-rl_caption=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2020-03-31 11.52.38\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.sambuichi.jp\/wp-content\/uploads\/2020\/03\/dd45f9b0fba490e835c8c28e98421109-1024x647.png\" alt=\"\" width=\"1024\" height=\"647\" class=\"alignnone size-large wp-image-3428\" srcset=\"https:\/\/www.sambuichi.jp\/wp-content\/uploads\/2020\/03\/dd45f9b0fba490e835c8c28e98421109-1024x647.png 1024w, https:\/\/www.sambuichi.jp\/wp-content\/uploads\/2020\/03\/dd45f9b0fba490e835c8c28e98421109-300x190.png 300w, https:\/\/www.sambuichi.jp\/wp-content\/uploads\/2020\/03\/dd45f9b0fba490e835c8c28e98421109-768x486.png 768w, https:\/\/www.sambuichi.jp\/wp-content\/uploads\/2020\/03\/dd45f9b0fba490e835c8c28e98421109-110x70.png 110w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>\u53c2\u8003\uff1a<a href=\"https:\/\/socket.io\/get-started\/chat\/\">https:\/\/socket.io\/get-started\/chat\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Views: 74 \u76ee\u6b21 \u6982\u8981 Nginx\u306econf\u30d1\u30e9\u30e1\u30bf\u8a2d\u5b9a \u30d7\u30ed\u30b0\u30e9\u30e0\u4fee\u6b63 \u6982\u8981 Amazon EC2\u30b5\u30fc\u30d0\u30fc\u306eNginx\u3092Node.js(Express)\u3078\u306e\u30d7\u30ed\u30ad\u30b7\u30fc\u3068\u3057\u3066\u8a2d\u5b9a\u3057\u3066GitHub\u306esocketio [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3425,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts\/3408"}],"collection":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3408"}],"version-history":[{"count":31,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts\/3408\/revisions"}],"predecessor-version":[{"id":3443,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/posts\/3408\/revisions\/3443"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=\/wp\/v2\/media\/3425"}],"wp:attachment":[{"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sambuichi.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}