【发布时间】:2013-11-12 00:49:19
【问题描述】:
为了描述我的问题,我附上了简单的 Cramp http://cramp.in/ 类。 我添加了一些修改,但它主要像https://github.com/lifo/cramp-pub-sub-chat-demo/blob/master/app/actions/chat_action.rb
class ChatAction < Cramp::Websocket
use_fiber_pool
on_start :create_redis
on_finish :handle_leave, :destroy_redis
on_data :received_data
def create_redis
@redis = EM::Hiredis.connect('redis://127.0.0.1:6379/0')
end
def destroy_redis
@redis.pubsub.close_connection
@redis.close_connection
end
def received_data(data)
msg = parse_json(data)
case msg[:action]
when 'join'
handle_join(msg)
when 'message'
handle_message(msg)
else
# skip
end
end
def handle_join(msg)
@user = msg[:user]
subscribe
publish(:action => 'control', :user => @user, :message => 'joined the chat room')
end
def handle_leave
publish :action => 'control', :user => @user, :message => 'left the chat room'
end
def handle_message(msg)
publish(msg.merge(:user => @user))
# added only for inline sync tests
render_json(:action => 'message', :user => @user, :message => "this info should appear after published message")
end
private
def subscribe
@redis.pubsub.subscribe('chat') do |message|
render(message)
end
end
def publish(message)
@redis.publish('chat', encode_json(message))
end
def encode_json(obj)
Yajl::Encoder.encode(obj)
end
def parse_json(str)
Yajl::Parser.parse(str, :symbolize_keys => true)
end
def render_json(hash)
render encode_json(hash)
end
end
更多关于我尝试做的是handle_message方法。
我尝试以正确的顺序向客户端发送消息。首先向所有订阅者发布消息,然后只为当前连接的客户端呈现一些内部信息。
对于以上代码客户端接收:
{"action":"message","user":"user1","message":"this info should appear after published message"}
{"action":"message","message":"simple message","user":"user1"}
它不同步,可能是因为 em-hiredis 可延迟响应。 所以我尝试以这种方式同步它:
def handle_message(msg)
EM::Synchrony.sync publish(msg.merge(:user => @user))
EM::Synchrony.next_tick do # if I comment this block messages order is still incorrect
render_json(:action => 'message', :user => @user, :message => "this info should appear after published message")
end
end
现在,客户端以正确的顺序处理消息。
{"action":"message","message":"simple message","user":"user1"}
{"action":"message","user":"user1","message":"this info should appear after published message"}
我的问题是:
- 当我评论 EM::Synchrony.next_tick 块时,消息顺序仍然不正确。这个例子中的 EM::Synchrony.next_tick 块有什么意义?
- 这是使用 Cramp 或 EventMachine 处理内联同步的好方法吗?
- 有没有更好、更清晰的处理方式?
谢谢!
【问题讨论】:
标签: ruby eventmachine em-synchrony cramp