websocket_example1.anubis 1.09 KB


   $begin
   Here is an example of a subprotocol handler that is as simple as possible. 
   
   The client browser sends a character string obtained from an input HTML
   element, and the server returns the character string capitalized, that
   replaces the content of the input element.  
   $acode(    
read system/string.anubis
read http/websocket_handler.anubis
   )
   $acode( 
define (TxtBin,WebSocketMessage) -> One
   the_example1_handler
   (
     MaybeTicket                                            mb_ticket, 
     (TxtBin,WebSocketMessage) -> Var(WebSocketSendControl) send_message,
     One -> One                                             close_connection 
   ) =
   (TxtBin tb, WebSocketMessage msg) |-> 
      if to_string(msg) is 
      {
        failure     then  unique, 
        success(s)  then 
          forget(send_message(
             tb,to_websocket_message(to_byte_array(to_upper(s)),1000)))
      }. 
   )
   Here is the secondary module. 
   $acode(  
module WebSocketHandler
   websocket_example1
   =
   websocket_handler
   (
     the_example1_handler
   ). 
   ) 
   $end