Source file httpun_ws_async.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
open Core
open Async

let sha1 s =
  s
  |> Digestif.SHA1.digest_string
  |> Digestif.SHA1.to_raw_string

module Server = struct
  let create_connection_handler
    ?(config = Httpun.Config.default)
    ~websocket_handler
    ~error_handler = fun client_addr socket ->
    let connection =
      Httpun_ws.Server_connection.create
        ~sha1
        ~error_handler:(error_handler client_addr)
        (websocket_handler client_addr)
    in
    Gluten_async.Server.create_connection_handler
      ~read_buffer_size:config.read_buffer_size
      ~protocol:(module Httpun_ws.Server_connection)
      connection
      client_addr
      socket
end

module Client = struct
  let connect ~nonce ~host ~port ~resource ~error_handler ~websocket_handler socket =
    let headers = Httpun.Headers.of_list
      ["host", String.concat ~sep:":" [host; string_of_int port]]
    in
    let connection =
      Httpun_ws.Client_connection.connect
        ~nonce
        ~headers
        ~sha1
        ~error_handler
        ~websocket_handler
        resource
    in
    Deferred.ignore_m
      (Gluten_async.Client.create
        ~read_buffer_size:0x1000
        ~protocol:(module Httpun_ws.Client_connection)
        connection
        socket)
end