如何在仅api的应用程序Rails中使用Action Cable



我是rails的新用户,我被要求使用rails实现聊天API应用程序,我已经看到ActionCable,但已经看到我们必须实现一些前端来连接WebSocket,是否有其他方法可以使用来自Postman的请求来制作实时应用程序,我不完全知道如何做到这一点,谢谢。

我也在试图找出一个解决方案。然而,这是我到目前为止所发现的。

  1. 我使用Firecamp https://firecamp.io/,它有一个WebSocket客户端连接到我的动作电缆服务器,它有一个端点在ws://localhost:4000/cable>
  2. 我使用设计和设计-jwt进行用户身份验证。请参阅下面的
  3. 操作电缆连接类
module ApplicationCable
class Connection < ActionCable::Connection::Base
include Warden
identified_by :current_user
def connect
self.current_user = find_verified_user!
end
def disconnect
Rails.logger.info("Disconnected: #{self}")
end
protected

def find_verified_user!
token = request.headers["Authorization"].split(" ").second
decoder = JWTAuth::UserDecoder.new
decoder.call(token, :user, nil)
rescue StandardError => e
logger.debug(e)
reject_unauthorized_connection
end
end
end

到目前为止,这是我收集到的。我可以从一个经过身份验证的用户创建一个连接。

正在为已验证的用户订阅通道。当我弄清楚的时候我会更新这个

最新更新