2017-08-01 63 views
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 

回答

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