我在向聊天室发送消息时收到错误消息
Room_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])
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
这是控制台错误消息
[info] Replied room:Pamidu :ok
[error] GenServer #PID<0.464.0> terminating
** (FunctionClauseError) no function clause matching in PhoenixChat.RoomChannel.handle_info/2
(phoenix_chat) web/channels/room_channel.ex:14: PhoenixChat.RoomChannel.handle_info(%Phoenix.Socket.Broadcast{event: "message:new", payload: %{body: %{"to" => "rusiru", "value" => "fuck you "}, timestamp: 1501499563929, user: "abc"}, topic: "room:rusiru"}, %Phoenix.Socket{assigns: %{user: "abc"}, channel: PhoenixChat.RoomChannel, channel_pid: #PID<0.464.0>, endpoint: PhoenixChat.Endpoint, handler: PhoenixChat.UserSocket, id: nil, joined: true, pubsub_server: PhoenixChat.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "room:abc", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.421.0>})
(phoenix) lib/phoenix/channel/server.ex:239: Phoenix.Channel.Server.handle_info/2
(stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
(stdlib) gen_server.erl:667: :gen_server.handle_msg/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Broadcast{event: "message:new", payload: %{body: %{"to" => "rusiru", "value" => "fuck you "}, timestamp: 1501499563929, user: "abc"}, topic: "room:rusiru"}
State: %Phoenix.Socket{assigns: %{user: "abc"}, channel: PhoenixChat.RoomChannel, channel_pid: #PID<0.464.0>, endpoint: PhoenixChat.Endpoint, handler: PhoenixChat.UserSocket, id: nil, joined: true, pubsub_server: PhoenixChat.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "room:abc", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.421.0>}
这里我正在创建用户智能聊天室,并传递消息应该传递到的消息和用户名。 消息传递没有问题,但从控制台,它显示此错误。我不知道这个并找到解决这个问题的方法。 有没有办法解决这个错误?
第14行是这个
def handle_info(:after_join, socket) do
答案 0 :(得分:1)
来自Phoenix.Channel文档:
注意:调用者必须负责防止重复订阅。从您的终端呼叫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