From 12ca4522300c0d0c112ca2d2d24ec8d9ab5c27a1 Mon Sep 17 00:00:00 2001 From: Zack Date: Tue, 13 Dec 2016 06:58:42 -0800 Subject: [PATCH] Create keystore.go --- keystore/keystore.go | 86 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 keystore/keystore.go diff --git a/keystore/keystore.go b/keystore/keystore.go new file mode 100644 index 0000000..ff4094e --- /dev/null +++ b/keystore/keystore.go @@ -0,0 +1,86 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + + "github.com/gorilla/websocket" +) + +func get(key string) interface{} { + c, _, err := websocket.DefaultDialer.Dial("wss://cowyo.com/ws", nil) + if err != nil { + log.Fatal("dial:", err) + } + defer c.Close() + done := make(chan struct{}) + var value interface{} + go func() { + defer c.Close() + defer close(done) + for { + _, message, err := c.ReadMessage() + if err != nil { + // log.Println("read:", err) + return + } + var m struct { + TextData string + } + json.Unmarshal([]byte(message), &m) + json.Unmarshal([]byte(m.TextData), &value) + done <- struct{}{} + } + }() + // ask for something + c.WriteMessage(websocket.TextMessage, []byte(`{"Title":"`+key+`", "UpdateClient":true}`)) + <-done + return value +} + +func set(key string, message interface{}) interface{} { + c, _, err := websocket.DefaultDialer.Dial("wss://cowyo.com/ws", nil) + if err != nil { + log.Fatal("dial:", err) + } + defer c.Close() + done := make(chan struct{}) + var value interface{} + go func() { + defer c.Close() + defer close(done) + for { + _, message, err := c.ReadMessage() + if err != nil { + // log.Println("read:", err) + return + } + var m struct { + TextData string + } + json.Unmarshal([]byte(message), &m) + json.Unmarshal([]byte(m.TextData), &value) + done <- struct{}{} + } + }() + // ask for something + bJson, _ := json.Marshal(message) + aa, _ := json.Marshal(map[string]interface{}{ + "Title": key, + "UpdateServer": true, + "TextData": string(bJson), + }) + c.WriteMessage(websocket.TextMessage, aa) + <-done + return value +} + +func main() { + fmt.Println(get("data")) + fmt.Println(get("data")) + m := make(map[string]int) + m["some string"] = 29 + fmt.Println(set("data2", m)) + fmt.Println(get("data2")) +}