Hi Magnus,<br><br>Thank you very much!<br><br>Then, if broadcasting to all other clients except self, how to do it with gproc?<br><br>Regards,<br>Barco<br><br><div class="gmail_quote">On Sat, Apr 7, 2012 at 10:02 PM, Magnus Klaar <span dir="ltr"><<a href="mailto:magnus.klaar@gmail.com">magnus.klaar@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi!<div><br></div><div>This is the line where the crash is occuring at: <span> {reply, {text, <<Msg>>}, Req, State, hibernate}. </span><span>The last message in was: </span>{<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.</div>

<div><div><br></div><div>TLDR; replace {reply, {text, <<Msg>>}, Req, State, hibernate}. with  {reply, {text, Msg}, Req, State, hibernate}.</div><div><br></div><div>MVH Magnus</div><div><div class="h5"><br><div class="gmail_quote">

On Sat, Apr 7, 2012 at 3:50 PM, Barco You <span dir="ltr"><<a href="mailto:barcojie@gmail.com" target="_blank">barcojie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi AD,<br><br>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!<br><br>my code is:<br><br>websocket_init(_Any, Req, []) -><br>
    gproc:reg({p, l,{?MODULE, ?BC}}),<br>    Req2 = cowboy_http_req:compact(Req),<br>    {ok, Req2, undefined, hibernate}.<br><br>websocket_handle({text, Msg}, Req, State) -><br>    broadcast(Msg),<br>    {reply, {text, << "You said: ", Msg/binary >>}, Req, State, hibernate};<br>



websocket_handle(_Any, Req, State) -><br>    {ok, Req, State}.<br><br>websocket_info({_Pid, {_Module, ?BC}, Msg}, Req, State) -><div><div><br>    {reply, {text, <<Msg>>}, Req, State, hibernate}.<br>

<br></div></div>broadcast(Msg) -><br>
    gproc:send({p, l, {?MODULE, ?BC}}, {self(), {?MODULE, ?BC}, Msg}).<br><br><br><br>** Handler websocket_handler terminating in websocket_info/3<div><div><br>   for the reason error:badarg<br></div></div>** Message was {<0.113.0>,{websocket_handler,<div>


"broadcast"},<<"hello server!">>}<br>
** Options were []<br>** Handler state was undefined<br>** Request was [{socket,#Port<0.940>},<br>                {transport,cowboy_tcp_transport},<br>                {connection,keepalive},<br>                {pid,<0.113.0>},<br>



                {method,'GET'},<br>                {version,{1,1}},<br>                {peer,undefined},<br>                {host,undefined},<br>                {host_info,undefined},<br>                {raw_host,<<"localhost">>},<br>



                {port,80},<br>                {path,undefined},<br>                {path_info,undefined},<br>                {raw_path,<<"/websocket">>},<br>                {qs_vals,undefined},<br>                {raw_qs,<<>>},<br>



                {bindings,undefined},<br>                {headers,[]},<br>                {p_headers,[]},<br>                {cookies,[]},<br>                {meta,[{websocket_version,13}]},<br>                {body_state,waiting},<br>



                {buffer,<<>>},<br>                {resp_state,done},<br>                {resp_headers,[]},<br>                {resp_body,<<>>},<br>                {urldecode,{#Fun<cowboy_http.urldecode.2>,crash}}]<br>



** Stacktrace: [{websocket_handler,websocket_info,3,<br>                    [{file,"src/websocket_handler.erl"},{line,39}]},<br>                {cowboy_http_websocket,handler_call,7,<br>                    [{file,"src/cowboy_http_websocket.erl"},{line,389}]}]</div>


<br></blockquote></div><br></div></div></div>
</blockquote></div><br>