我正在尝试从本地主机上的两个不同端口设置 Web 套接字连接。我正在使用 Sente 和 Immutant。我有以下内容,但在尝试连接时返回 403 禁止
服务器.clj
(defn handler
"Comment"
[]
"<h1>Hello World</h1>")
(let [{:keys [ch-recv send-fn connected-uids
ajax-post-fn ajax-get-or-ws-handshake-fn]}
(sente/make-channel-socket! (get-sch-adapter) {})]
(def ring-ajax-post ajax-post-fn)
(def ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn)
(def ch-chsk ch-recv) ; ChannelSocket's receive channel
(def chsk-send! send-fn) ; ChannelSocket's send API fn
(def connected-uids connected-uids) ; Watchable, read-only atom
)
(defroutes app
"The router."
(GET "/" [] (handler))
(GET "/chsk" req (ring-ajax-get-or-ws-handshake req))
(POST "/chsk" req (ring-ajax-post req))
(route/not-found
"<h1>Page not found</h1>"))
(def my-app
(-> app
;; Add necessary Ring middleware:
ring.middleware.keyword-params/wrap-keyword-params
ring.middleware.params/wrap-params))
(def wrapped
(wrap-cors my-app :access-control-allow-origin [#".*"]
:access-control-allow-methods [:get :put :post :delete]))
(defn -main
"Start the server"
[& args]
(immutant/run wrapped {:host "localhost" :port 8080 :path "/"}))
这不会引发任何错误,并且“/”路由会正确显示。
客户端.cljs
(let [{:keys [chsk ch-recv send-fn state]}
(sente/make-channel-socket! "/chsk" ; Note the same path as before
"sdasds" ; dummy
{:type :auto ; e/o #{:auto :ajax :ws}
:host "localhost:8080/"
}
)]
(def chsk chsk)
(def ch-chsk ch-recv) ; ChannelSocket's receive channel
(def chsk-send! send-fn) ; ChannelSocket's send API fn
(def chsk-state state) ; Watchable, read-only atom
)
这会在尝试连接时引发 403 错误。我不知道它为什么会这样,我已经看了一段时间了,但我觉得很短。