我编写了一个非常简单的广播/回波服务器,它使用了带有clojure和aleph的web套接字。
我花了很多时间查看阿雷夫和薄片的资料来源,以获得一个体面的,基本的了解这里发生了什么。
我想做什么
因此,这可以处理数据(这是伟大的)和格式的响应(这是伟大的)。我怎样才能让它只向有关方面发出回应?
到目前为止我拥有的
(defn do-something
[arg]
(str "pickles" "are" "nice" arg))
(defn ws-handler [ch request]
(siphon (map* #(do-something %) ch) broadcast-channel)
(siphon broadcast-channel ch))
(defn -main
"Start the http server"
[& args]
(start-http-server ws-handler {:port 8080 :websocket true}))示例请求
假设我在JSON中有这个请求:
{"room":32, "color":"red", "command":"do something..."}我想让它执行“做点什么”命令,则生成的输出将发送给最近的命令包括{"room":32、"color":"red"}的所有其他人。
我不知道怎么用这种方式来管理亚历夫的关系.有什么帮助吗?
发布于 2013-05-15 17:46:45
如果你想让谁接收到更多的消息,你需要比“广播频道”更细的东西。Lamina提供了一个(named-channel ...)函数,允许您创建自己的通道命名空间。
这看起来应该是:
(defn broadcast [channel-name message]
(enqueue (named-channel channel-name nil) message))
(defn subscribe [channel-name client-channel]
(let [bridge-channel (channel)]
(siphon
(named-channel channel-name nil)
bridge-channel
client-channel)
#(close bridge-channel)))在本例中,subscribe方法确保客户端连接将接收来自该特定通道的所有消息,并返回将取消该订阅的函数。您需要有一些每个客户端的状态来保存那些取消回调,但我将把它作为一个练习留给读者。
发布于 2013-05-15 17:48:15
你可以尝试这样的方法:
(defn ws-handler [ch request]
(let [last-msg (atom nil)]
(siphon (map* #(do (swap! last-msg (constantly %)) [% (do-something %)]) ch) broadcast-channel)
(siphon (map* second (filter* (fn [[msg v]] (= @last-msg msg)) broadcast-channel)) ch)))https://stackoverflow.com/questions/16566265
复制相似问题