2014-09-24 30 views
2

我正在開發帶有Rails 4和websocket-rails gem的websocket服務器。如何從websocket-rails中的ActionController發送消息

我想發送一條消息作爲HTTP POST消息(因爲它很方便自定義標題或等)。

我想從我的控制器發送websocket消息,但我可能不會使用WebsocketRails::BaseController.send_message。我該怎麼做,或者有什麼不對?

這裏的代碼:

# message_controller.rb 

# This controller doesn't inherit from WebsocketRails::BaseController 
class MessageController < ActionController::Base 
    def create 
    # do something with params and models 
    Message.create params 

    # then, I want to dispatch message here! 
    end 
end 

回答

4

我找到了答案由我自己,只是在自述。

https://github.com/websocket-rails/websocket-rails

廣播從您的Rails應用程序內的任意位置的通道。 WebsocketRails控制器的現有控制器,模型,後臺作業或新的 。

latest_post = Post.latest 
WebsocketRails[:posts].trigger 'new', latest_post