0
在發送消息給聊天室得到錯誤而解析聊天按摩到另一個聊天室
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