<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 6. message\_bus #### 1. 介紹 本來是要先寫`ActionCable`的,不過在寫`ActionCable`之前,先來寫寫[message\_bus](https://github.com/SamSaffron/message_bus)。它不是websocket的內容,但它在ActionCable出現之前用得蠻多,以后可能會少用了,但它還是一個優秀的gem。 `message_bus`的機制是輪循,長循環,或者EventSource相關的,它在前后端都進行了整合,很方便使用。如果在以前要自己手動使用輪循機制,可能得手動寫一些js代碼,然而`message_bus`不僅在js端進行了封裝,在后端也提供了一種叫pub/sub的機制,理解了`message_bus`,也能更好的理解`ActionCable`,因為很概念是互通的。 #### 2. 使用 我們還是來實現之前的聊天室來例子,來玩一下message\_bus。 安裝。 ``` gem 'message_bus' gem 'puma' ``` 添加app/controllers/chat\_controller.rb文件,內容如下: ``` class ChatController < ApplicationController def chat MessageBus.publish "/channel", params[:text] head 200, content_type: "text/html" end end ``` 主要是`MessageBus.publish "/channel", params[:text]`這一行,表示向`/channel`這個通道推送`params[:text]`參數中的數據。 在config/routes.rb中添加路由。 ``` Rails.application.routes.draw do post "/chat", to: "chat#chat" end ``` 分別添加view和js。 ``` h1 Tubesock Chat pre id="output" = form_tag "/chat", class: "chat", remote: true input placeholder="hello world" autofocus=true name="text" ``` ``` MessageBus.start() MessageBus.callbackInterval = 500 MessageBus.subscribe "/channel", (data) -> $("#output").append "#{data}<br>" ``` `MessageBus.subscribe "/channel"`表示訂閱服務器端推送過來的通道的數據,還有,`callbackInterval`表示的是輪循的時間。 最后的效果如下: ![](https://box.kancloud.cn/c3d97d10f1d46343ffbd327aa624fd6c_898x387.png) 本篇完結。 下一篇:[websocket之actioncable入門(七)](http://www.rails365.net/articles/websocket-zhi-actioncable-ru-men-qi)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看