Skip to content

Commit

Permalink
Updated client to support new UDP server.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryugi committed Jun 1, 2017
1 parent 801b492 commit ec2ec4f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/cljs/cli/timi/client/cli.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[clojusc.twig :as logger]
[taoensso.timbre :as log]
[timi.client.config :as config]
[timi.client.tcp :as tcp]))
[timi.client.udp :as udp]))

;;; CLI setup and functions

Expand All @@ -18,29 +18,31 @@
[args]
(str (string/join " "args) "\n"))

;;; Callbacks
(defn wait
[]
(js/setTimeout #(log/info "UDP client timed out.")
3000))

(defn handle-connect
[client data]
(log/debug "Connected.")
(tcp/send client data))
;;; UDP Callback

(defn handle-receive
[client data]
(let [buffer (js/Buffer. data)]
(log/debug "Received data:" data)
(tcp/disconnect client)
(udp/close client)
(log/debug "Disconnected.")
(println (str data))))
(println (str data))
(.exit node/process)))

;;; Main

(defn -main
[& args]
(log/debug "Got args:" args)
(let [client (tcp/connect config/data)
(let [client (udp/client)
data (args->str args)]
(tcp/on-connect client #(handle-connect client data))
(tcp/on-receive client #(handle-receive client %))))
(udp/on-receive client #(handle-receive client %))
(udp/send client config/data data)
(wait)))

(set! *main-cli-fn* -main)
37 changes: 37 additions & 0 deletions src/cljs/cli/timi/client/udp.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns timi.client.udp
(:require
[cljs.nodejs :as node]
[taoensso.timbre :as log]))

(def dgram (node/require "dgram"))

(defn client
[]
(log/debug "Creating datagram socket ...")
(.createSocket dgram "udp4"))

(defn on-listen
[client callback]
(.on client "listening" callback))

(defn send
[client config data]
(let [buf (js/Buffer. data)
cfg (get-in config [:cli :server])]
(log/debug cfg)
(log/debug "Sending data:" data)
(.send client
buf
0
buf.length
(get-in config [:cli :server :port]))))

(defn close
[client]
(log/debug "Disconnecting ...")
(.close client))

(defn on-receive
[client callback]
(log/debug "Receiving data ...")
(.on client "message" callback))

0 comments on commit ec2ec4f

Please sign in to comment.