Implement room channel
•Roomチャンネルの作成
2016/12/13 9
# rails g channel room speak
Running via Spring preloader in process 12363
Expected string default value for '--jbuilder'; got true (boolean)
create app/channels/room_channel.rb
identical app/assets/javascripts/cable.js
create app/assets/javascripts/channels/room.coffee
10.
Implement room channel
•Roomチャンネルの作成
2016/12/13 10
# rails g channel room speak
Running via Spring preloader in process 12363
Expected string default value for '--jbuilder'; got true (boolean)
create app/channels/room_channel.rb
identical app/assets/javascripts/cable.js
create app/assets/javascripts/channels/room.coffee
11.
Implement room channel
•Roomチャンネルの作成
• app/channels/room_channel.rb
• WebSocketサーバー ⇒ クライアント
• app/assets/javascripts/channels/room.coffee
• クライアント ⇒ WebSocketサーバー
2016/12/13 11
# rails g channel room speak
Running via Spring preloader in process 12363
Expected string default value for '--jbuilder'; got true (boolean)
create app/channels/room_channel.rb
identical app/assets/javascripts/cable.js
create app/assets/javascripts/channels/room.coffee
12.
Implement room channel
•app/channels/room_channel.rb
2016/12/13 12
class RoomChannel < ApplicationCable::Channel
def subscribed
stream_from "room_channel“
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def speak(data)
ActionCable.server.broadcast 'room_channel', message: data['message']
end
end
13.
Implement room channel
•app/assets/javascripts/channels/room.coffee
2016/12/13 13
App.room = App.cable.subscriptions.create "RoomChannel",
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
h = $("<p>" + data["message"] + "</p>")
$(".messages").append(h)
$("input.speak").val("")
speak: (data)->
@perform 'speak', data