わからないこと
Railsでチャットアプリを作成しております。channelを使用し、フロント側からく送った情報をサーバで受け取り、チャンネル名を指定しブロードキャストでフロント側のアラートを表示させようとしているのですが、アラートとして表示されず、画面に出力されてしまいます。どなたか分かる方いらっしゃいますでしょうか?
ソース
room.js
App.room = App.cable.subscriptions.create("RoomChannel", { connected: function() { console.log('connected') // Called when the subscription is ready for use on the server }, disconnected: function() { // Called when the subscription has been terminated by the server }, received: function(data) { alert(data) // Called when there's incoming data on the websocket for this channel }, speak: function() { return this.perform('speak', {message: 'aaa'} ); } });
room_channel.rb
class RoomChannel < ApplicationCable::Channel def subscribed # stream_from "some_channel" end def unsubscribed # Any cleanup needed when channel is unsubscribed end def speak(data) Message.create!(content: data['message']) ActionCable.server.broadcast 'room_channel', data['message'] end end
環境情報
Rails: 5.1.7
ruby: 2.6.3
IDE: AWSのCloud 9
ブラウザ: Chrome
ブラウザ画面
回答1件
あなたの回答
tips
プレビュー