mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
239341ee87
Former-commit-id: bc371d744dca228255db92d05fa764fbaa157eb2 [formerly ed45c432823c880548e99b37f400a103dc4f91e8] [formerly dcb0bd1f93260429a86ed8abf1ef8f835b0680e7 [formerly 12ca452230
]]
Former-commit-id: 00e79655127e4308e6f7665486f95a3c417e0025 [formerly 40bfa8fd615cf56c8890120d1f5bc92b40d2e6c9]
Former-commit-id: c47dfb60b3cffe45224be010e30328eb2330277b
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
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"))
|
|
}
|