diff --git a/app/assets/javascripts/channels/tv.coffee b/app/assets/javascripts/channels/tv.coffee new file mode 100644 index 0000000..c5f37d5 --- /dev/null +++ b/app/assets/javascripts/channels/tv.coffee @@ -0,0 +1,14 @@ +App.tv = App.cable.subscriptions.create "TvChannel", + connected: -> + # Called when the subscription is ready for use on the server + + disconnected: -> + # Called when the subscription has been terminated by the server + + received: (data) -> + # Called when there's incoming data on the websocket for this channel + console.info(data) + + put_message: () -> + @perform("put_message") + return diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb index 0ff5442..53d3027 100644 --- a/app/channels/application_cable/connection.rb +++ b/app/channels/application_cable/connection.rb @@ -1,4 +1,15 @@ module ApplicationCable class Connection < ActionCable::Connection::Base + identified_by :current_user + + def connect + self.current_user = find_verified_user + end + + protected + + def find_verified_user # this checks whether a user is authenticated with devise + env["warden"].user || reject_unauthorized_connection + end end end diff --git a/app/channels/tv_channel.rb b/app/channels/tv_channel.rb new file mode 100644 index 0000000..b95450b --- /dev/null +++ b/app/channels/tv_channel.rb @@ -0,0 +1,15 @@ +class TvChannel < ApplicationCable::Channel + # クライアントと接続された時 + def subscribed + stream_from "tv:message" + end + + # クライアントとの接続が解除された時 + def unsubscribed + # Any cleanup needed when channel is unsubscribed + end + + def put_message + TvChannel.broadcast_to("message", "hello") + end +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index a4d3a95..6a97a5b 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -6,4 +6,7 @@ def index def dashboard end + + def tv + end end diff --git a/app/views/pages/tv.html.erb b/app/views/pages/tv.html.erb new file mode 100644 index 0000000..71d5702 --- /dev/null +++ b/app/views/pages/tv.html.erb @@ -0,0 +1,2 @@ +

Pages#tv

+

Find me in app/views/pages/tv.html.erb

diff --git a/config/routes.rb b/config/routes.rb index 97d4f67..9ef490e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ Rails.application.routes.draw do root "pages#index" get "dashboard", to: "pages#dashboard" + get "tv", to: "pages#tv" resources :users, only: [:show, :edit, :update], path: "/", param: :name, constraints: { name: /@\w+/ } do resources :meals, param: :date, constraints: { date: %r{\d{4}\/\d{2}\/\d{2}} } do