1
0
forked from MTSR/mapserver
mapserver/web/ws.go

104 lines
1.7 KiB
Go
Raw Permalink 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-03-27 17:12:21 +03:00
"github.com/sirupsen/logrus"
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
2019-02-08 18:02:24 +03:00
clients int
2019-01-22 18:36:50 +03:00
}
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,
CheckOrigin: func(r *http.Request) bool {
return true
},
2019-01-21 17:44:53 +03:00
}
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) {
2019-03-27 17:12:21 +03:00
conn, err := upgrader.Upgrade(resp, req, nil)
if err != nil {
fields := logrus.Fields{
"err": err,
}
logrus.WithFields(fields).Error("ws-upgrade")
}
2019-01-22 18:36:50 +03:00
id := rand.Intn(64000)
ch := make(chan []byte)
t.mutex.Lock()
t.channels[id] = ch
2019-02-08 16:25:07 +03:00
t.clients++
wsClients.Set(float64(t.clients))
2019-01-22 18:36:50 +03:00
t.mutex.Unlock()
for {
data := <-ch
2019-03-22 10:18:09 +03:00
if data == nil {
//how the hell got a nil reference in here..?!
//related issue: #18
continue
}
2019-02-07 10:08:54 +03:00
err := conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
break
}
2019-01-22 18:36:50 +03:00
}
2019-01-21 17:44:53 +03:00
2019-02-07 10:08:54 +03:00
t.mutex.Lock()
2019-02-08 16:25:07 +03:00
t.clients--
wsClients.Set(float64(t.clients))
2019-02-07 10:08:54 +03:00
delete(t.channels, id)
close(ch)
t.mutex.Unlock()
2019-01-21 17:44:53 +03:00
}