Skip to content

Commit

Permalink
Merge pull request #10 from romanchechyotkin/sub
Browse files Browse the repository at this point in the history
Sub
  • Loading branch information
romanchechyotkin authored Aug 23, 2024
2 parents a7c13d2 + b96a7b9 commit 7abb694
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,26 @@ type t = {
exception Connection_refused
(** Raised when failed to make a TCP connection. *)

let crlf = "\r\n"

module Send = struct
let pong conn = Lwt_io.write conn.oc "PONG\r\n"
let ping conn = Lwt_io.write conn.oc "PING\r\n"
let pong conn = Lwt_io.write conn.oc (Printf.sprintf "PONG%s" crlf)
let ping conn = Lwt_io.write conn.oc (Printf.sprintf "PING%s" crlf)

let pub ~subject ~reply_to ~payload conn =
Lwt_io.fprintf conn.oc "PUB %s%s %d\r\n%s\r\n" subject
Lwt_io.fprintf conn.oc "PUB %s%s %d%s%s%s" subject
(Option.fold ~none:"" ~some:(Printf.sprintf " %s") reply_to)
(String.length payload) payload
(String.length payload) crlf payload crlf

let sub ~subject ~queue_group ~sid conn =
Lwt_io.fprintf conn.oc "SUB %s%s %s\r\n" subject
Lwt_io.fprintf conn.oc "SUB %s%s %s%s" subject
(Option.fold ~none:"" ~some:(Printf.sprintf " %s") queue_group)
sid
sid crlf

let connect ~json conn =
(* NOTE: Yojson.Safe.pp gives a bad result.
TODO: improve performance of JSON encoding (now is bad) *)
Lwt_io.fprintf conn.oc "CONNECT %s\r\n" (Yojson.Safe.to_string json)
Lwt_io.fprintf conn.oc "CONNECT %s%s" (Yojson.Safe.to_string json) crlf

(* TODO: add other *)
end
Expand Down

0 comments on commit 7abb694

Please sign in to comment.