Ruby on rails Rails action电缆使用异步进行一次广播和多次传输

Ruby on rails Rails action电缆使用异步进行一次广播和多次传输,ruby-on-rails,actioncable,Ruby On Rails,Actioncable,我正在用rails开发一个经过服务器验证的hive棋盘游戏,在使用带有异步设置的动作电缆进行开发时遇到了问题。我在终端中看到服务器广播一次,并传输相同的消息大约30次。这是正常的行为吗?对于这类任务,我应该使用async吗?另外,是否可以强制rails只传输一次?我已经尝试实现了一个简单的客户端状态系统,因此第一次传输将进行更改,而后续传输将不会看到并执行任何操作,但有时似乎在状态生效之前处理了另一次传输 game_channel.rb class GameChannel < Applic

我正在用rails开发一个经过服务器验证的hive棋盘游戏,在使用带有异步设置的动作电缆进行开发时遇到了问题。我在终端中看到服务器广播一次,并传输相同的消息大约30次。这是正常的行为吗?对于这类任务,我应该使用async吗?另外,是否可以强制rails只传输一次?我已经尝试实现了一个简单的客户端状态系统,因此第一次传输将进行更改,而后续传输将不会看到并执行任何操作,但有时似乎在状态生效之前处理了另一次传输

game_channel.rb

class GameChannel < ApplicationCable::Channel
  def subscribed
    stream_from "player_#{current_user.id}"
  end

  def unsubscribed
    Game.forfeit(current_user.id)
  end

  def take_turn(data)
    data["action"] = "take_turn"
    GameValidator.make_move(current_user.id, data)
  end
end
game_validator.rb

class GameValidator

  def self.set_opponents(uid1, uid2)
    user1 = User.find(uid1)
    user2 = User.find(uid2)
    user1.update_attribute(:opponent_id, uid2)
    user2.update_attribute(:opponent_id, uid1)
  end

  def self.get_opponent(uid)
    user = User.find(uid)
    user.opponent_id
  end

  def self.start(uuid1, uuid2)
    white, black = [uuid1, uuid2].shuffle
    user = User.find(uuid1)
    game = Game.find(user.game_id)
    game.update_attribute(:white_id, white)
    ActionCable.server.broadcast "player_#{white}", {action:     "game_start", msg: "white"}
    ActionCable.server.broadcast "player_#{black}", {action:     "game_start", msg: "black"}

    set_opponents(uuid1, uuid2)
  end

  def self.forfeit(uuid)
    if winner = get_opponent(uid)
      ActionCable.server.broadcast "player_#{winner}", {action:     "opponent_forfeits"}
    end
  end


  def self.make_move(uuid, data)
    opponent = get_opponent(uuid)
    game = Game.find(User.find(uuid).game_id)
    #if valid move
    ActionCable.server.broadcast "player_#{uuid}", data
    ActionCable.server.broadcast "player_#{opponent}", data
  end     
end
这是航站楼的样子

[ActionCable]向播放器206广播:{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}[ActionCable]向播放器_101广播: {“颜色”=>“黑色”,“移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3, “转”=>3,“转”=>6,“转”=>“转”} {“颜色”=>“黑色”,“移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3, “到”=>3,“到”=>6,“操作”=>“转”}(通过从 玩家_206)游戏频道传送{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_206的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过玩家_101的流媒体)游戏频道 发送{“颜色”=>“黑色”、“移动类型”=>“移动”、“代码”=>-1, “q”=>5,“r”=>3,“to_r”=>3,“to_q”=>6,“action”=>“take_turn”}(通过 从玩家_101)游戏频道传输{“颜色”=>“黑色”, “移动类型”=>“移动”,“代码”=>-1,“q”=>5,“r”=>3,“到r”=>3,“到q”=>6, “action”=>“take_turn”}(通过播放器_101的流媒体)


请多花点时间让你的答案更容易阅读,并添加一些代码示例。这将帮助您进行更多更改以获得答案。请花更多时间使您的答案更易于阅读,并添加一些代码示例。这将帮助您进行更多的更改以获得答案。
class GameValidator

  def self.set_opponents(uid1, uid2)
    user1 = User.find(uid1)
    user2 = User.find(uid2)
    user1.update_attribute(:opponent_id, uid2)
    user2.update_attribute(:opponent_id, uid1)
  end

  def self.get_opponent(uid)
    user = User.find(uid)
    user.opponent_id
  end

  def self.start(uuid1, uuid2)
    white, black = [uuid1, uuid2].shuffle
    user = User.find(uuid1)
    game = Game.find(user.game_id)
    game.update_attribute(:white_id, white)
    ActionCable.server.broadcast "player_#{white}", {action:     "game_start", msg: "white"}
    ActionCable.server.broadcast "player_#{black}", {action:     "game_start", msg: "black"}

    set_opponents(uuid1, uuid2)
  end

  def self.forfeit(uuid)
    if winner = get_opponent(uid)
      ActionCable.server.broadcast "player_#{winner}", {action:     "opponent_forfeits"}
    end
  end


  def self.make_move(uuid, data)
    opponent = get_opponent(uuid)
    game = Game.find(User.find(uuid).game_id)
    #if valid move
    ActionCable.server.broadcast "player_#{uuid}", data
    ActionCable.server.broadcast "player_#{opponent}", data
  end     
end