mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Create keystore.go
This commit is contained in:
parent
1432660990
commit
12ca452230
86
keystore/keystore.go
Normal file
86
keystore/keystore.go
Normal file
@ -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"))
|
||||
}
|
Loading…
Reference in New Issue
Block a user