【发布时间】:2020-09-04 01:13:07
【问题描述】:
我正在努力将 Action Cable 切换到 AnyCable。
我不使用 cookie 来识别用户,我使用 JWT 因为我的应用只提供 API。
所以在聊天的情况下,我们需要检索发送消息的用户。
在日志中,我看到这条消息1:
RPC Command: <AnyCable::CommandMessage: command: "subscribe", identifier: "{\"channel\":\"RoomChannel\",\"room_id\":\"566\"}", connection_identifiers: "{\"current_user\":{\"id\":\"XXXXXXXX\",\"login_time\":1589745276,\"okta_id\":\"xxxxx@gmail.com\"
如何检索对象“connection_identifiers”的值?
在当前连接类下面: `类:模块ApplicationCable 类连接
identified_by :current_user
def connect
self.current_user = create_user_from_tokens
reject_unauthorized_connection unless current_user.valid
end
结束 结束`
create_user_from_tokens > 从 JWT 令牌创建用户对象
以及当前接收新消息的方法: `def接收(内容) 除非接收参数返回 false 除非对话,否则返回 false
message = content['content']
message_params = {conversation_id: @conversation.id,
conversation: @conversation,
sender_id: @connection.current_user.okta_id,
sender_name: @connection.current_user.name,
content: message}
ConversationMessageService.post message_params
救援标准错误 Rails.logger.error I18n.t('log.api.websocket.error_receive') 渲染 json: {错误: :bad_request, error_description: I18n.t('log.api.websocket.error_receive'), error_uri: ''}, 状态: :bad_request 结束`
据我所知,无法检索@connection。
【问题讨论】:
-
能否提供您的连接类代码?另外,您能否提供一个您想要检索用户的示例频道代码?
-
@palkan,我已经用代码示例编辑了帖子。