Created
October 31, 2010 06:48
-
-
Save ibdknox/656227 to your computer and use it in GitHub Desktop.
compojure with websockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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