diff options
Diffstat (limited to 'src/cljs/gnunet_web')
-rw-r--r-- | src/cljs/gnunet_web/service.cljs | 6 | ||||
-rw-r--r-- | src/cljs/gnunet_web/webrtc.cljs | 68 |
2 files changed, 72 insertions, 2 deletions
diff --git a/src/cljs/gnunet_web/service.cljs b/src/cljs/gnunet_web/service.cljs index e7e8254..0eea5a2 100644 --- a/src/cljs/gnunet_web/service.cljs +++ b/src/cljs/gnunet_web/service.cljs @@ -1,5 +1,5 @@ ;; service.cljs - service manager for gnunet-web website -;; Copyright (C) 2013-2015 David Barksdale <amatus@amat.us> +;; Copyright (C) 2013-2018 David Barksdale <amatus@amat.us> ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -15,7 +15,8 @@ ;; along with this program. If not, see <http://www.gnu.org/licenses/>. (ns gnunet-web.service - (:require [cognitect.transit :as t])) + (:require [cognitect.transit :as t] + [gnunet-web.webrtc :refer [peer-connect]])) (def private-key ;; XXX This has no synchronization! @@ -68,6 +69,7 @@ "client_connect" (client-connect (aget data "service_name") (aget data "client_name") (aget data "message_port")) + "peer_connect" (peer-connect (aget data "message_port") (aget data "offer")) (.warn js/console worker-name data)))) (catch :default e (js/console.error "REKT" e)))) diff --git a/src/cljs/gnunet_web/webrtc.cljs b/src/cljs/gnunet_web/webrtc.cljs new file mode 100644 index 0000000..d43292e --- /dev/null +++ b/src/cljs/gnunet_web/webrtc.cljs @@ -0,0 +1,68 @@ +;; webrtc.cljs - WebRTC functions for gnunet-web +;; Copyright (C) 2018 David Barksdale <amatus@amat.us> +;; +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <http://www.gnu.org/licenses/>. + +(ns gnunet-web.webrtc) + +(def rtc-config + (clj->js + {:ice-servers [{:url "stun:stun.l.google.com:19302"}]})) + +(def data-channel-config + (clj->js + {:ordered false + :maxRetransmits 0 + :negotiated true + :id 1})) + +(defn peer-connect + [message-port offer] + (.debug js/console "peer-connect called with offer:" offer) + (let [connection (js/RTCPeerConnection. rtc-config) + channel (.createDataChannel connection + "data" + data-channel-config)] + (set! (.-onopen channel) + nil) + (set! (.-onmessage channel) + nil) + (if (empty? offer) + (.then (.createOffer connection) + (fn [offer] + (.debug js/console "created offer:" offer) + (.setLocalDescription connection offer) + (.postMessage message-port + (js-obj "type" "offer" + "sdp" (.-sdp offer))))) + (.then (.setRemoteDescription connection + (js-obj "type" "offer" + "sdp" offer)) + (fn [] + (.then (.createAnswer connection) + (fn [answer] + (.debug js/console "created answer:" answer) + (.setLocalDescription connection answer) + (.postMessage message-port + (js-obj "type" "answer" + "sdp" (.-sdp answer)))))))) + (set! (.-onmessage message-port) + (fn [e] + (.debug js/console "got webrtc message-channel message" e) + (let [data (.-data e)] + (condp = (.-type data) + "answer" (.then (.setRemoteDescription connection + (js-obj "type" "answer" + "sdp" (.-sdp data))) + (fn [])))))))) |