我正在使用rails开发一个经过验证的服务器验证的蜂巢板游戏,并且在使用具有异步集的动作电缆进行开发时遇到问题。我在终端看到服务器广播一次并发送相同的消息大约30次。这是正常的行为吗?我应该使用异步来完成这类任务吗?此外,是否可以强制导轨仅传输一次?我已经尝试实现一个简单的客户端状态系统,因此第一次传输会进行更改,以下传输将看到并且什么都不做,但看起来好像有时在状态生效之前处理了另一个传输。
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.coffee
//= require cable
//= require_self
//= require_tree .
this.App = {}
App.cable = ActionCable.createConsumer();
App.game = App.cable.subscriptions.create "GameChannel",
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) ->
switch data.action
when "game_start"
start_game(data.msg)
when "take_turn"
if game.turn == data.color
game.flop_turn()
if data.move_type == "move"
game.move_piece(data.q, data.r, data.to_q, data.to_r)
draw_all_ctx();
else
game.place_piece(data.q, data.r, data.code, data.color);
draw_all_ctx();
send_turn: (move_type, code, q, r, to_q, to_r) ->
data = {};
data.color = game.color;
data.move_type = move_type;
data.code = code;
data.q = q;
data.r = r;
data.to_r = to_r
data.to_q = to_q
@perform 'take_turn', data
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]播放给player_206:{&#34; color&#34; =&gt;&#34; black&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34;动作&#34; =&gt;&#34; take_turn&#34;} [ActionCable]广播到player_101: {&#34; color&#34; =&gt;&#34; black&#34;,&#34; move_type&#34; =&gt;&#34; move&#34;,&#34; code&#34; = &gt; -1,&#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3, &#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;} GameChannel传输 {&#34; color&#34; =&gt;&#34; black&#34;,&#34; move_type&#34; =&gt;&#34; move&#34;,&#34; code&#34; = &gt; -1,&#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3, &#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;操作&#34; =&gt;&#34; take_turn&#34;}(来自 player_206)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_206)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34; action&#34; =&gt;&#34; take_turn&#34;}(通过播放来自player_101)GameChannel 传输{&#34;颜色&#34; =&gt;&#34;黑&#34;,&#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; = -1个, &#34; q&#34; =&gt; 5,&#34; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6,&#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过 从player_101流式传输)GameChannel传输{&#34;颜色&#34; =&gt;&#34;黑色&#34;, &#34; move_type&#34; =&gt;&#34;移动&#34;,&#34;代码&#34; =&gt; -1,&#34; q&#34; =&gt; 5,&#34 ; r&#34; =&gt; 3,&#34; to_r&#34; =&gt; 3,&#34; to_q&#34; =&gt; 6, &#34;动作&#34; =&gt;&#34; take_turn&#34;}(通过播放器播放来自player_101)