2019-01-21 17:44:53 +03:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2019-01-22 18:36:50 +03:00
|
|
|
"encoding/json"
|
2019-01-21 18:27:31 +03:00
|
|
|
"fmt"
|
2019-01-21 17:44:53 +03:00
|
|
|
"mapserver/app"
|
2019-01-22 18:36:50 +03:00
|
|
|
"mapserver/coords"
|
|
|
|
"mapserver/mapblockparser"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParsedMapBlockEvent struct {
|
|
|
|
Eventtype string `json:"type"`
|
|
|
|
Block *mapblockparser.MapBlock `json:"block"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RenderedTileEvent struct {
|
|
|
|
Eventtype string `json:"type"`
|
|
|
|
Tc *coords.TileCoords `json:"tilepos"`
|
2019-01-21 17:44:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:36:50 +03:00
|
|
|
func (t *WS) OnParsedMapBlock(block *mapblockparser.MapBlock) {
|
|
|
|
e := &ParsedMapBlockEvent{"parsed-mapblock", block}
|
|
|
|
data, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-21 17:44:53 +03:00
|
|
|
|
2019-01-22 18:36:50 +03:00
|
|
|
t.mutex.RLock()
|
|
|
|
defer t.mutex.RUnlock()
|
|
|
|
|
|
|
|
for _, c := range t.channels {
|
|
|
|
select {
|
|
|
|
case c <- data:
|
|
|
|
default:
|
2019-01-21 18:27:31 +03:00
|
|
|
}
|
2019-01-22 18:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 18:27:31 +03:00
|
|
|
|
2019-01-22 18:36:50 +03:00
|
|
|
func (t *WS) OnRenderedTile(tc *coords.TileCoords) {
|
2019-01-21 18:27:31 +03:00
|
|
|
|
2019-01-22 18:36:50 +03:00
|
|
|
e := &RenderedTileEvent{"rendered-tile", tc}
|
|
|
|
data, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mutex.RLock()
|
|
|
|
defer t.mutex.RUnlock()
|
|
|
|
|
|
|
|
for _, c := range t.channels {
|
|
|
|
select {
|
|
|
|
case c <- data:
|
|
|
|
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()
|
|
|
|
}()
|
|
|
|
|
|
|
|
fmt.Print("Socket opened: ")
|
|
|
|
fmt.Println(id)
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
data := <-ch
|
|
|
|
conn.WriteMessage(websocket.TextMessage, data)
|
|
|
|
}
|
2019-01-21 17:44:53 +03:00
|
|
|
|
|
|
|
}
|