Skip to content

Instantly share code, notes, and snippets.

@Kakadu
Created December 23, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kakadu/1e922258006bd2bcc1a1527d5a891925 to your computer and use it in GitHub Desktop.
Save Kakadu/1e922258006bd2bcc1a1527d5a891925 to your computer and use it in GitHub Desktop.
Part of issue for vscode-reasonm
module Path = struct
type pathT
external join : string array -> string = "" [@@bs.module "path"] [@@bs.splice ]
end
module Express = struct
type expressT
external express : unit -> expressT = "" [@@bs.module ]
external use : expressT -> string -> unit = "use" [@@bs.send ]
external static : string -> string = "static" [@@bs.module "express"]
type responseT
external sendFile : responseT -> string -> 'a -> unit = "sendFile" [@@bs.send]
external get :
expressT -> string -> ('a -> responseT -> unit) -> unit = "get" [@@bs.send]
end
module Http = struct
type http
external create : Express.expressT -> http = "Server" [@@bs.module "http"]
external listen : http -> int -> (unit -> unit) -> unit = "" [@@bs.send ]
end
let app = Express.express ()
let http = Http.create app
external __dirname : string = "" [@@bs.val]
let _ =
Express.use app @@
Express.static (Path.join [| __dirname; ".." |])
let () =
Express.get app "/" (fun _ res ->
Express.sendFile res "index.html" ([%bs.obj { root = __dirname }]))
module MyServer = BsSocket.Server.Make(ExampleMessages)
external serveClient : BsSocket.Server.serverT -> (bool * (string list) -> unit) -> unit = "clients" [@@bs.send ]
let io = MyServer.createWithHttp http
let socket =
MyServer.onConnect io (fun socket ->
let open MyServer in
print_endline "Got a connection!";
let socket = Socket.join socket "someRoom" in
Socket.on socket (function
| Shared message ->
Socket.broadcast socket message;
Socket.emit socket message
| Hi ->
Js.log "oh, hi client.";
Js.log "Sorry I can't say hi back. Try uncommenting the line below to see why."
)
)
(*
let () =
let loop () = () in
setInterval loop (10*1000)
*)
let () =
Http.listen http 3000 (fun () ->
print_endline "listening on *:3000")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment