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:

转载于:https://www.cnblogs.com/shirley18/p/10563886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值