Skip to content

Commit 034c928

Browse files
author
Brad Peterson
committed
Merge pull request #3 from clojurecup2014/websocketerror
Websocketerror
2 parents 0c9bb96 + 8dad6a4 commit 034c928

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/chatb0x/websockets.clj

+17-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require [org.httpkit.server :refer [with-channel on-close on-receive send!]]
33
[chatb0x.user :refer :all]
44
[cheshire.core :refer [generate-string]]
5+
[cemerick.friend :as friend]
56
[digest :refer [md5]]
67
[clojure.string :as str]))
78

@@ -76,23 +77,23 @@
7677
;; Agent visitor handling
7778
(defn send-msg2 [client1 client2 msg]
7879
(do (if client1
79-
(println "Sending client1: " msg)
80-
(send! client1 msg false))
80+
(do (println "Sending client1: " client1 msg)
81+
(send! client1 msg false)))
8182
(if client2
82-
(println "Sending client2" msg)
83-
(send! client2 msg false))))
83+
(do (println "Sending client2" client2 msg)
84+
(send! client2 msg false)))))
8485

8586
(defn msg-init [client1 client2]
8687
"Send address of opposite end to both clients"
87-
(send! client1 (generate-string {:channel client2}) false)
88-
(send! client2 (generate-string {:channel client1}) false))
88+
(send! client1 (pr-str {:channel client2}) false)
89+
(send! client2 (pr-str {:channel client1}) false))
8990

9091
(defn msg-text [sender data]
9192
(let [agent (get-agent sender)
9293
visitor (get-visitor data)
9394
text (get-text data)]
94-
(println "In msg-text")
95-
(send-msg2 agent visitor (generate-string {:ch-visitor (:ch-visitor data) :message text}))))
95+
(println "In msg-text" agent visitor)
96+
(send-msg2 agent visitor (pr-str {:ch-visitor (:ch-visitor data) :message text}))))
9697

9798
(defn msg-close [client]
9899
(let [agent (get-agent client)
@@ -101,9 +102,9 @@
101102
(do (println "websockets: agent closed, send closed message to all visitors" (get-agent-visitors agent))
102103
(doseq [visitor (get-agent-visitors agent)]
103104
(println "Sending msg to visitor " visitor)
104-
(send! visitor (generate-string {:agent agent :visitor visitor :message text}) false)))
105+
(send! visitor (pr-str {:agent agent :visitor visitor :message text}) false)))
105106
(do (println "websockets: visitor closed, send closed message to the agent" client agent)
106-
(if (= agent nil) (send! agent (generate-string {:agent agent :visitor client :message text}) false))
107+
(if (= agent nil) (send! agent (pr-str {:agent agent :visitor client :message text}) false))
107108
(println "barbazbop")))))
108109

109110
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -114,7 +115,7 @@
114115
(with-channel req channel
115116
;; CONNECT
116117
(println req)
117-
(if (contains? (get-in req [:session :cemerick.friend/identity :authentications nil :roles]) :chatb0x.user/agent)
118+
(if (friend/authorized? #{:chatb0x.user/agent} (friend/identity req))
118119
(do (println "Agent connected: " channel) ;; Agent
119120
(swap! ds-clients assoc channel {:name nil :gravatar-url (calc-gravatar (get-in req [:session :cemerick.friend/identity :authentications nil :username])) :room nil}) ;; Add to ds-clients
120121
(swap! ds-agents assoc channel {}))
@@ -123,12 +124,13 @@
123124
gravatar-url (calc-gravatar (get-in req [:session :cemerick.friend/identity :authentications nil :username]))]
124125
(if (= ch-agent nil)
125126
(println "ERROR EVENTUALLY!!! no agents on, ignoring visitor!")
126-
(do (println "Visitor connected: " channel)
127+
(do (println "Visitor connected: " ch-visitor)
127128
(swap! ds-clients assoc ch-visitor {:name nil :gravatar-url gravatar-url :room nil}) ;; Add to ds-clients
128129
(ds-agents-add-visitor ch-agent ch-visitor)
129130
(ds-visitors-add ch-visitor ch-agent)
130-
(send! ch-agent (generate-string {:ch-visitor (str ch-visitor) :gravatar-url gravatar-url}) false)
131-
;; (send! ch-visitor (generate-string {:gravatar-url gravatar-url}) false)
131+
(println "sending this message over!!" (pr-str {:ch-visitor (str ch-visitor) :gravatar-url gravatar-url}))
132+
(send! ch-agent (pr-str {:ch-visitor (str ch-visitor) :gravatar-url gravatar-url}) false)
133+
;; (send! ch-visitor (pr-str {:gravatar-url gravatar-url}) false)
132134
))))
133135
;; RECEIVE
134136
(on-receive channel (fn [data]
@@ -147,7 +149,7 @@
147149
(let [client-filter-fn (fn [room] (fn [client] (if (= room (:room (val client))) true false)))
148150
clients-in-room (fn [room clients] (filter (client-filter-fn room) clients))
149151
channels-to-room (keys (clients-in-room room @comment-clients))
150-
message-string (generate-string message-map)]
152+
message-string (pr-str message-map)]
151153
(when (seq channels-to-room)
152154
(println "sending message: " message-map "to" (count channels-to-room) "channels")
153155
(doseq [channel channels-to-room]

0 commit comments

Comments
 (0)