Elixir 将聊天信息解析到其他聊天室时出错
我在向聊天室发送消息时收到错误消息 房间_channel.exElixir 将聊天信息解析到其他聊天室时出错,elixir,chat,phoenix-framework,Elixir,Chat,Phoenix Framework,我在向聊天室发送消息时收到错误消息 房间_channel.ex def join("room:" <> _user, _, socket) do send self(), :after_join {:ok, socket} end def handle_info(:after_join, socket) do {_, value} = Redix.command(:redix, ["GET", socket.assigns.user])
def join("room:" <> _user, _, socket) do
send self(), :after_join
{:ok, socket}
end
def handle_info(:after_join, socket) do
{_, value} = Redix.command(:redix, ["GET", socket.assigns.user])
Presence.track(socket, socket.assigns.user, %{
online_at: :os.system_time(:millisecond),
status: value
})
push socket, "presence_state", Presence.list(socket)
{:noreply, socket}
end
def handle_in("message:new", message, socket) do
broadcast! socket, "message:new", %{
user: socket.assigns.user,
body: message,
timestamp: :os.system_time(:millisecond)
}
PhoenixChat.Endpoint.subscribe("room:" <> message["to"])
PhoenixChat.Endpoint.broadcast!("room:" <> message["to"], "message:new",%{
user: socket.assigns.user,
body: message,
timestamp: :os.system_time(:millisecond)
})
milisecondstime = :os.system_time(:millisecond)
[room, user] = String.split(Map.get(socket, :topic), ":")
data = Poison.encode!(%{"created_at" => :os.system_time(:millisecond), "updated_at" => :os.system_time(:millisecond), "uuid" => UUID.uuid1(), "date" => :os.system_time(:millisecond), "from" => user, "to" => message["to"], "message" => message["value"]})
Redix.command(:redix, ["SET", UUID.uuid1(), data])
{:noreply, socket}
end
从文档中:
注意:调用方必须负责防止重复订阅。从端点调用subscribe/1
后,同样的流程适用于处理频道内的常规Elixir消息。通常,您只需中继%Phoenix.Socket.Broadcast{}
事件和负载:
alias Phoenix.Socket.Broadcast
def handle_info(%Broadcast{topic: _, event: ev, payload: payload}, socket) do
push socket, ev, payload
{:noreply, socket}
end
def handle_info(:after_join, socket) do
alias Phoenix.Socket.Broadcast
def handle_info(%Broadcast{topic: _, event: ev, payload: payload}, socket) do
push socket, ev, payload
{:noreply, socket}
end