質問編集履歴
2
コード、現状の更新、修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,29 +5,19 @@
|
|
5
5
|
温かい目で見てやってください。
|
6
6
|
|
7
7
|
現状:
|
8
|
-
・console
|
8
|
+
・consoleにて、App.room.speak("内容")を実行すると、falseが帰ってきてしまう。
|
9
|
-
データベースにそれが保存されない。
|
10
|
-
・
|
9
|
+
・https://qiita.com/kohei1228/items/7aed5aad9c63e834c0e1 を参照
|
11
|
-
(ActionCableの動作の中でどう抜けばいいのかがよくわかっていない)
|
12
10
|
|
13
|
-
```ruby
|
14
|
-
class ChatChannel < ApplicationCable::Channel
|
15
|
-
def subscribed
|
16
|
-
stream_from "chat_channel"
|
17
|
-
end
|
18
11
|
|
19
|
-
|
12
|
+
以下エラー文
|
20
|
-
|
13
|
+
`An unauthorized connection attempt was rejected
|
21
|
-
|
14
|
+
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)`
|
22
15
|
|
23
|
-
def put_message(data)
|
24
|
-
Message.create! { content: data['message'], user: @user, group: @group }
|
25
|
-
end
|
26
|
-
end
|
27
16
|
|
28
|
-
|
17
|
+
以下、コード
|
18
|
+
|
29
19
|
```ruby
|
30
|
-
App.
|
20
|
+
App.room = App.cable.subscriptions.create {channel: "RoomChannel"},
|
31
21
|
connected: ->
|
32
22
|
# Called when the subscription is ready for use on the server
|
33
23
|
|
@@ -35,20 +25,26 @@
|
|
35
25
|
# Called when the subscription has been terminated by the server
|
36
26
|
|
37
27
|
received: (data) ->
|
38
|
-
|
28
|
+
$('#messages').append data['message']
|
39
29
|
|
40
|
-
|
30
|
+
speak: (message)->
|
41
|
-
@perform '
|
31
|
+
@perform 'speak', message: message
|
42
32
|
```
|
43
33
|
```ruby
|
44
|
-
class
|
34
|
+
class RoomChannel < ApplicationCable::Channel
|
35
|
+
def
|
45
|
-
|
36
|
+
stream_from "room_channel_#{params['room_id']}"
|
46
|
-
|
37
|
+
end
|
47
|
-
belongs_to :group
|
48
38
|
|
49
|
-
|
39
|
+
def unsubscribed
|
50
|
-
|
40
|
+
# Any cleanup needed when channel is unsubscribed
|
41
|
+
end
|
42
|
+
|
43
|
+
def speak(data)
|
44
|
+
Message.create(content: data['message'], sent_user: current_user, room: Room.find(params['room_id']))
|
45
|
+
end
|
51
46
|
end
|
47
|
+
|
52
48
|
```
|
53
49
|
```ruby
|
54
50
|
module ApplicationCable
|
@@ -59,32 +55,61 @@
|
|
59
55
|
self.current_user = find_verified_user
|
60
56
|
end
|
61
57
|
|
62
|
-
|
58
|
+
private
|
63
|
-
|
59
|
+
def find_verified_user
|
64
|
-
|
60
|
+
if verified_user = User.find_by(id: cookies.encrypted[:user_id])
|
61
|
+
verified_user
|
65
|
-
|
62
|
+
else
|
66
|
-
|
63
|
+
reject_unauthorized_connection
|
67
|
-
|
64
|
+
end
|
68
|
-
|
69
|
-
def session
|
70
|
-
@session ||= cookies.encrypted[Rails.application.config.session_options[:key]]
|
71
|
-
|
65
|
+
end
|
72
66
|
end
|
73
67
|
end
|
74
68
|
```
|
69
|
+
|
75
70
|
```ruby
|
76
71
|
class MessageBroadcastJob < ApplicationJob
|
77
72
|
queue_as :default
|
78
73
|
|
79
|
-
def perform(
|
74
|
+
def perform(message)
|
80
|
-
|
75
|
+
ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
|
81
76
|
end
|
82
77
|
|
83
78
|
private
|
79
|
+
|
84
80
|
def render_message(message)
|
85
|
-
ApplicationController.renderer.render(partial: '
|
81
|
+
ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
|
86
82
|
end
|
87
83
|
end
|
84
|
+
|
88
85
|
```
|
86
|
+
```javascript
|
87
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
88
|
+
// listed below.
|
89
|
+
//
|
90
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
91
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
92
|
+
//
|
93
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
94
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
95
|
+
//
|
96
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
97
|
+
// about supported directives.
|
98
|
+
//
|
99
|
+
//= require jquery
|
100
|
+
//= require jquery_ujs
|
101
|
+
//= require turbolinks
|
102
|
+
//= require_tree .
|
89
103
|
|
104
|
+
jQuery(document).on 'turbolinks:load', ->
|
105
|
+
messages = $('#messages')
|
106
|
+
if $('#messages').length > 0
|
107
|
+
|
108
|
+
$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
|
109
|
+
if event.keyCode is 13 # return = send
|
110
|
+
App.room.speak event.target.value
|
111
|
+
event.target.value = ''
|
112
|
+
event.preventDefault()
|
113
|
+
```
|
114
|
+
|
90
|
-
|
115
|
+
何卒よろしくお願いします。
|
1
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|