teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

画像をテキストに変えました

2021/02/07 15:17

投稿

tatsuhiros
tatsuhiros

スコア0

title CHANGED
File without changes
body CHANGED
@@ -2,16 +2,157 @@
2
2
  リアルタイムチャットでメッセージを受信したユーザに通知を送る機能を実装したいと考えているのですが、方法が思いつきません。
3
3
 
4
4
  もし分かる方がいれば、お力をお借りしたいです。
5
- 以下のようなコードとなっています。
6
5
 
7
- ![room_channel.js](ba6531fe1fcae179da337bafb6ef2838.png)
6
+ ずっと困っているので、助けていただければ本当に助かります!
8
- ![room_channel.js](9f634b72d4e9c7a567b3463bbda1771a.png)](597fb33f553351c522ac34090a1f0398.png)
7
+ どうか宜しくお願いいたします。
9
8
 
10
- ![messages_controller.rb](19549ca88412a213279d2399ccc05a71.png)
11
- ![_message.html.erb](fac8b8923abea70465b66add376e91ae.png)
12
- ![message.rb](d9610b54cb73ed678ccbb64f5800095b.png)
9
+ 以下のようなコードとなっております。
13
- ![message_broadcast_job.rb](26e45fedf10c44c9d6d3bc70cbe6dcc1.png)
14
- ![rooms/show.html.erb](fc3051372492ba6045bbed15adfa3f1c.png)
15
10
 
11
+ ```ruby
12
+ class RoomChannel < ApplicationCable::Channel
13
+ def subscribed
14
+ stream_from "room_channel_#{params['room']}"
15
+ end
16
+
17
+ def unsubscribed
18
+ # Any cleanup needed when channel is unsubscribed
19
+ end
20
+
21
+ def speak(data)
22
+ # jsで実行されたspeakのmessageを受け取り、room_channelのreceivedにブロードキャストする
23
+ Message.create! content: data['message'], user_id: current_user.id, room_id: params['room']
24
+ end
25
+ end
26
+
27
+ ```
28
+
29
+
30
+
31
+ ```ruby
32
+ class MessageBroadcastJob < ApplicationJob
33
+ queue_as :default
34
+
35
+ def perform(message)
36
+ ActionCable.server.broadcast "room_channel_#{message.room_id}", message: render_message(message)
37
+ end
38
+
39
+ private
40
+
41
+ def render_message(message)
42
+ ApplicationController.render_with_signed_in_user(message.user, partial: 'messages/message', locals: { message: message })
43
+ end
44
+ end
45
+ ```
46
+ ```Ruby
47
+ module ApplicationCable
48
+ class Connection < ActionCable::Connection::Base
49
+ identified_by :current_user
50
+
51
+ def connect
52
+ reject_unauthorized_connection unless find_verified_user
53
+ end
54
+
55
+ private
56
+
57
+ def find_verified_user
58
+ self.current_user = env['warden'].user
59
+ end
60
+ end
61
+ end
62
+ ```
63
+
64
+ ```Ruby
65
+ class Message < ApplicationRecord
66
+ validates :content, presence: true
67
+ after_create_commit { MessageBroadcastJob.perform_later self }
68
+ belongs_to :user
69
+ belongs_to :room
70
+ has_many :active_notifications, class_name: 'Notification', foreign_key: 'visitor_id', dependent: :destroy
71
+ has_many :passive_notifications, class_name: 'Notification', foreign_key: 'visited_id', dependent: :destroy
72
+ end
73
+
74
+ ```
75
+
76
+ ```Ruby
77
+ class MessagesController < ApplicationController
78
+ end
79
+
80
+ ```
81
+
82
+ ```JavaScript
83
+ import consumer from './consumer'
84
+
85
+ // $(function() { ... }); で囲むことでレンダリング後に実行される
86
+ // レンダリング前に実行されると $('#messages').data('room_id') が取得できない
87
+ // turbolinks を使っている場合は $(document).on('turbolinks:load', function() { ... }); で囲う
88
+ $(function() {
89
+ const chatChannel = consumer.subscriptions.create({ channel: 'RoomChannel', room: $('#messages').data('room_id') }, {
90
+ connected() {
91
+ // Called when the subscription is ready for use on the server
92
+ },
93
+
94
+ disconnected() {
95
+ // Called when the subscription has been terminated by the server
96
+ },
97
+
98
+ received: function(data) {
99
+ return $('#messages').append(data['message']);
100
+ },
101
+
102
+ speak: function(message) {
103
+ return this.perform('speak', {
104
+ message: message
105
+ });
106
+ }
107
+ });
108
+
109
+ $(document).on('keypress', '[data-behavior~=room_speaker]', function(event) {
110
+ if (event.keyCode === 13) {
111
+ chatChannel.speak(event.target.value);
112
+ event.target.value = '';
113
+ return event.preventDefault();
114
+ }
115
+ });
116
+
117
+ $(".button").on("click",function(){
118
+ var message = $(".speaker_txt").val();
119
+ chatChannel.speak(message);
120
+ $(".speaker_txt").val("")
121
+ });
122
+ });
123
+
124
+ ```
125
+
126
+
127
+ ```HTML
128
+ <div class="chat-room-container">
129
+ <%= link_to rooms_path do%>
130
+ <i class="fas fa-arrow-left fa-2x" aria-hidden="true"></i>
131
+ <% end %>
132
+ <% if @user.image? %>
133
+ <%= link_to user_path(@user) do %>
134
+ <%= image_tag(@user.image.to_s, :class => "top-image")%>
135
+ <% end %>
136
+ <span class="name"><%= @user.name %></span>
137
+ <% else %>
138
+ <%= link_to user_path(@user) do %>
139
+ <%= image_tag("default.png", :class => "top-image")%>
140
+ <% end %>
141
+ <span class="name"><%= @user.name %></span>
142
+ <% end %>
143
+ <hr>
144
+ <div class= "messages" id ='messages' data-room_id="<%= @room.id %>">
16
- ずっと困っているので、助けていただければ本当に助かります!
145
+ <%= render @messages %>
17
- どうか宜しくお願いいたします。
146
+ </div>
147
+ </div>
148
+ <div class= "message-form row">
149
+ <div class = "send-form offset-1 col-9">
150
+ <form>
151
+ <input class="speaker_txt col-12" name="content" type="text" data-behavior="room_speaker", autofocus>
152
+ </form>
153
+ </div>
154
+ <div class= "send-button col-1">
155
+ <button class="button btn btn-primary"><i class="fas fa-paper-plane"></i></button>
156
+ </div>
157
+ </div>
158
+ ```