Mykal Marsh
Mykal Marsh

Reputation: 1

Hi, I am working on a Yaws web socket connection issue

I have stumbled upon a issue successfully enabling a web socket connection between the web browser, and the Yaws web server. Both, the Javascript code sample by the client, and Erlang code samples by the server that I want to show came from samples in a programming textbook called, "Building Web Applications with Erlang". I have a feeling that the issue is I'm running a later version of the Yaws Web server than this book, "2.0.6" to be exact; but I don't know. I want to know your thoughts. Thank you.

Javascript code sample (Client side).

   $(function ()
{
  var WebSocket = window.WebSocket || window.MozWebSocket;
  var socket = new WebSocket("ws://localhost:8080/");

  // wait for socket to open
  socket.onopen = function ()
  {


   $('input#echo').on('keypress', function (event)


                       {
                       if (event.which == 13) {
                       event.preventDefault();
                       var msg = $(this).val();

                       socket.send(JSON.stringify(
                       {
                                message:msg
                       }
                       ));
                       }
                       });

                        socket.onmessage = function(msg)
                        {
                             var message = $.parseJSON(msg.data);
                             var html    = $('div#messages').html() + message.message + "<br>\n";
                             $('div#message').html(html);
                        }
                        }

                       });
Upgrade: WebSocket

Erlang code sample (server-side)

    -module(sole_callback).

%% Export for websocket callbacks
-export([handle_message/1, say_hi/1]).


handle_message({binary, Message}) ->
      io:format("~p:~p basic echo handler got ~p~n",
             [?MODULE, ?LINE, Message]),
      {reply, {binary, <<Message/binary>>}}.

say_hi(Pid) ->
      io:format("asynchronous greetings~n", []),
      yaws_api:websocket_send(Pid, {text, <<"hi there!">>}).

Erlang code sample (Embedded mode)

<script language="Javascript" type="text/javascript" src="jquery.min.js"></script><script language="Javascript" type="text/javascript" src="record.js"></script><script language="Javascript" type="text/javascript" src="socket.js"></script>

<erl>
out(Arg) ->
{html, "<img src=images_folder/audio.png onclick=socket.onopen() width=25px height=25px>"}.
</erl>

<erl>


get_upgrade_header(#headers{other=L}) ->
lists:foldl(fun({http_header,_,KO,_,V}, undefined) ->
                    K = case is_atom(KO) of
                            true ->
                                 atom_to_list(KO);
                            false ->
                                 KO
                        end,
                    case string:to_lower(K) of
                        "upgrade" ->
                            true;
                       _ ->
                            false
                    end;
               (_, ACC) ->
                    ACC
           end, undefined, L).


%%------------------------------------------------------------------------------
out(Arg) ->
   case get_upgrade_header(Arg#arg.headers) of
   true ->
       error_logger:warning_msg("Not a web socket client~n"),
       {content, "text/plain", "You're not a web sockets client! Go away!"};
   false ->
       error_logger:info_msg("Starting web socket~n"),
       {websocket, sole_callback, []}
   end.
</erl>

Upvotes: 0

Views: 54

Answers (0)

Related Questions