phoenix学习笔记(channel)

1. 信息流:

 2. 在endpoint.ex里要进行声明:用哪一个socket handler进行处理

socket "/socket", HelloWeb.UserSocket,
  websocket: true,
  longpoll: false

3. socket handler里(类似router):

   

channel "room:*", HelloWeb.RoomChannel

当客户端发送"room:"topic的消息时,将会由room_channel.ex处理

4. Channels: 处理来自客户端的events, join,terminate, handle_in, handle_out

  4.1 joining channels:

     

// assets/js/socket.js
...
socket.connect()

// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("room:lobby", {})
channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })

export default socket

4.2 Incoming Events

    

 def handle_in("new_msg", %{"body" => body}, socket) do
    broadcast!(socket, "new_msg", %{body: body})
    {:noreply, socket}
  end

4.3 Intercepting Outgoing Events (拦截outgoing event)

4.4 socket assigns

4.5 token authentication  

5. Topics

6. Message: 结构体,包含:

  

  • :topic - The string topic or topic:subtopic pair namespace, for example “messages”, “messages:123”
  • :event- The string event name, for example “phx_join”
  • :payload - The message payload
  • :ref - The unique string ref
  • :join_ref - The unique string ref when joining

7.PubSub:

posted @ 2019-03-20 14:51  每天都有一点进步  阅读(542)  评论(0编辑  收藏  举报