2012-03-26 56 views
0

我正在嘗試使用zlib來壓縮發送到客戶端的數據。我通常只使用send()向客戶端發送JSON斑點。下面是處理工作中的作用:Erlang:爲什麼zlib在這裏返回一個空體?

handle_tcp({data, RawData}, #s1{socket=Socket}=State) -> 
    case decode_and_dispatch(RawData, State#s1.state, Socket) of 
    {ok, NewState, Body} -> 
     lager:debug("Sending to client decompressed ~p",[Body]), 
     send(Socket, Body), 
     {noreply, State#s1{state=NewState}}; 
    {error, _NewState, Msg} -> 
     lager:info("calling client_err_msg with ~p~n",[Msg]), 
     send(Socket, client_err_msg(Msg)), 
     {noreply, State}; 
    Else -> 
     lager:error("Unexpected error: ~p", [Else]), 
     send(Socket, client_err_msg(<<"server rejected the message">>)), 
     {noreply, State} 
    end; 

我的變化是微妙的,並符合本條款的第一部分,以便將其發送回客戶機之前壓縮身體,不幸的是,這是返回一個空列表。任何人都可以捕捉到我在這裏做錯了嗎?我沒有看到任何進程崩潰或任何錯誤日誌。只是一個空的列表被傳遞給客戶端。

handle_tcp({data, RawData}, #s1{socket=Socket}=State) -> 
    case decode_and_dispatch(RawData, State#s1.state, Socket) of 
    {ok, NewState, Body} -> 
     lager:debug("Sending to client decompressed ~p",[Body]), 
     %% compress the payload 
     Z = zlib:open(), 
     zlib:deflateInit(Z), 
     CompressedBody = zlib:deflate(Z,Body), 
     lager:debug("Sending to client compressed ~p",[CompressedBody]), 
     %%zlib:deflateEnd(Z), 
     send(Socket, CompressedBody), 
     %%send(Socket, Body), 
     {noreply, State#s1{state=NewState}}; 
    {error, _NewState, Msg} -> 
     lager:info("calling client_err_msg with ~p~n",[Msg]), 
     send(Socket, client_err_msg(Msg)), 
     {noreply, State}; 
    Else -> 
     lager:error("Unexpected error: ~p", [Else]), 
     send(Socket, client_err_msg(<<"server rejected the message">>)), 
     {noreply, State} 
    end; 

回答