note description : "simple application execution" date : "$Date$" revision : "$Revision$" class APPLICATION_EXECUTION inherit WSF_WEBSOCKET_EXECUTION WEB_SOCKET_EVENT_I create make feature -- Basic operations execute local s: STRING dt: HTTP_DATE do -- To send a response we need to setup, the status code and -- the response headers. if request.path_info.same_string_general ("/app") then s := websocket_app_html (9090) else s := "Hello World!" create dt.make_now_utc s.append (" (UTC time is " + dt.rfc850_string + ").") s.append ("

Websocket demo

") end response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"], ["Content-Length", s.count.out]>>) response.set_status_code ({HTTP_STATUS_CODE}.ok) response.header.put_content_type_text_html response.header.put_content_length (s.count) if request.is_keep_alive_http_connection then response.header.put_connection_keep_alive end response.put_string (s) end feature -- Websocket execution new_websocket_handler (ws: WEB_SOCKET): WEB_SOCKET_HANDLER do create Result.make (ws, Current) end feature -- Websocket execution on_open (ws: WEB_SOCKET) do ws.put_error ("Connecting") ws.send (Text_frame, "Hello, this is a simple demo with Websocket using Eiffel. (/help for more information).%N") end on_binary (ws: WEB_SOCKET; a_message: READABLE_STRING_8) do ws.send (Binary_frame, a_message) end on_text (ws: WEB_SOCKET; a_message: READABLE_STRING_8) do if a_message.same_string_general ("/help") then -- Echo the message for testing. ws.send (Text_frame, "Help: available commands%N - /time : return the server UTC time.%N") elseif a_message.starts_with_general ("/time") then ws.send (Text_frame, "Server time is " + (create {HTTP_DATE}.make_now_utc).string) else -- Echo the message for testing. ws.send (Text_frame, a_message) end end on_close (ws: WEB_SOCKET) -- Called after the WebSocket connection is closed. do ws.put_error ("Connection closed") end feature -- HTML Resource websocket_app_html (a_port: INTEGER): STRING do Result := "[ WebSockets Client

WebSockets Client

]" Result.replace_substring_all ("##PORTNUMBER##", a_port.out) if request.is_https then Result.replace_substring_all ("##HTTPSCHEME##", "https") Result.replace_substring_all ("##WSSCHEME##", "wss") else Result.replace_substring_all ("##HTTPSCHEME##", "http") Result.replace_substring_all ("##WSSCHEME##", "ws") end end end