いま、rails5をつかって、グループコミュニケーションプラットフォームを作っています。
その中の機能としてリアルタイムチャット機能をactioncableでつくっているのですが、
うまく実装できなくて悩んでいます。
初心者の思いつきでやってしまっていることなので、
温かい目で見てやってください。
現状:
・consoleにて、App.room.speak("内容")を実行すると、falseが帰ってきてしまう。
・https://qiita.com/kohei1228/items/7aed5aad9c63e834c0e1 を参照
以下エラー文
An unauthorized connection attempt was rejected Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
以下、コード
ruby
1App.room = App.cable.subscriptions.create {channel: "RoomChannel"}, 2 connected: -> 3 # Called when the subscription is ready for use on the server 4 5 disconnected: -> 6 # Called when the subscription has been terminated by the server 7 8 received: (data) -> 9 $('#messages').append data['message'] 10 11 speak: (message)-> 12 @perform 'speak', message: message
ruby
1class RoomChannel < ApplicationCable::Channel 2 def 3 stream_from "room_channel_#{params['room_id']}" 4 end 5 6 def unsubscribed 7 # Any cleanup needed when channel is unsubscribed 8 end 9 10 def speak(data) 11 Message.create(content: data['message'], sent_user: current_user, room: Room.find(params['room_id'])) 12 end 13end 14
ruby
1module ApplicationCable 2 class Connection < ActionCable::Connection::Base 3 identified_by :current_user 4 5 def connect 6 self.current_user = find_verified_user 7 end 8 9 private 10 def find_verified_user 11 if verified_user = User.find_by(id: cookies.encrypted[:user_id]) 12 verified_user 13 else 14 reject_unauthorized_connection 15 end 16 end 17 end 18end
ruby
1class MessageBroadcastJob < ApplicationJob 2 queue_as :default 3 4 def perform(message) 5 ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message) 6 end 7 8 private 9 10 def render_message(message) 11 ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message }) 12 end 13end 14
javascript
1// This is a manifest file that'll be compiled into application.js, which will include all the files 2// listed below. 3// 4// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 5// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. 6// 7// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 8// compiled file. JavaScript code in this file should be added after the last require_* statement. 9// 10// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details 11// about supported directives. 12// 13//= require jquery 14//= require jquery_ujs 15//= require turbolinks 16//= require_tree . 17 18jQuery(document).on 'turbolinks:load', -> 19 messages = $('#messages') 20 if $('#messages').length > 0 21 22$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) -> 23 if event.keyCode is 13 # return = send 24 App.room.speak event.target.value 25 event.target.value = '' 26 event.preventDefault()
何卒よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/28 23:42