mapserver/server/web/ws.go

81 lines
1.2 KiB
Go
Raw Normal View History

2019-01-21 17:44:53 +03:00
package web
import (
2019-01-23 15:13:32 +03:00
"bytes"
2019-01-22 18:36:50 +03:00
"encoding/json"
2019-01-21 17:44:53 +03:00
"mapserver/app"
2019-01-22 18:36:50 +03:00
"math/rand"
2019-01-21 17:44:53 +03:00
"net/http"
2019-01-22 18:36:50 +03:00
"sync"
2019-01-22 22:19:04 +03:00
"github.com/gorilla/websocket"
2019-01-21 17:44:53 +03:00
)
type WS struct {
2019-01-22 18:36:50 +03:00
ctx *app.App
channels map[int]chan []byte
mutex *sync.RWMutex
}
func NewWS(ctx *app.App) *WS {
ws := WS{}
ws.mutex = &sync.RWMutex{}
ws.channels = make(map[int]chan []byte)
return &ws
}
2019-01-21 17:44:53 +03:00
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
2019-01-28 15:31:48 +03:00
func (t *WS) OnEvent(eventtype string, o interface{}) {
2019-01-23 15:06:54 +03:00
data, err := json.Marshal(o)
2019-01-22 18:36:50 +03:00
if err != nil {
panic(err)
}
2019-01-23 15:06:54 +03:00
buf := new(bytes.Buffer)
buf.Write([]byte("{\"type\":\""))
buf.Write([]byte(eventtype))
buf.Write([]byte("\",\"data\":"))
buf.Write(data)
buf.Write([]byte("}"))
2019-01-22 18:36:50 +03:00
t.mutex.RLock()
defer t.mutex.RUnlock()
for _, c := range t.channels {
select {
2019-01-23 15:06:54 +03:00
case c <- buf.Bytes():
2019-01-22 18:36:50 +03:00
default:
2019-01-21 17:44:53 +03:00
}
2019-01-21 18:27:31 +03:00
}
2019-01-22 18:36:50 +03:00
}
func (t *WS) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
conn, _ := upgrader.Upgrade(resp, req, nil)
id := rand.Intn(64000)
ch := make(chan []byte)
t.mutex.Lock()
t.channels[id] = ch
t.mutex.Unlock()
defer func() {
t.mutex.Lock()
delete(t.channels, id)
close(ch)
t.mutex.Unlock()
}()
for {
data := <-ch
conn.WriteMessage(websocket.TextMessage, data)
}
2019-01-21 17:44:53 +03:00
}