質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

4612閲覧

ActionCableとDeviseがうまくつながらない

a.hamaaaaaaaa

総合スコア12

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

2クリップ

投稿2018/05/16 00:22

編集2022/01/12 10:55

いま、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()

何卒よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

An unauthorized connection attempt was rejected
というエラーを見る限り、うまくUserを認証できていないらしいですね。

以下コードでUserが探せていないようです。

ruby

1 def find_verified_user 2 if verified_user = User.find_by(id: cookies.encrypted[:user_id]) 3 verified_user 4 else 5 reject_unauthorized_connection 6 end 7 end

まず、cookies.encrypted[:user_id]がnilでないか、loggerなどで確認してみてはいかがでしょうか?
もしnilの場合は、ChromeのDeveloper toolのNetworkタブなので、ActionCableの通信でcookieが送信されているか確認してみてはどうでしょう。

また、試していませんが、cookies.encryptedではなく、cookies.signed[:user_id]かもしれません。(Railsガイドを見る限り: https://railsguides.jp/action_cable_overview.html)

投稿2018/05/28 23:40

umeneri

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

a.hamaaaaaaaa

2018/05/28 23:42

ありがとうございます! 一度修正してみます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問