[erlang-questions] Push a message to cowboy websockets

Barco You barcojie@REDACTED
Sat Apr 7 16:13:23 CEST 2012


Hi Magnus,

Thank you very much!

Then, if broadcasting to all other clients except self, how to do it with
gproc?

Regards,
Barco

On Sat, Apr 7, 2012 at 10:02 PM, Magnus Klaar <magnus.klaar@REDACTED>wrote:

> Hi!
>
> This is the line where the crash is occuring at:  {reply, {text,
> <<Msg>>}, Req, State, hibernate}. The last message in was: {<0.113.0>,
> {websocket_handler, "broadcast"},<<"hello server!">>} Where Msg is bound to
> the <<"hello server!">> term in the message. The <<Msg>> expression
> attempts to pack this binary as a one-byte value which will not work.
> Remove the <<>>-s around Msg and you should be good to go. I also don't see
> why you have defined the ?BC macro/constant as a string, an atom would
> suffice.
>
> TLDR; replace {reply, {text, <<Msg>>}, Req, State, hibernate}.
> with  {reply, {text, Msg}, Req, State, hibernate}.
>
> MVH Magnus
>
> On Sat, Apr 7, 2012 at 3:50 PM, Barco You <barcojie@REDACTED> wrote:
>
>> Hi AD,
>>
>> I also tried to implement a websocket broadcast as you did in this mail.
>> but I got crash in websocket_info as following. Could you please show me
>> your complete code? thanks!
>>
>> my code is:
>>
>> websocket_init(_Any, Req, []) ->
>>     gproc:reg({p, l,{?MODULE, ?BC}}),
>>     Req2 = cowboy_http_req:compact(Req),
>>     {ok, Req2, undefined, hibernate}.
>>
>> websocket_handle({text, Msg}, Req, State) ->
>>     broadcast(Msg),
>>     {reply, {text, << "You said: ", Msg/binary >>}, Req, State,
>> hibernate};
>> websocket_handle(_Any, Req, State) ->
>>     {ok, Req, State}.
>>
>> websocket_info({_Pid, {_Module, ?BC}, Msg}, Req, State) ->
>>
>>     {reply, {text, <<Msg>>}, Req, State, hibernate}.
>>
>> broadcast(Msg) ->
>>     gproc:send({p, l, {?MODULE, ?BC}}, {self(), {?MODULE, ?BC}, Msg}).
>>
>>
>>
>> ** Handler websocket_handler terminating in websocket_info/3
>>
>>    for the reason error:badarg
>> ** Message was {<0.113.0>,{websocket_handler,
>> "broadcast"},<<"hello server!">>}
>> ** Options were []
>> ** Handler state was undefined
>> ** Request was [{socket,#Port<0.940>},
>>                 {transport,cowboy_tcp_transport},
>>                 {connection,keepalive},
>>                 {pid,<0.113.0>},
>>                 {method,'GET'},
>>                 {version,{1,1}},
>>                 {peer,undefined},
>>                 {host,undefined},
>>                 {host_info,undefined},
>>                 {raw_host,<<"localhost">>},
>>                 {port,80},
>>                 {path,undefined},
>>                 {path_info,undefined},
>>                 {raw_path,<<"/websocket">>},
>>                 {qs_vals,undefined},
>>                 {raw_qs,<<>>},
>>                 {bindings,undefined},
>>                 {headers,[]},
>>                 {p_headers,[]},
>>                 {cookies,[]},
>>                 {meta,[{websocket_version,13}]},
>>                 {body_state,waiting},
>>                 {buffer,<<>>},
>>                 {resp_state,done},
>>                 {resp_headers,[]},
>>                 {resp_body,<<>>},
>>                 {urldecode,{#Fun<cowboy_http.urldecode.2>,crash}}]
>> ** Stacktrace: [{websocket_handler,websocket_info,3,
>>                     [{file,"src/websocket_handler.erl"},{line,39}]},
>>                 {cowboy_http_websocket,handler_call,7,
>>                     [{file,"src/cowboy_http_websocket.erl"},{line,389}]}]
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120407/156635fc/attachment.htm>


More information about the erlang-questions mailing list