From 718e4c0f516ff6956fef276de591aa6ff234ed8a Mon Sep 17 00:00:00 2001 From: Timo Volkmann Date: Sat, 7 Nov 2020 17:58:47 +0100 Subject: [PATCH] 3d orientation visualization html/css --- dispatcher/dispatcher.go | 15 +++- index.html | 174 ++++++++++++++++++++++++++++++++++++++ server.go | 177 ++++++++------------------------------- 3 files changed, 220 insertions(+), 146 deletions(-) create mode 100644 index.html diff --git a/dispatcher/dispatcher.go b/dispatcher/dispatcher.go index bcfcba5..05bcce4 100644 --- a/dispatcher/dispatcher.go +++ b/dispatcher/dispatcher.go @@ -1,26 +1,32 @@ package dispatcher -import "errors" +import ( + "errors" + "fmt" +) type Dispatcher struct { listeners map[int16]chan string counter int16 } -func New() Dispatcher { - return Dispatcher{ - listeners: map[int16]chan string{}, +func New() *Dispatcher { + fmt.Println("new dispatcher") + return &Dispatcher{ + listeners: make(map[int16]chan string), counter: 0, } } func (d *Dispatcher) Publish(message string) { + fmt.Println("publish to listeners", len(d.listeners)) for _, ch := range d.listeners { ch <- message } } func (d *Dispatcher) Subscribe() (id int16, receiver <-chan string) { + fmt.Println("subscribe") key := d.counter d.counter++ rec := make(chan string) @@ -29,6 +35,7 @@ func (d *Dispatcher) Subscribe() (id int16, receiver <-chan string) { } func (d *Dispatcher) Unsubscribe(id int16) error { + fmt.Println("unsubscribe") receiver, ok := d.listeners[id] if !ok { return errors.New("no subscription with id") diff --git a/index.html b/index.html new file mode 100644 index 0000000..84835f2 --- /dev/null +++ b/index.html @@ -0,0 +1,174 @@ + + + + + + + + + + + + + +
+

Click "Open" to create a connection to the server, + "Send" to send a message to the server and "Close" to close the connection. + You can change the message and send multiple times. +

+
+ + +

+ + +

+
+
+
+
front
+
back
+
right
+
left
+
top
+
bottom
+
+
+ + +
+
+
+ + \ No newline at end of file diff --git a/server.go b/server.go index 3b3f56f..5744ee5 100644 --- a/server.go +++ b/server.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "git.timovolkmann.de/gyrogpsc/dispatcher" "github.com/gorilla/websocket" "github.com/tidwall/pretty" "html/template" @@ -19,26 +20,38 @@ const ( var upgrader = websocket.Upgrader{} // use default options -func echo(channel <-chan string) func(w http.ResponseWriter, r *http.Request) { +func echo(d *dispatcher.Dispatcher) func(w http.ResponseWriter, r *http.Request) { + fmt.Println("echo") return func(w http.ResponseWriter, r *http.Request) { + fmt.Println("upgrading to ws") c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } - defer c.Close() + //defer c.Close() + go func() { + for { + if _, _, err := c.NextReader(); err != nil { + c.Close() + break + } + } + }() + dispatcherId, channel := d.Subscribe() + defer d.Unsubscribe(dispatcherId) for { - //mt, message, err := c.ReadMessage() + log.Println("") //if err != nil { // log.Println("read:", err) // break //} - //log.Printf("recv: %s", message) cmsg := <-channel err = c.WriteMessage(websocket.TextMessage, []byte(cmsg)) if err != nil { log.Println("write:", err) + break } } @@ -46,23 +59,26 @@ func echo(channel <-chan string) func(w http.ResponseWriter, r *http.Request) { } func home(w http.ResponseWriter, r *http.Request) { - homeTemplate.Execute(w, "ws://"+r.Host+"/echo") + //var homeTemplate = template.Must(template.New("").ParseFiles("index.html")) + tpl, err := template.ParseFiles("index.html") + if err != nil { + log.Fatalln(err) + } + err = tpl.Execute(w, "ws://"+r.Host+"/echo") + if err != nil { + log.Fatalln(err) + } } func main() { - channel := make(chan string) - switch CONN_TYPE { - case "tcp": - go tcp(channel) - case "udp": - go udp() - } - http.HandleFunc("/echo", echo(channel)) + d := dispatcher.New() + go tcp(d) + http.HandleFunc("/echo", echo(d)) http.HandleFunc("/", home) log.Fatal(http.ListenAndServe(":3011", nil)) } -func tcp(messageChannel chan<- string) { +func tcp(d *dispatcher.Dispatcher) { fmt.Println("Hello TCP") listener, err := net.Listen("tcp", CONN_PORT) if err != nil { @@ -72,7 +88,6 @@ func tcp(messageChannel chan<- string) { // Close the listener when the application closes. defer listener.Close() - //messageChannel := make(chan string) for { // Listen for an incoming connection. conn, err := listener.Accept() @@ -81,12 +96,13 @@ func tcp(messageChannel chan<- string) { os.Exit(1) } // Handle connections in a new goroutine. - go handleRequest(conn, messageChannel) + go handleRequest(conn, d) } } // Handles incoming requests. -func handleRequest(conn net.Conn, messageChannel chan<- string) { +func handleRequest(conn net.Conn, d *dispatcher.Dispatcher) { + fmt.Println("handling sensordata via tcp") defer conn.Close() // Make a buffer to hold incoming data. for { @@ -99,134 +115,11 @@ func handleRequest(conn net.Conn, messageChannel chan<- string) { } json := buf json = pretty.Pretty(json) - fmt.Println(string(json)) - messageChannel <- string(json) + //fmt.Println(string(json)) + d.Publish(string(json)) // Send a response back to person contacting us. // conn.Write([]byte("You stepped into my honey pot. I'll find you! ")) conn.Write([]byte("success")) // Close the connection when you're done with it. } } - -func udp() { - fmt.Println("Hello!") - - port := CONN_PORT - protocol := "udp" - - //Build the address - udpAddr, err := net.ResolveUDPAddr(protocol, port) - if err != nil { - fmt.Println("Wrong Address") - return - } - - //Output - fmt.Println("Reading " + protocol + " from " + udpAddr.String()) - - //Create the connection - udpConn, err := net.ListenUDP(protocol, udpAddr) - if err != nil { - fmt.Println(err) - } - - //Keep calling this function - for { - handlePacket(udpConn) - } -} - -func handlePacket(conn *net.UDPConn) { - - var buf = make([]byte, 2048) - n, err := conn.Read(buf) - fmt.Println("Buffersize:", n) - if err != nil { - fmt.Println("Error Reading") - return - } else { - fmt.Println(string(buf)) - //fmt.Println(hex.EncodeToString(buf[0:n])) - //fmt.Println("Package Done") - } - -} - -var homeTemplate = template.Must(template.New("").Parse(` - - - - - - - - -
-

Click "Open" to create a connection to the server, -"Send" to send a message to the server and "Close" to close the connection. -You can change the message and send multiple times. -

-

- - -

- -

-
-
-
- - -`))