質問編集履歴
2
コード、現状の更新、修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,23 +12,63 @@
|
|
12
12
|
|
13
13
|
現状:
|
14
14
|
|
15
|
-
・console
|
15
|
+
・consoleにて、App.room.speak("内容")を実行すると、falseが帰ってきてしまう。
|
16
|
+
|
16
|
-
|
17
|
+
・https://qiita.com/kohei1228/items/7aed5aad9c63e834c0e1 を参照
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
17
|
-
|
23
|
+
以下エラー文
|
18
|
-
|
24
|
+
|
19
|
-
|
25
|
+
`An unauthorized connection attempt was rejected
|
20
|
-
|
26
|
+
|
21
|
-
|
27
|
+
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)`
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
22
|
-
|
33
|
+
以下、コード
|
23
|
-
|
24
|
-
|
34
|
+
|
35
|
+
|
36
|
+
|
25
|
-
```ruby
|
37
|
+
```ruby
|
38
|
+
|
26
|
-
|
39
|
+
App.room = App.cable.subscriptions.create {channel: "RoomChannel"},
|
40
|
+
|
41
|
+
connected: ->
|
42
|
+
|
43
|
+
# Called when the subscription is ready for use on the server
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
disconnected: ->
|
48
|
+
|
49
|
+
# Called when the subscription has been terminated by the server
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
received: (data) ->
|
54
|
+
|
55
|
+
$('#messages').append data['message']
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
speak: (message)->
|
60
|
+
|
61
|
+
@perform 'speak', message: message
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
|
27
|
-
class Cha
|
67
|
+
class RoomChannel < ApplicationCable::Channel
|
28
|
-
|
68
|
+
|
29
|
-
def
|
69
|
+
def
|
30
|
-
|
70
|
+
|
31
|
-
|
71
|
+
stream_from "room_channel_#{params['room_id']}"
|
32
72
|
|
33
73
|
end
|
34
74
|
|
@@ -42,11 +82,11 @@
|
|
42
82
|
|
43
83
|
|
44
84
|
|
45
|
-
def p
|
85
|
+
def speak(data)
|
46
|
-
|
86
|
+
|
47
|
-
Message.create
|
87
|
+
Message.create(content: data['message'], sent_user: current_user, room: Room.find(params['room_id']))
|
48
|
-
|
88
|
+
|
49
|
-
end
|
89
|
+
end
|
50
90
|
|
51
91
|
end
|
52
92
|
|
@@ -56,124 +96,134 @@
|
|
56
96
|
|
57
97
|
```ruby
|
58
98
|
|
59
|
-
App.chat = App.cable.subscriptions.create "ChatChannel",
|
60
|
-
|
61
|
-
connected: ->
|
62
|
-
|
63
|
-
# Called when the subscription is ready for use on the server
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
disconnected: ->
|
68
|
-
|
69
|
-
# Called when the subscription has been terminated by the server
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
99
|
+
module ApplicationCable
|
74
|
-
|
75
|
-
|
100
|
+
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
put_message: (message) ->
|
80
|
-
|
81
|
-
@perform 'put_message', message: message, user: @user, group: @group
|
82
|
-
|
83
|
-
```
|
84
|
-
|
85
|
-
```ruby
|
86
|
-
|
87
|
-
class
|
101
|
+
class Connection < ActionCable::Connection::Base
|
102
|
+
|
88
|
-
|
103
|
+
identified_by :current_user
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def connect
|
108
|
+
|
109
|
+
self.current_user = find_verified_user
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def find_verified_user
|
118
|
+
|
89
|
-
|
119
|
+
if verified_user = User.find_by(id: cookies.encrypted[:user_id])
|
90
|
-
|
120
|
+
|
91
|
-
|
121
|
+
verified_user
|
92
|
-
|
122
|
+
|
93
|
-
|
123
|
+
else
|
94
|
-
|
95
|
-
|
96
|
-
|
124
|
+
|
97
|
-
|
125
|
+
reject_unauthorized_connection
|
98
|
-
|
126
|
+
|
99
|
-
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
100
132
|
|
101
133
|
end
|
102
134
|
|
103
135
|
```
|
104
136
|
|
137
|
+
|
138
|
+
|
105
|
-
```ruby
|
139
|
+
```ruby
|
106
|
-
|
107
|
-
|
140
|
+
|
108
|
-
|
109
|
-
|
141
|
+
class MessageBroadcastJob < ApplicationJob
|
110
|
-
|
142
|
+
|
111
|
-
|
143
|
+
queue_as :default
|
112
|
-
|
113
|
-
|
114
|
-
|
144
|
+
|
145
|
+
|
146
|
+
|
115
|
-
|
147
|
+
def perform(message)
|
116
|
-
|
148
|
+
|
117
|
-
|
149
|
+
ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
|
118
|
-
|
150
|
+
|
119
|
-
|
151
|
+
end
|
120
|
-
|
121
|
-
|
122
|
-
|
152
|
+
|
153
|
+
|
154
|
+
|
123
|
-
|
155
|
+
private
|
124
|
-
|
156
|
+
|
157
|
+
|
158
|
+
|
125
|
-
|
159
|
+
def render_message(message)
|
126
|
-
|
127
|
-
|
160
|
+
|
128
|
-
|
129
|
-
rescue
|
130
|
-
|
131
|
-
reject_unauthorized_connection
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
def session
|
138
|
-
|
139
|
-
|
161
|
+
ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
|
140
|
-
|
141
|
-
end
|
142
162
|
|
143
163
|
end
|
144
164
|
|
145
165
|
end
|
146
166
|
|
167
|
+
|
168
|
+
|
147
|
-
```
|
169
|
+
```
|
148
|
-
|
170
|
+
|
149
|
-
```r
|
171
|
+
```javascript
|
172
|
+
|
150
|
-
|
173
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
174
|
+
|
175
|
+
// listed below.
|
176
|
+
|
177
|
+
//
|
178
|
+
|
179
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
180
|
+
|
181
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
182
|
+
|
183
|
+
//
|
184
|
+
|
185
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
186
|
+
|
187
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
188
|
+
|
189
|
+
//
|
190
|
+
|
191
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
192
|
+
|
151
|
-
|
193
|
+
// about supported directives.
|
194
|
+
|
152
|
-
|
195
|
+
//
|
196
|
+
|
197
|
+
//= require jquery
|
198
|
+
|
199
|
+
//= require jquery_ujs
|
200
|
+
|
201
|
+
//= require turbolinks
|
202
|
+
|
203
|
+
//= require_tree .
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
jQuery(document).on 'turbolinks:load', ->
|
208
|
+
|
209
|
+
messages = $('#messages')
|
210
|
+
|
211
|
+
if $('#messages').length > 0
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
|
216
|
+
|
217
|
+
if event.keyCode is 13 # return = send
|
218
|
+
|
219
|
+
App.room.speak event.target.value
|
220
|
+
|
221
|
+
event.target.value = ''
|
222
|
+
|
153
|
-
|
223
|
+
event.preventDefault()
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
224
|
+
|
158
|
-
|
159
|
-
Actioncable.server.broadcast 'room_channel', message: render_message(message)
|
160
|
-
|
161
|
-
end
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
private
|
166
|
-
|
167
|
-
def render_message(message)
|
168
|
-
|
169
|
-
ApplicationController.renderer.render(partial: 'groups/message', locals: { message: message, user: @user, group: @group })
|
170
|
-
|
171
|
-
end
|
172
|
-
|
173
|
-
end
|
174
|
-
|
175
|
-
```
|
225
|
+
```
|
176
|
-
|
177
|
-
|
178
|
-
|
226
|
+
|
227
|
+
|
228
|
+
|
179
|
-
|
229
|
+
何卒よろしくお願いします。
|
1
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|