jsa-aerial / hanasu

A very light weight, simple websocket based messaging system in Clojure(Script). Converse/talk 話す(hanasu).
MIT License
26 stars 1 forks source link

Unexpected response code: 200 at client.cli/open-connection #1

Open MawiraIke opened 4 years ago

MawiraIke commented 4 years ago

I get an unexpected response code error when I use cli/open-connection. The error is thrown in ws/connect url {...} at

(let [...
       socket (js/WebSocket. url protocols)
       ...]

in haslett.client.

The error thrown is

client.cljs?rel=1578861696350:27 WebSocket connection to 'ws://localhost:10666/ws' failed: Error during WebSocket handshake: Unexpected response code: 302
Expected behaviour

A connection to an already running clojure-side server would go through to enable sending of messages between both sides. The server starts successfully from the clojure side.

Actual behaviour

Error Error during WebSocket handshake: Unexpected response code: 302

I am using aerial.hanasu v0.2.3 What could the problem be or is this an issue for haslett?

jsa-aerial commented 4 years ago

The general answer to this is 'your endpoint isn't what you think it is'. That could mean the socket isn't what you think it is or more likely your routing isn't what you think it is.

If you are not using the defaults for some reason, you need to make your own handler and routes using hanasu-handlers:

https://github.com/jsa-aerial/hanasu/blob/16b8cdd572fd2519b16cb0d0c3d15369f26e55fc/src/clj/aerial/hanasu/server.clj#L80

as shown here:

https://github.com/jsa-aerial/hanami/blob/cd7d66f80ac05d8769cbe85d91a0020bd9cb11cd/src/clj/aerial/hanami/core.clj#L192

and used here: https://github.com/jsa-aerial/hanami/blob/cd7d66f80ac05d8769cbe85d91a0020bd9cb11cd/examples/basic_charts.clj#L31

MawiraIke commented 4 years ago

Thank you for your reply and for the repo hanasu, really love its simplicity.

The project I am working on uses a custom handler and I think it should work. The :main-handler is

(def main-ring-handler
  (-> my-routes
      (ring.middleware.defaults/wrap-defaults ring.middleware.defaults/site-defaults)
      (wrap-cljsjs)
      (wrap-gzip)))

where my-routes is a compojure's defroutes value:

(defroutes my-routes
  (GET  "/" req (response/content-type
                  {:status 200
                   :session (if (session-uid req)
                              (:session req)
                              (assoc (:session req) :uid (unique-id)))
                   :body (io/input-stream (io/resource "oz/public/index.html"))}
                  "text/html"))
    ...)

Started the server with

(srv/start-server 10666 :main-handler main-ring-handler)

Edit: Changed the connection on Cljs to be (cli/open-connection "ws://localhost:10666/"), to match the url "/". Getting an error of status code 200

Error during WebSocket handshake: Unexpected response code: 200