Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jldoubleu
Last active June 6, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jldoubleu/70dbe2eb6d31ae8a5c36730d748e0fc0 to your computer and use it in GitHub Desktop.
Save jldoubleu/70dbe2eb6d31ae8a5c36730d748e0fc0 to your computer and use it in GitHub Desktop.
sente websocket server example
curl -v -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: localhost:8080" -H "Origin: http://localhost:8080" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" -H "Sec-WebSocket-Version: 13" http://localhost:8080/chsk
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /chsk HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> Connection: Upgrade:csrf-token-fn nil}
> Upgrade: websocket
> Origin: http://localhost:8080
> Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==
> Sec-WebSocket-Version: 13
>
< HTTP/1.1 500 Internal Server Error
HTTP/1.1 500 Internal Server Error
< Content-Length: 148
Content-Length: 148
< Server: http-kit
Server: http-kit
< Date: Thu, 06 Jun 2019 11:14:06 GMT
Date: Thu, 06 Jun 2019 11:14:06 GMT
<
Client's Ring request doesn't have a client id. Does your server have the necessary keyword Ring middleware (`wrap-params` & `wrap-keyword-params`)?* Connection #0 to host localhost left intact
(ns fig-example.server
(:require [org.httpkit.server :as server]
[taoensso.sente :as sente]
[taoensso.sente.server-adapters.http-kit :refer (get-sch-adapter)]
[ring.middleware.keyword-params]
[ring.middleware.params]
[compojure.core :refer [defroutes GET POST]])
(:gen-class))
(let [{:keys [ch-recv send-fn connected-uids
ajax-post-fn ajax-get-or-ws-handshake-fn]}
(sente/make-channel-socket! (get-sch-adapter) {:csrf-token-fn nil}})] ; passing nil for the csrf token function, probably a terrible idea, but it makes the example work
(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
)
(defn foo
[req]
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>This is also a test</h1>"})
(defroutes app-routes
;; sente routes
(GET "/chsk" req (ring-ajax-get-or-ws-handshake req))
(POST "/chsk" req (ring-ajax-post req))
;; sanity check route
(GET "/test" [] foo))
(def app
(-> app-routes
ring.middleware.keyword-params/wrap-keyword-params
ring.middleware.params/wrap-params))
(defn -main []
(server/run-server app {:port 8080})
(println "Server Running"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment