我正在使用HTTP-KIT来编写简单的服务器和客户端HTTP。现在我想发送一些使用gloss编码的数据。
我的光泽代码是这样的:
(gcore/defcodec sized-string (gcore/finite-frame :uint16-be
(gcore/string :utf-8)))
(gcore/defcodec ordered-map-codec
(gcore/ordered-map :msg sized-string
:type sized-string))
此代码效果很好:
(def data {:msg "Hello World", :type "alert"})
(def encoded
(gio/encode ordered-map-codec data))
(gio/decode ordered-map-codec encoded)
在我的服务器/客户端HTTP代码中,我有:
对于服务器:
(defonce server (atom nil))
(defn handler [request]
(println (gio/decode ordered-map-codec (get request :body ))))
(defn -main
[]
(reset! server (run-server handler {:port 8080})))
对于客户:
(def options {:timeout 200
:body (gio/encode ordered-map-codec data)})
(http/post "http://localhost:8080" options
(fn [{:keys [status headers body error]}]
(if error
(println "Failed, exception is " error)
(println "Async HTTP GET: " status))))
然后当我尝试使用客户端发送一些data
:
Sat Apr 18 11:24:24 BRT 2015 [worker-3] ERROR - GET /
java.lang.Exception: Insufficient bytes to decode frame.
at gloss.io$decode.invoke(io.clj:85)
at gloss.io$decode.invoke(io.clj:79)
at tapahtuma_server.server$handler.invoke(server.clj:35)
at org.httpkit.server.HttpHandler.run(RingHandler.java:91)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Async HTTP GET: 500
感谢您的帮助