juxt / bidi

Bidirectional URI routing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Routes for Sente's chsk

manuel-uberti opened this issue · comments

Hello,

first of all thanks for all the great work. Been using bidi successfully with ClojureScript.

I have a question with regard to sente routes. As per sente example project, there need to be two routes for /chsk, one GET and one POST.

This is what I came up with:

(defn index-handler [request]
  (-> (response/ok index-html)
      (response/header "content-type" "text/html; charset=utf-8")))

(defn sente-get-handler [request]
  (ws/ring-ajax-get-or-ws-handshake request))

(defn sente-post-handler [request]
  (ws/ring-ajax-post request))

(def routes
  ["/" {"" index-handler
        "chsk" {:get {"" sente-get-handler}
                :post {"" sente-post-handler}}}])

A quick test in the REPL shows that I am missing something though:

rfs.handler> (bidi/match-route routes "/")
;; => {:handler #function[rfs.handler/index-handler]}
rfs.handler> (bidi/match-route routes "/chsk")
;; => nil
rfs.handler> 

I have also tried the guards example in the documentation:

rfs.handler> (def test-routes ["/" {"blog" {:get
                {"/index" (fn [req] {:status 200 :body "Index"})}}
              {:request-method :post :server-name "juxt.pro"}
                {"/zip" (fn [req] {:status 201 :body "Created"})}}])
;; => #'rfs.handler/test-routes
rfs.handler> (bidi/match-route test-routes "/blog/index")
;; => nil
rfs.handler> 

You've got it right, don't worry. When working with guarded routes, you need to supply the additional data that is required. In this example:

(bidi/match-route* routes "/chsk" {:request-method :get})
(bidi/match-route* routes "/chsk" {:request-method :post})

You can see that in practice in the bidi.ring namespace

Thanks, that works from the REPL.
When I try with Ring, I am still getting this error though:

WebSocket connection to 'ws://localhost:8080/chsk?client-id=f8289ae4-a732-4fd4-88c5-293ddb8ca29b' failed: Error during WebSocket handshake: Unexpected response code: 404

This is what I am doing:

(def app
  (let [ring-handler (wrap-defaults handler site-defaults)]
    (-> ring-handler
        wrap-exceptions
        wrap-reload)
    ring-handler))

Where handler is:

(def routes
  ["/" {"" index-handler
        "chsk" {:get {"" sente-get-handler}
                :post {"" sente-post-handler}}}])

(def handler
  (make-handler routes))

If you need more details, feel free to ask.

If you remove the guard, and have a dumb handler which does a prn on the req, what does the stdout look like? I'm guessing it might look different for websocket requests as opposed to normal ring requests.

Here it is:

{:cookies {"ring-session" {:value "a6bc51e3-e103-49c4-8a47-698a61c23b1c"}}, :remote-addr "127.0.0.1", :params {:client-id "3e933972-5ba3-43cf-a52a-d9aed256d458"}, :flash nil, :route-params nil, :headers {"origin" "http://localhost:8080", "host" "localhost:8080", "upgrade" "websocket", "user-agent" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36", "cookie" "ring-session=a6bc51e3-e103-49c4-8a47-698a61c23b1c", "connection" "Upgrade", "pragma" "no-cache", "sec-websocket-key" "KpmnFrUiKbVJzBGPRi/dzQ==", "accept-language" "en-US,en;q=0.8", "sec-websocket-version" "13", "accept-encoding" "gzip, deflate, sdch, br", "sec-websocket-extensions" "permessage-deflate; client_max_window_bits", "cache-control" "no-cache"}, :async-channel #object[org.httpkit.server.AsyncChannel 0x3130c44 "/127.0.0.1:8080<->/127.0.0.1:45708"], :server-port 8080, :content-length 0, :form-params {}, :websocket? true, :session/key "a6bc51e3-e103-49c4-8a47-698a61c23b1c", :query-params {"client-id" "3e933972-5ba3-43cf-a52a-d9aed256d458"}, :content-type nil, :character-encoding "utf8", :uri "/chsk", :server-name "localhost", :query-string "client-id=3e933972-5ba3-43cf-a52a-d9aed256d458", :body nil, :multipart-params {}, :scheme :http, :request-method :get, :session {:ring.middleware.anti-forgery/anti-forgery-token "I6pt9ren3x4WgMsmumTV9DSyXpcVWQx403JXLds2yqFTc5p39sz4HvdWW6VrhZnr35xgP81+frR4fUdw"}}

To expand on this. I did a lein do clean, cljsbuild auto and a lein run and tested again from different browsers.

Now it seems to be working fine.

Channel socket successfully established!: {:type :ws, :open? true, :ever-opened? true, :uid :taoensso.sente/nil-uid, :csrf-token "PNyBEFALvgxm/JHy8IDyFsnR0eZDQayFInRmhX+lWfqJP6FzMKPhVqgP4bXZiks8c/YtdMYRTvdDZtbO", :handshake-data nil, :first-open? true}
encore.cljs:2615
Handshake: [:taoensso.sente/nil-uid "PNyBEFALvgxm/JHy8IDyFsnR0eZDQayFInRmhX+lWfqJP6FzMKPhVqgP4bXZiks8c/YtdMYRTvdDZtbO" nil true]

Thanks again for helping me on this.