Skip to content

Commit

Permalink
Full circle working sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
INGSOL committed Sep 4, 2018
1 parent 903e75c commit 5c343ed
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@

[[constraint]]
name = "github.com/gobuffalo/packr"
version = "1.13.1"
version = "1.13.1"

[[constraint]]
name = "github.com/gorilla/websocket"
version = "1.4.0"

16 changes: 16 additions & 0 deletions client/src/cljs/cse_client/core.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns cse-client.core
(:require [kee-frame.core :as k]
[kee-frame.websocket :as websocket]
[ajax.core :as ajax]
[re-interval.core :as re-interval]
[re-frame.core :as rf]))
Expand All @@ -16,6 +17,21 @@
:start [:poll/start]
:stop [:poll/stop]})

(k/reg-controller :websocket-controller
{:params #(when (-> % :handler (= :index)) true)
:start [:start-websockets]
:stop [:stop-websockets]})

(k/reg-event-fx :start-websockets
(fn [_ _]
{::websocket/open {:path "/ws"
:dispatch ::socket-message-received
:format :json}}))

(k/reg-event-db ::socket-message-received
(fn [db event]
db))

(k/reg-chain :poll/tick
(fn [_ _]
{:http-xhrio {:method :get
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Server(command chan string) {
command <- "pause"
json.NewEncoder(w).Encode(Simulator{ID: "id-1", Name: "Clock", Status: "-"})
})
router.HandleFunc("/ws", WebsocketHandler)

router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(box)))

Expand Down
32 changes: 32 additions & 0 deletions websockets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"github.com/gorilla/websocket"
"net/http"
"log"
)

var upgrader = websocket.Upgrader{}

func WebsocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
conn.WriteMessage(websocket.TextMessage, []byte("{\"Hello\": \"Mordi\"}"))
if err != nil {
log.Print("upgrade:", err)
return
}
defer conn.Close()
for {
mt, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = conn.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}

0 comments on commit 5c343ed

Please sign in to comment.