Skip to content

Commit

Permalink
rails g channel tv
Browse files Browse the repository at this point in the history
  • Loading branch information
defaultcf committed Oct 27, 2017
1 parent 385e929 commit c9c61e9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/assets/javascripts/channels/tv.coffee
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions app/channels/tv_channel.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ def index

def dashboard
end

def tv
end
end
2 changes: 2 additions & 0 deletions app/views/pages/tv.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Pages#tv</h1>
<p>Find me in app/views/pages/tv.html.erb</p>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9c61e9

Please sign in to comment.