【发布时间】:2018-02-22 00:14:24
【问题描述】:
这不是我第一次在 rails 中安装 websocket,但是当我在我的 rails 控制台中输入它时,没有任何反应。那应该在 Chrome 控制台中打印“确定”吗?已连接功能有效但未收到,此代码有什么问题?我用redis替换了async,没有任何变化。
NotificationsChannel.broadcast_to(User.find_by(email: "test@mail.com"), title: "ok")
登录rails c
[ActionCable] Broadcasting to notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw: {:title=>"ok"}
=> 无
我的终端导轨 s
[ActionCable] [8857] Finished "/cable/" [WebSocket] for ::1 at 2017-09-
13 11:38:00 +0200
[ActionCable] [8857] NotificationsChannel stopped streaming from notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw
Started GET "/cable" for ::1 at 2017-09-13 11:38:00 +0200
Started GET "/cable/" [WebSocket] for ::1 at 2017-09-13 11:38:00 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 8857], ["LIMIT", 1]]
[ActionCable] [8857] Registered connection (Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw)
[ActionCable] [8857] NotificationsChannel is transmitting the subscription confirmation
[ActionCable] [8857] NotificationsChannel is streaming from notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw
还有我的webSocket安装:
channel/application_cable/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.id
end
protected
def find_verified_user
if verified_user = env['warden'].user
verified_user
else
reject_unauthorized_connection
end
end
end
end
app/channels/notifications_channel.rb
class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_for current_user
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
app/assets/javascript/channels/notifications.coffee
App.cable.subscriptions.create channel: "NotificationsChannel",
connected: ->
console.log "connected"
received: (data) ->
console.log "ok"
当我广播时,我的帧中没有任何内容。
【问题讨论】:
标签: javascript ruby-on-rails ruby coffeescript