Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ibdknox
Created October 31, 2010 06:48
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ibdknox/656227 to your computer and use it in GitHub Desktop.
Save ibdknox/656227 to your computer and use it in GitHub Desktop.
compojure with websockets
(ns wl.core
(:use compojure.core, aleph.core, aleph.http, hiccup.core, hiccup.page-helpers)
(:require [compojure.route :as route])
(:gen-class))
(def broadcast-channel (channel))
(defn chat-handler [ch handshake]
(receive ch
(fn [name]
(siphon broadcast-channel ch))))
(defn layout [& content]
(html5 [:head [:title "new page"] (include-js "woot.js")]
[:body content]))
(defroutes my-app
(GET "/" [] (layout [:p "awesome!"]))
(GET "/socket" [] (wrap-aleph-handler chat-handler))
(route/not-found (layout [:p "aww... this doesn't exist"])))
(defn main []
(start-http-server (wrap-ring-handler my-app) {:port 8080 :websocket true})
(println "server started")
var count = 0;
var clients = 0;
var total = 0;
module.exports = function(ws) {
ws.onopen = function() {
clients++;
console.log("Connected: " + clients);
};
ws.onmessage = function(m) {
count++;
if(count % clients == 0) {
total++;
console.log("All received: " + total);
count = 0;
}
};
};
;; this one will work just fine.
(ns wl.core
(:use compojure.core, aleph.core, aleph.http, hiccup.core, hiccup.page-helpers)
(:require [compojure.route :as route])
(:gen-class))
(def broadcast-channel (channel))
(defn chat-handler [ch handshake]
(receive ch
(fn [name]
(siphon broadcast-channel ch))))
(defn main []
(start-http-server chat-handler {:port 8080 :websocket true})
(println "server started"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment