http-kit websocket解决符号

时间:2015-11-28 02:14:11

标签: clojure http-kit

问题:如果我通过json从javascript套接字客户端传递数据,当我在下面的代码中调用调用函数时,我得到一个空指针异常。当我从repl运行相同的代码时,我没有收到错误,并且我传递了相同的数据,即。

在repl中:

(def data (json/write-str {:controller "hql" :function "something"}))
(caller data)

这很好用

从javascript:当从处理程序调用函数调用时,这给了我空指针异常。

<script>
var msg = {'controller': 'hql','function':'something'};
socket = new WebSocket('ws://192.168.0.7:9090');
socket.onopen = function() {
  socket.send(JSON.stringify(msg));
}
socket.onmessage = function(s) {
 console.log(s);
 socket.close();
}
socket.onclose = function() {
  console.log('Connection Closed');
}
</script>

(ns hqlserver.core
 (:use [org.httpkit.dbcp :only [use-database! close-database! insert-record update-values query delete-rows]]
       [org.httpkit.server])
 (:require [clojure.data.json :as json]))

(defn hql [data]
 (use-database! "jdbc:mysql://localhost/xxxx" "user" "xxxxx")
 (let [rows (query ("select username,firstname,lastname from users order by lastname,firstname")]
 (close-database!)
 (json/write-str rows)))

(defn get-controller [data]
 (if data
   (let [data (json/read-str data :key-fn keyword)]
     (data :controller))))

(defn caller [data]
  ((ns-resolve *ns* (symbol (get-controller data))) data))

(defn handler [request]
 (with-channel request channel
   (on-close channel (fn [status] (println "channel closed: " status)))
   (on-receive channel (fn [data] (send! channel (caller data))))))

 (defn -main [& args]
   (run-server handler {:port 9090}))

1 个答案:

答案 0 :(得分:0)

不知道这是否是最佳方式,但这是解决方案: 感谢noisesmith的帮助和提示......

(ns hqlserver.core
(:use [org.httpkit.dbcp :only [use-database! close-database! insert-   record update-values query delete-rows]]
    [org.httpkit.server]
    [hqlserver.s300.login :only [login]])
(:require [clojure.data.json :as json]
        [hqlserver.s300.site :refer [site-index site-update]]
        [clojure.string :as str]))

(defn get-site [data]
 (if data
   (let [data (json/read-str data :key-fn keyword)]
     (-> data :site))))

(defn get-controller [data]
  (if data
    (let [data (json/read-str data :key-fn keyword)]
      (-> data :controller))))

(defn get-function [data]
  (if data
    (let [data (json/read-str data :key-fn keyword)]
      (-> data :function))))

(defn caller [data]
 (let [site (get-site data)]
   (let [controller (get-controller data)]
     (let [function (get-function data)]
       (cond
         (and (= site "s300")(= controller "login")(= function "login")) (login data)
         (and (= site "s300")(= controller "site")(= function "site-index")) (site-index data)
         (and (= site "s300")(= controller "site")(= function "site-update")) (site-update data)
         :else (json/write-str {:error "No records found!"}))))))

(defn async-handler [ring-request]
  (with-channel ring-request channel
    (if (websocket? channel)
      (on-receive channel (fn [data]
                        (send! channel (caller data))))
      (send! channel {:status 200
                  :headers {"Content-Type" "text/plain"}
                  :body    "Long polling?"}))))

(defn -main [& args]
 (run-server async-handler {:port 9090}));