Forráskód Böngészése

ios http request works

Harlan Iverson 8 éve
szülő
commit
538b4f71a6
2 módosított fájl, 45 hozzáadás és 11 törlés
  1. 42 9
      ios-client/src/sample/core.clj
  2. 3 2
      web-backend/src/example/core.clj

+ 42 - 9
ios-client/src/sample/core.clj

@@ -1,17 +1,17 @@
 (ns sample.core
     (:require [uikit.core :as uikit]
               [com.stuartsierra.component :as component]
-              [com.github.harlanji.clojure_stack.greetings :as c]))
-
-; last idea: name mangling of dash/underscore isn't right... try eliminating from package. must sleep.
-
+              [com.github.harlanji.clojure_stack.greetings :as c])
+  (:import ;[com.google.j2objc.net IosHttpURLConnection]
+           [java.net URL]))
 (def white (-> ($ UIColor)
                ($ :whiteColor)))
 
-(declare abc)
+(declare abc say-hi)
 
 (defn on-tap [scope]
-      (println "Tap"))
+      (println "Tap")
+      (say-hi))
 
 (defn- my-screen [g] [:UIView :main
                 {:setBackgroundColor white
@@ -20,6 +20,38 @@
                 [(uikit/button 1) :button {:setTitle:forState [g 0]
                                            :gestures          {:UITapGestureRecognizer #'on-tap}}]])
 
+(defn say-hi []
+  (let [c (.openConnection (URL. "http://localhost:3000/hi"))]
+    (println "hi: :)" (slurp (.getContent c)))))
+
+(comment defn say-hi []
+  (let [uri (str "http://localhost:3000/hi")
+        session ($ ($ NSURLSession) :sharedSession)
+        _ (println "session:" session)
+        url ($ ($ NSURL) :URLWithString uri)
+        _ (println "url:" url)
+        req ($ session
+               :dataTaskWithURL url
+               :completionHandler (fn [a b c]
+                                      (println "got hi :)")))
+        _ (println "req:" req)
+        done ($ req :resume)]
+
+       (println "uh, done:" done)))
+
+; https://github.com/google/j2objc/blob/master/jre_emul/Classes/com/google/j2objc/net/IosHttpURLConnection.java#L369
+(comment "
+      NSURLSession *session = [NSURLSession sharedSession];
+      [[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
+        completionHandler:^(NSData *data,
+                                   NSURLResponse *response,
+                                   NSError *error) {
+                                                    // handle response
+
+                                                    }] resume];
+
+")
+
 (defrecord MainUI []
            component/Lifecycle
            (start [component]
@@ -49,6 +81,7 @@
 (defonce system (atom (component/system-map :ui (->MainUI))))
 
 (defn main []
-      (println "Hai! hello:" (c/hello-again))
-      (let [{:keys [ui]} (swap! system component/start)]
-           ui))
+  (println "Hai! hello:" (c/hello-again))
+
+  (let [{:keys [ui]} (swap! system component/start)]
+    ui))

+ 3 - 2
web-backend/src/example/core.clj

@@ -11,8 +11,9 @@
               ))
 
 (defn say-hi [req]
-      (when (.startsWith (:uri req) "/hi")
-            {:status 200 :body "hi" :content-type "text/plain"}))
+  (when (.startsWith (:uri req) "/hi")
+    (println "/hi")
+    {:status 200 :body "haiii" :headers {"Content-Type" "text/plain"}}))
 
 (defn sente-handler [uri]
   (let [{:keys [ch-recv