檢視原始碼 Phoenix.LiveView.Socket (Phoenix LiveView v0.20.17)

適用於 Phoenix 端點的 LiveView socket。

這通常會直接安裝在你的端點。

socket "/live", Phoenix.LiveView.Socket,
  websocket: [connect_info: [session: @session_options]]

若要從你的 MyAppWeb.UserSocket 模組,在一般 Phoenix 管道與 LiveView 處理程序間共享底層的傳輸連線,請 使用 Phoenix.LiveView.Socket

接著,宣告你的 channel 定義,還有處理你的管道特定需求的選用 connect/3id/1 回呼,然後將你自己的 socket 安裝在你的端點。

socket "/live", MyAppWeb.UserSocket,
  websocket: [connect_info: [session: @session_options]]

如果你需要在執行時間設定階段選項,你可以使用 MFA 元組。它指定的功能必須傳回一個關鍵字清單。

socket "/live", MyAppWeb.UserSocket,
  websocket: [connect_info: [session: {__MODULE__, :runtime_opts, []}]]

# ...

def runtime_opts() do
  Keyword.put(@session_options, :domain, host())
end

摘要

型態

一個 LiveView 的資料,儲存在 socket 中。

assigns 不在 socket 中時傳回的結構。

t()

型態

@type assigns() :: map() | assigns_not_in_socket()

一個 LiveView 的資料,儲存在 socket 中。

@opaque assigns_not_in_socket()

assigns 不在 socket 中時傳回的結構。

@type fingerprints() :: {nil, map()} | {binary(), map()}
@type t() :: %Phoenix.LiveView.Socket{
  assigns: assigns(),
  endpoint: module(),
  fingerprints: fingerprints(),
  host_uri: URI.t() | :not_mounted_at_router,
  id: binary(),
  parent_pid: nil | pid(),
  private: map(),
  redirected: nil | tuple(),
  root_pid: pid(),
  router: module(),
  transport_pid: pid() | nil,
  view: module()
}