0

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>
  • What problem are you seeing, exactly? Please provide more details about where the error is occurring (for example, in the client or server), any error messages you're getting, and what actions to take to try to duplicate the problem. – Steve Vinoski Nov 20 '21 at 01:34
  • Also, note that page 72 of the book you reference says, "The Yaws WebSockets interfaces have changed recently. This chapter works with Yaws Version 1.92, which was released December 23, 2011; future versions may change things again." 1.92 is _really_ old. I released the current version of Yaws, 2.1.0, earlier this week. – Steve Vinoski Nov 20 '21 at 02:13

0 Answers0