Commit 641ffac

bryfry <bryon@fryer.io>
2017-03-31 21:47:26
add place.go
Changed files (1)
place.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"github.com/gorilla/websocket"
+	"time"
+)
+
+func Connect() (c *websocket.Conn, err error) {
+	dialer := &websocket.Dialer{}
+	dialer.EnableCompression = false
+	place_url := "YOUR WSS URL HERE"
+	c, _, err = dialer.Dial(place_url, nil)
+	if err != nil {
+		return c, err
+	}
+	return c, err
+}
+
+type Place struct {
+	Type         string `json:"type"`
+	PlacePayload `json:"payload"`
+}
+type PlacePayload struct {
+	Y      int       `json:"y"`
+	X      int       `json:"x"`
+	Color  int       `json:"color"`
+	Author string    `json:"author"`
+	Date   time.Time `json:"date"`
+}
+
+func main() {
+	c, _ := Connect()
+	for {
+		_, packet, err := c.ReadMessage()
+		if err != nil {
+			fmt.Println(err)
+		} else {
+			var p Place
+			err := json.Unmarshal(packet, &p)
+			if err != nil {
+				panic(err)
+			} else {
+				p.Date = time.Now()
+				pStr, _ := json.Marshal(p)
+				fmt.Println(string(pStr))
+			}
+		}
+	}
+}