126 lines
2.7 KiB
Go
126 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.timovolkmann.de/gyrogpsc/dispatcher"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/tidwall/pretty"
|
|
"html/template"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
//CONN_HOST = "localhost"
|
|
CONN_PORT = ":3010"
|
|
CONN_TYPE = "tcp"
|
|
)
|
|
|
|
var upgrader = websocket.Upgrader{} // use default options
|
|
|
|
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()
|
|
go func() {
|
|
for {
|
|
if _, _, err := c.NextReader(); err != nil {
|
|
c.Close()
|
|
break
|
|
}
|
|
}
|
|
}()
|
|
|
|
dispatcherId, channel := d.Subscribe()
|
|
defer d.Unsubscribe(dispatcherId)
|
|
for {
|
|
log.Println("")
|
|
//if err != nil {
|
|
// log.Println("read:", err)
|
|
// break
|
|
//}
|
|
cmsg := <-channel
|
|
err = c.WriteMessage(websocket.TextMessage, []byte(cmsg))
|
|
if err != nil {
|
|
log.Println("write:", err)
|
|
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func home(w http.ResponseWriter, r *http.Request) {
|
|
//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() {
|
|
d := dispatcher.New()
|
|
go tcp(d)
|
|
http.HandleFunc("/echo", echo(d))
|
|
http.HandleFunc("/", home)
|
|
log.Fatal(http.ListenAndServe(":3011", nil))
|
|
}
|
|
|
|
func tcp(d *dispatcher.Dispatcher) {
|
|
fmt.Println("Hello TCP")
|
|
listener, err := net.Listen("tcp", CONN_PORT)
|
|
if err != nil {
|
|
fmt.Println("Error listening:", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
// Close the listener when the application closes.
|
|
defer listener.Close()
|
|
|
|
for {
|
|
// Listen for an incoming connection.
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
fmt.Println("Error accepting: ", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
// Handle connections in a new goroutine.
|
|
go handleRequest(conn, d)
|
|
}
|
|
}
|
|
|
|
// Handles incoming requests.
|
|
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 {
|
|
buf := make([]byte, 2048)
|
|
// Read the incoming connection into the buffer.
|
|
_, err := conn.Read(buf)
|
|
if err != nil {
|
|
fmt.Println("Error reading:", err.Error())
|
|
break
|
|
}
|
|
json := buf
|
|
json = pretty.Pretty(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.
|
|
}
|
|
}
|