Last active
February 2, 2016 19:18
-
-
Save torbjornvatn/4835120dbd6156c760d1 to your computer and use it in GitHub Desktop.
Unacaster examples
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
#!/usr/bin/env node | |
var WebSocketClient = require('websocket').client; | |
var Player = require('play-sound')(opts = {"player": "omxplayer"}) | |
var winston = require('winston'); | |
winston.add(winston.transports.File, { filename: 'unacaster.log' }); | |
var client = new WebSocketClient(); | |
var playing = false; | |
var reconnectInterval = 1 * 1000 * 60; | |
var connect = function() { | |
client.connect('ws://██████████████████/ws', null); | |
}; | |
client.on('connectFailed', function(error) { | |
winston.info('Connect Error: ' + error.toString()); | |
}); | |
client.on('connect', function(connection) { | |
winston.info('WebSocket Client Connected'); | |
connection.on('error', function(error) { | |
winston.info("Connection Error: " + error.toString()); | |
}); | |
connection.on('close', function() { | |
winston.info('Connection Closed! Trying to reconnect in ' + reconnectInterval/1000 + ' seconds'); | |
setTimeout(connect, reconnectInterval); | |
}); | |
connection.on('message', function(message) { | |
if (message.type === 'utf8') { | |
var name = message.utf8Data; | |
winston.info("Received: '" + name + "'"); | |
if (!playing) { | |
playing = true; | |
winston.info("Playing: " + name + ".mp3"); | |
Player.play('./mp3s/' + name + '.mp3', function(err){ | |
playing=false; | |
}); | |
} | |
} | |
}); | |
function sendNumber() { | |
if (connection.connected) { | |
var number = Math.round(Math.random() * 0xFFFFFF); | |
connection.sendUTF(number.toString()); | |
setTimeout(sendNumber, 1000); | |
} | |
} | |
sendNumber(); | |
}); | |
connect(); |
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 unacaster.handler | |
(:require [org.httpkit.server :refer :all] | |
[clj-time.core :as t] | |
[clj-time.format :as f] | |
[compojure.core :refer :all] | |
[compojure.route :as route] | |
[clojure.tools.logging :as log] | |
[compojure.handler :refer [site]] | |
[clojure.data.json :as json] | |
[clojure.pprint :refer [pprint]] | |
[ring.middleware.reload :as reload] | |
[ring.middleware.defaults :refer [wrap-defaults api-defaults]])) | |
(def users {███ "torbjorn" | |
███ "heim" | |
███ "martin" | |
███ "romet" | |
███ "kjartan" | |
███ "birte" | |
███ "petter" | |
███ "frode" | |
███ "ken"}) | |
(def broadcasted-today (atom {})) | |
(defn date [] (f/unparse (f/formatters :date) (t/now))) | |
(def listeners (atom {})) | |
(defn unacast [id] | |
(let [today (date)] | |
(if (= (get @broadcasted-today id) today) | |
(do | |
(log/info "Id" id "has been broadcasted today already") | |
"Broadcasted today already!") | |
(do | |
(doseq [listener (keys @listeners)] | |
(send! listener (get users (read-string id)))) | |
(swap! broadcasted-today assoc id today) | |
"Broadcasting")))) | |
(defn ws-handler [request] | |
(with-channel request channel | |
(on-close channel (fn [status] | |
(do | |
(swap! listeners dissoc channel) | |
(log/info "channel closed: " status)))) | |
(on-receive channel (do | |
(log/info "channel opened" channel) | |
(swap! listeners assoc channel true))))) | |
(defroutes app-routes | |
(GET "/" [] "Hello World") | |
(GET "/ws" [] ws-handler) | |
(GET "/unacast" {{id :id} :params} (unacast id)) | |
(POST "/writeup" [account_id beacon_id] | |
(do | |
(log/info (str "Account id: " account_id " has checked in")) | |
(unacast account_id))) | |
(route/not-found "Not Found")) | |
(def app | |
(-> app-routes | |
(wrap-defaults api-defaults))) | |
(defn in-dev? [] false) | |
(defn -main [& {:as args}] ;; entry point, lein run will pick up and start from here | |
(let [handler (if in-dev? | |
(reload/wrap-reload (site #'app)) ;; only reload when dev | |
(site app)) | |
port (Integer. (or (get args "port") "1337"))] | |
(do | |
(log/info (str "Running on " port)) | |
(run-server handler {:port port})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment