2020年3月9日 星期一

惡補文章 websocket 跟TCP跟HTTP跟ActionCable

WebSocket及HTTP出現的歷史原因
https://www.zhihu.com/question/20215561
“websocket出现是因为浏览器不给开门”,“不是WebSocket基于HTTP,相反,可以看成可以看成可以看成HTTP基于WebSocket”。

  1. Rails 聊一聊 ActionCable 背后的技术



1. 客户端发起http请求,附加头信息为:“Upgrade Websocket”
客戶端那邊請求,假如你伺服器可以接受Websocket的話,那我們就升級使用Websocket吧。

ActionCable Rails

Action Cable uses the Rack socket hijacking API to take over control of connections from the application server. Action Cable then manages connections internally, in a multithreaded manner, layering as many channels as you care to define over that socket connection.

For every instance of your application that spins up, an instance of Action Cable is created, using Rack to open and maintain a persistent connection, and using a channel mounted on a sub-URI of your main application to stream from certain areas of your application and broadcast to other areas.

Action Cable offers server-side code to broadcast certain content (think new messages or notifications) over the channel, to a subscriber. The subscriber is instantiated on the client-side with a handy JavaScript function that uses jQuery to append new content to the DOM.

Lastly, Action Cable uses Redis as a data store for transient data, syncing content across instances of your application.

Action Cable使用Rack套接字劫持API來接管來自應用程序服務器的連接控制。然後,Action Cable以多線程的方式在內部管理連接,可以根據需要在該套接字連接上定義盡可能多的通道。

對於您啟動的每個應用程序實例,都會創建一個“動作電纜”實例,使用Rack打開並維護持久連接,並使用安裝在主應用程序子URI上的通道從應用程序的某些區域進行流傳輸並廣播到其他區域。

Action Cable提供服務器端代碼,以通過通道向訂戶廣播某些內容(例如新消息或通知)。用方便的JavaScript函數在客戶端實例化訂閱服務器,該函數使用jQuery將新內容附加到DOM。

最後,Action Cable將Redis用作臨時數據的數據存儲,從而在應用程序實例之間同步內容。

沒有留言:

張貼留言

cloud9上面開發rails

 使用IAM身分登入,以免有資安問題。(用admin帳號登入,創建iam使用者,然後在上面的連結登入) 選擇 ubuntu18.04 減少查詢成本。 已經是超級使用者了,sudo 什麼都不需要輸入密碼了。 $ ruby -v  檢查 ruby使用的版本>>2.6.3,...