{"id":3168,"date":"2013-09-09T13:29:10","date_gmt":"2013-09-09T04:29:10","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=3168"},"modified":"2018-11-14T16:33:54","modified_gmt":"2018-11-14T07:33:54","slug":"actioncontrollerlive%e3%81%a8server-sent-events-%e3%81%a7%e5%9c%b0%e5%9b%b3%e4%b8%8a%e3%81%ab%e3%81%98%e3%82%8f%e3%81%98%e3%82%8f%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/09\/09\/actioncontrollerlive%e3%81%a8server-sent-events-%e3%81%a7%e5%9c%b0%e5%9b%b3%e4%b8%8a%e3%81%ab%e3%81%98%e3%82%8f%e3%81%98%e3%82%8f%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b\/","title":{"rendered":"ActionController::Live\u3068Server-Sent Events \u3067\u5730\u56f3\u4e0a\u306b\u3058\u308f\u3058\u308f\u8868\u793a\u3059\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Rails4 \u306e\u65b0\u6a5f\u80fd\u306e\u4e00\u3064\u306b ActionController::Live \u3068\u3044\u3046\u3082\u306e\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>ActionController::Live \u306f\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u306e Push \u901a\u77e5\u3092\u53ef\u80fd\u306b\u3059\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u300c\u30c1\u30e3\u30c3\u30c8\u304c\u3067\u304d\u308b\u3088\u3002\u300d\u300c\u307e\u305f\u304b\u3088\uff01\u300d<\/h2>\n<p>\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0 Push \u901a\u4fe1\u3092\u884c\u3046\u4ed6\u306e\u6280\u8853\u3068\u3057\u3066\u306f\u3001Commet \u3084 WebSocket \u306a\u3069\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u7cfb\u306e\u8a71\u306e\u5fdc\u7528\u4f8b\u3068\u3057\u3066\u826f\u304f\u6319\u3052\u3089\u308c\u308b\u306e\u306f\u30c1\u30e3\u30c3\u30c8\u3067\u3059\u304c\u30fb\u30fb\u3001\u305d\u3093\u306a\u306b\u3044\u3064\u3082\u30c1\u30e3\u30c3\u30c8\u3092\u4f5c\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u300c\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0 Push \u901a\u4fe1\u304c\u3067\u304d\u308b\u25cb\u25cb\u767b\u5834\uff01\u30c1\u30e3\u30c3\u30c8\u304c\u3067\u304d\u308b\u3088\u3002\u300d\u2192\u300c\u307e\u305f\u304b\u3088\uff01\u300d\u3068\u601d\u3046\u3053\u3068\u3082\u591a\u3044\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3068\u306f\u3044\u3048\u300c\u30c1\u30e3\u30c3\u30c8\u4ee5\u5916\u306e\u5fdc\u7528\u4f8b\u3063\u3066\u4f55\uff1f\u300d\u3068\u8a00\u308f\u308c\u3066\u3082\u3059\u3050\u306b\u306f\u826f\u3044\u30a2\u30a4\u30c7\u30a3\u30a2\u304c\u6d6e\u304b\u3070\u306a\u3044\u306e\u3082\u4e8b\u5b9f\u3067\u3059\u3002\u300c\u3053\u3046\u3044\u3046\u4f7f\u3044\u65b9\u304c\u3067\u304d\u308b\u3093\u3058\u3083\u306a\u3044\uff1f\u300d\u3068\u3044\u3046\u601d\u3044\u3064\u304d\u3092\u5f62\u306b\u3059\u308b\u3053\u3068\u3092\u7a4d\u307f\u91cd\u306d\u3066\u3044\u3051\u3070\u3001\u3044\u305a\u308c\u300c\u3053\u308c\u305e\uff01\u300d\u3068\u3044\u3046\u5fdc\u7528\u304c\u898b\u3064\u304b\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5730\u56f3\u4e0a\u306b\u30de\u30fc\u30ab\u30fc\u3092\u3058\u308f\u3058\u308f\u8868\u793a\u3059\u308b\u30b5\u30f3\u30d7\u30eb<\/h2>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u601d\u3044\u3064\u304d\u306e\u4e00\u3064\u3068\u3057\u3066\u5730\u56f3\u4e0a\u306b\u30de\u30fc\u30ab\u30fc\u3092\u3058\u308f\u3058\u308f\u8868\u793a\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>Heroku \u306b\u30b5\u30f3\u30d7\u30eb\u3092\u30c7\u30d7\u30ed\u30a4\u3057\u307e\u3057\u305f\u3002\u4ee5\u4e0b\u306e URL \u3067\u78ba\u8a8d\u3067\u304d\u308b\u306e\u3067\u3001\u826f\u3051\u308c\u3070\u89e6\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-3172\" alt=\"jiwajiwa-maps\" src=\"https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/09\/jiwajiwa-maps-300x256.png\" width=\"300\" height=\"256\" srcset=\"https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/09\/jiwajiwa-maps-300x256.png 300w, https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/09\/jiwajiwa-maps.png 472w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><a href=\"http:\/\/techscore-ac-live-sample.herokuapp.com\/\">http:\/\/techscore-ac-live-sample.herokuapp.com\/<\/a><\/p>\n<p>\u5730\u56f3\u3092\u30c9\u30e9\u30c3\u30b0\u3059\u308b\u3068\u3001\u753b\u9762\u5916\u3068\u306a\u3063\u305f\u30de\u30fc\u30ab\u30fc\u306f\u524a\u9664\u3055\u308c\u308b\u3068\u3068\u3082\u306b\u3001\u65b0\u3057\u304f\u753b\u9762\u5916\u306b\u5165\u3063\u305f\u9818\u57df\u306b\u8868\u793a\u3059\u3079\u304d\u30de\u30fc\u30ab\u30fc\u306e\u60c5\u5831\u304c\u3058\u308f\u3058\u308f\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u5834\u6240\u306b\u3042\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/suzuki-kei\/rails4_action_controller_live_with_google_maps_sample\">https:\/\/github.com\/suzuki-kei\/rails4_action_controller_live_with_google_maps_sample<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u89e3\u8aac<\/h2>\n<h3>Model<\/h3>\n<p>\u5730\u56f3\u4e0a\u306b\u8868\u793a\u3059\u308b\u30de\u30fc\u30ab\u30fc\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u306f\u3001\u300c\u540d\u524d\u300d\u300c\u7def\u5ea6\u300d\u300c\u7d4c\u5ea6\u300d\u306e\u60c5\u5831\u3092\u6301\u305f\u305b\u307e\u3057\u305f\u3002<\/p>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30b3\u30fc\u30c9 ( <a href=\"https:\/\/github.com\/suzuki-kei\/rails4_action_controller_live_with_google_maps_sample\/blob\/master\/db\/migrate\/20130907160100_create_markers.rb\">db\/migrate\/20130907160100_create_markers.rb<\/a> ) \u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class CreateMarkers &lt; ActiveRecord::Migration\r\n\r\n  def change\r\n    create_table :markers do |table|\r\n      # \u540d\u524d.\r\n      table.string :name, null: false\r\n\r\n      # \u7def\u5ea6.\r\n      table.decimal :latitude, null: false\r\n\r\n      # \u7d4c\u5ea6.\r\n      table.decimal :longitude, null: false\r\n    end\r\n  end\r\n\r\nend<\/pre>\n<p>&nbsp;<\/p>\n<h3>Controller<\/h3>\n<p>Controller \u306f\u6b21\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class IndexController &lt; ApplicationController\r\n\r\n  include ActionController::Live\r\n\r\n  def index\r\n  end\r\n\r\n  def markers\r\n    response.header['Content-Type'] = 'text\/event-stream'\r\n\r\n    latitude = Range.new(*[params[:north], params[:south]].sort)\r\n    longitude = Range.new(*[params[:east], params[:west]].sort)\r\n    exclude_ids = params[:excludes].to_s.split(',')\r\n\r\n    Marker.where(latitude: latitude, longitude: longitude).where.not(id: exclude_ids).each do |marker|\r\n      logger.debug(marker.inspect)\r\n      response.stream.write(\"event: marker\\n\")\r\n      response.stream.write(\"data: #{marker.attributes.to_json}\\n\\n\")\r\n      sleep 0.1\r\n    end\r\n\r\n    response.stream.write(\"event: done\\n\")\r\n    response.stream.write(\"data: bye\\n\\n\")\r\n  rescue IOError\r\n    # \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u304b\u3089 close \u3055\u308c\u305f\u5834\u5408\u306e\u4f8b\u5916\u306f\u7121\u8996\u3059\u308b.\r\n  ensure\r\n    response.stream.close\r\n  end\r\n\r\nend<\/pre>\n<p>\u30dd\u30a4\u30f3\u30c8\u306f\u3001Content-Type \u306f text\/event-stream \u3067\u8fd4\u3059\u3053\u3068\u3068\u3001\u30c7\u30fc\u30bf\u3092\u4e00\u3064\u305a\u3064 response.stream.write \u3067\u51fa\u529b\u3057\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u300csleep 0.1\u300d\u3092\u5165\u308c\u3066\u3044\u308b\u306e\u306f\u3001\u308f\u3056\u3068\u51e6\u7406\u3092\u9045\u304f\u3057\u3066\u30de\u30fc\u30ab\u30fc\u304c\u3058\u308f\u3058\u308f\u8868\u793a\u3055\u308c\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>View<\/h3>\n<p>View \u306f\u3053\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;%= javascript_tag do %&gt;\r\n    $(function() {\r\n        if(window.EventSource) {\r\n            new Map(\"map\");\r\n        } else {\r\n            $(\"#map\").text(\"EventSource \u3092\u5229\u7528\u3067\u304d\u308b\u30d6\u30e9\u30a6\u30b6\u3067\u30a2\u30af\u30bb\u30b9\u3057\u3066\u304f\u3060\u3055\u3044\u3002\");\r\n        }\r\n    });\r\n&lt;% end %&gt;\r\n&lt;div id=\"map\"&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>Server-Sent Events (EventSource) \u304c\u4f7f\u3048\u306a\u3051\u308c\u3070\u30b4\u30e1\u30f3\u30ca\u30b5\u30a4\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4f7f\u3048\u308b\u5834\u5408\u306f\u72ec\u81ea\u306b\u4f5c\u6210\u3057\u305f Map \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u4ed5\u4e8b\u306e\u5168\u3066\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>Javascript<\/h3>\n<p>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b3\u30fc\u30c9\u306e\u30e1\u30a4\u30f3\u306f <a href=\"https:\/\/github.com\/suzuki-kei\/rails4_action_controller_live_with_google_maps_sample\/blob\/master\/app\/assets\/javascripts\/map.js\">app\/assets\/javascripts\/map.js<\/a> \u306b\u9589\u3058\u8fbc\u3081\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>100 \u884c\u5f31\u306e\u30b3\u30fc\u30c9\u3067\u3059\u304c\u3001\u5730\u56f3\u304c\u30c9\u30e9\u30c3\u30b0\u3055\u308c\u305f\u308a\u500d\u7387\u304c\u5909\u66f4\u3055\u308c\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u4ee5\u4e0b\u306e\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u307e\u3059\uff08mapChanged \u95a2\u6570\uff09\u3002<\/p>\n<ol>\n<li>\u30b5\u30fc\u30d0\u3068\u901a\u4fe1\u4e2d\u306e\u5834\u5408\u306f\u3001\u901a\u4fe1\u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u3002\uff08cancelRequest\uff09<\/li>\n<li>\u5730\u56f3\u306e\u7bc4\u56f2\u5916\u3068\u306a\u3063\u305f\u30de\u30fc\u30ab\u30fc\u3092\u524a\u9664\u3059\u308b\u3002\uff08removeOutsideMarkers\uff09<\/li>\n<li>\u30b5\u30fc\u30d0\u304b\u3089\u65b0\u3057\u3044\u7bc4\u56f2\u306b\u542b\u307e\u308c\u308b\u30de\u30fc\u30ab\u30fc\u3092\u53d6\u5f97\u3059\u308b\u3002\uff08requestMarkers\uff09<\/li>\n<\/ol>\n<p>1. \u306f\u30b5\u30fc\u30d0\u5074\u306b\u51e6\u7406\u304c\u6e9c\u307e\u3089\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u30012. \u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u306e\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u304c\u5897\u3048\u7d9a\u3051\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u30013 \u306e\u51e6\u7406\u3067\u306f\u753b\u9762\u5185\u306b\u65e2\u306b\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u30de\u30fc\u30ab\u30fc\u306e\u60c5\u5831\u3092\u30b5\u30fc\u30d0\u306b\u9001\u4fe1\u3057\u3001\u30b5\u30fc\u30d0\u5074\u306f\u4e0d\u8db3\u5206\u306e\u30de\u30fc\u30ab\u30fc\u60c5\u5831\u3060\u3051\u8fd4\u305b\u308b\u3088\u3046\u306b\u5de5\u592b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>ActionController::Live \u306e\u5229\u7528\u4f8b\u3068\u3057\u3066\u3001\u307e\u305f\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0 Push \u901a\u4fe1\u6d3b\u7528\u306e\u4e00\u4f8b\u3068\u3057\u3066\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u4f55\u304b\u306e\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Rails4 \u306e\u65b0\u6a5f\u80fd\u306e\u4e00\u3064\u306b ActionController::Live \u3068\u3044\u3046\u3082\u306e\u304c\u3042\u308a\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/09\/09\/actioncontrollerlive%e3%81%a8server-sent-events-%e3%81%a7%e5%9c%b0%e5%9b%b3%e4%b8%8a%e3%81%ab%e3%81%98%e3%82%8f%e3%81%98%e3%82%8f%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[108,22,109],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3168"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=3168"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3168\/revisions"}],"predecessor-version":[{"id":17912,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3168\/revisions\/17912"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=3168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=3168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=3168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}