简体   繁体   English

ActionCable多次注册用户

[英]ActionCable registers user multiple times

I'm using ActionCable in my RoR application designed to function as a chatroom for roleplays, according to Sitepoint's tutorial . 根据Sitepoint的教程 ,我在我的RoR应用程序中使用ActionCable,该应用程序旨在充当角色扮演的聊天室。 I have set it up, and messages are, as expected, broadcasted whenever one is created. 我已经设置好了,正如预期的那样,无论何时创建消息都会广播消息。 However, each time the chat room page is loaded, new subscriptions are made for the same user, making messages appear multiple times. 但是,每次加载聊天室页面时,都会为同一个用户进行新的订阅,从而使消息多次出现。

app/assets/javascripts/channels/roleplays.coffee: 应用程序/资产/ Java脚本/渠道/ roleplays.coffee:

jQuery(document).on 'turbolinks:load', ->
  messages = $('#messages')
  if $('#messages').length > 0


    App.global_chat = App.cable.subscriptions.create {
      channel: "RoleplaysChannel"
      roleplay_id: messages.data('roleplay-id')
    },
      connected: ->
# Called when the subscription is ready for use on the server

      disconnected: ->
# Called when the subscription has been terminated by the server

      received: (data) ->
        messages.append(data)
        console.log(data)


      send_message: (message, roleplay_id) ->
        @perform 'send_message', message: message, roleplay_id: roleplay_id


    $('#new_message').submit (e) ->
      $this = $(this)
      textarea = $this.find('#message_body')
      e.preventDefault()

      if $.trim(textarea.val()).length > 1
        App.global_chat.send_message textarea.val(), messages.data('roleplay-id')
        textarea.val('')

      return false

To avoid duplicate subscriptions, you will need some way of determining whether the user is already connected. 为避免重复订阅,您需要某种方法来确定用户是否已连接。

A simple approach might be to do something like the following: 一个简单的方法可能是执行以下操作:

(->
  connected = false

  jQuery(document).on 'turbolinks:load', ->
    messages = $('#messages')

    if $('#messages').length > 0 && !connected
      App.global_chat = App.cable.subscriptions.create {
        channel: "RoleplaysChannel"
        roleplay_id: messages.data('roleplay-id')
      },
        connected: ->
          connected = true

        disconnected: ->
          connected = false

        # …
)()

This will only work for a single roleplay (or chatroom). 这仅适用于单个角色扮演(或聊天室)。 If the user tries a different roleplay to one they've visited previously, they won't be subscribed to the new one. 如果用户尝试与之前访问过的角色扮演不同的角色扮演,则他们将不会订阅新角色扮演。 To fix this, you'll need a way of managing subscriptions. 要解决此问题,您需要一种管理订阅的方法。

Something like the following might work: 像下面这样的东西可能会起作用:

(->
  connections = []

  addConnection = (id) ->
    connections.push(id)

  removeConnection = (id) ->
    index = connections.indexOf(id)
    connections.splice(index, 1) if index > -1

  connectedTo = (id) ->
    connections.indexOf(id) > -1

  jQuery(document).on 'turbolinks:load', ->
    messages = $('#messages')
    roleplayID = messages.data('roleplay-id')

    if $('#messages').length > 0 && !connectedTo(roleplayID)
      App.global_chat = App.cable.subscriptions.create {
        channel: "RoleplaysChannel"
        roleplay_id: roleplayID
      },
        connected: ->
          addConnection(roleplayID)

        disconnected: ->
          removeConnection(roleplayID)

        # …
)()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM