Last active
August 29, 2015 14:20
-
-
Save j0sh/71c31743d4057a6f365f to your computer and use it in GitHub Desktop.
websocket disc test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>WebSocket</title> | |
| <script type="text/javascript"> | |
| function domLoaded(s) | |
| { | |
| (new WebSocket("ws://localhost:8080/ws")).onopen = function() { | |
| console.log("Connected to WebSocket Server"); | |
| }; | |
| } | |
| window.onload = domLoaded; | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let (>>=) = Lwt.bind | |
| let get_contents stream = | |
| let rec f = function | |
| | None -> Lwt.return_unit | |
| | Some fr -> Lwt_stream.get stream >>= f in | |
| Lwt_stream.get stream >>= f | |
| let cxn_fn uri (stream, _) = | |
| get_contents stream >>= fun () -> Lwt_io.printl "user disconnected" | |
| let sockaddr_of_dns node service = | |
| let open Lwt_unix in | |
| Lwt.map (fun ai -> ai.ai_addr) | |
| (getaddrinfo node service [AI_FAMILY(PF_INET); AI_SOCKTYPE(SOCK_STREAM)] >>= | |
| function h::t -> Lwt.return h | |
| | [] -> Lwt.fail Not_found) | |
| let rec wait_forever () = Lwt_unix.sleep 1000. >>= wait_forever | |
| let run_server node service = | |
| sockaddr_of_dns node service >>= fun sa -> | |
| Lwt.return (Websocket.establish_server sa cxn_fn) >>= fun _ -> wait_forever () | |
| let () = Lwt_main.run (run_server "0.0.0.0" "7890") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment