jarohen / chord

A library designed to bridge the gap between the triad of CLJ/CLJS, web-sockets and core.async.
439 stars 40 forks source link

Problem with routing compojure #51

Closed sivakumargsk closed 7 years ago

sivakumargsk commented 7 years ago

I am trying to create a chat-room websocket services with the help of example code in this https://github.com/jarohen/chord , the problem i don't able to create a different channel with respect to id. and i don't have good knowledge on clojure.core.async here is my code

(ns cljs-lein-project.mychat
  (:require [compojure.core :refer [GET defroutes]]
            [compojure.handler :refer [site]]
            [chord.http-kit :refer [wrap-websocket-handler]]
            [org.httpkit.server :refer [run-server]]
            [clojure.core.async :as a])
  (:gen-class))

(defn ws-handler
  [id {:keys [ws-channel] :as req} {:keys [chat-ch chat-mult]}]
  (let [tapped-ch (a/chan)
        room-id id]
    (a/tap chat-mult tapped-ch)
    (a/go
      (a/>! chat-ch {:type :user-joined :room-id room-id})
      (loop []
        (a/alt!
          tapped-ch ([message]
                     (if message
                       (do
                         (a/>! ws-channel message)
                         (recur))
                       (a/close! ws-channel)))

          ws-channel ([ws-message]
                      (if ws-message
                        (do
                          (println ws-message)
                          (a/>! chat-ch
                                {:type :message
                                 :message (:message ws-message)
                                 :room-id room-id})
                          (recur))
                        (do
                          (a/untap chat-mult tapped-ch)
                          (a/>! chat-ch
                                {:type :user-left
                                 :room-id room-id})))))))))

(def with-chat-chs1
  (let [chat-ch (a/chan)
        chat-mult (a/mult chat-ch)]
    {:chat-ch chat-ch
     :chat-mult chat-mult}))

(def with-chat-chs2
  (let [chat-ch (a/chan)
        chat-mult (a/mult chat-ch)]
    {:chat-ch chat-ch
     :chat-mult chat-mult}))

(defroutes chat-routes
  (GET "/user-chat/:id" [id]
       (-> #(ws-handler id  % with-chat-chs1)
           (wrap-websocket-handler {:format :transit-json})))
  (GET "/admin-chat/:id" [id]
       (-> #(ws-handler id % with-chat-chs2)
           (wrap-websocket-handler {:format :transit-json}))))

(defn run-chat []
  (run-server
   (site #'chat-routes)
   {:port 3008}))

If i connect to ws://localhost:3008/user-chat/235 and ws://localhost:3008/user-chat/236 Here the both routes are different right but massages are visible in both uri so please give me any idea to overtake this issue. I think the problem with my def's