1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00
cowyo/websockets.go
Zack Scholl 26eab17489 Removed case senstivity from savin
Former-commit-id: 1591534e5b64859e8a4fb4811644ef47ce8e172f [formerly 451962c969cfd77bf8cb4f8ec49d2604675a5246] [formerly b53ba2a5e4fd889e0838d1509143099546589614 [formerly 3d64629afaa66d105bfa01bedd7185982c66f91e [formerly fe69bdee4f]]]
Former-commit-id: 6063556d33984e3d9d448681c6b379b879a6d6c5 [formerly 7d66254c32a990820a5fe68622d78fd78ff580da]
Former-commit-id: b91d89eda14dde6a7f802da2fa25acf9deb1ba86
Former-commit-id: 371b64e731
2016-02-08 01:37:46 +00:00

65 lines
1.0 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/websocket"
)
var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func wshandler(w http.ResponseWriter, r *http.Request) {
conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Failed to set websocket upgrade: %+v", err)
return
}
for {
t, msg, err := conn.ReadMessage()
if err != nil {
break
}
type Message struct {
TextData string
Title string
UpdateServer bool
UpdateClient bool
}
var m Message
err = json.Unmarshal(msg, &m)
if err != nil {
panic(err)
}
var p CowyoData
err = p.load(strings.ToLower(m.Title))
if err != nil {
panic(err)
}
if m.UpdateServer {
err := p.save(m.TextData)
if err != nil {
panic(err)
}
}
if m.UpdateClient {
m.UpdateClient = len(m.TextData) != len(p.CurrentText)
m.TextData = p.CurrentText
}
newMsg, err := json.Marshal(m)
if err != nil {
panic(err)
}
conn.WriteMessage(t, newMsg)
}
}