1
0
forked from MTSR/mapserver
This commit is contained in:
NatureFreshMilk 2019-01-22 16:36:50 +01:00
parent 0c63823eeb
commit d5ea47ad87
5 changed files with 97 additions and 21 deletions

View File

@ -5,9 +5,10 @@ import (
)
type TileCoords struct {
X, Y int
Zoom int
LayerId int
X int `json:"x"`
Y int `json:"y"`
Zoom int `json:"zoom"`
LayerId int `json:"layerid"`
}
type TileQuadrants struct {

View File

@ -36,7 +36,7 @@ func Job(ctx *app.App) {
if len(mblist) == 0 && !hasMore {
fields = logrus.Fields{
"blocks": blockcount,
"tiles": tilecount,
"tiles": tilecount,
}
logrus.WithFields(fields).Info("Initial rendering complete")
rstate.InitialRun = false

View File

@ -22,7 +22,7 @@ type TileRenderer struct {
layers []layer.Layer
tdb mapobjectdb.DBAccessor
dba db.DBAccessor
listeners []TileListener
listeners []TileListener
}
type TileListener interface {
@ -252,11 +252,9 @@ func (tr *TileRenderer) RenderImage(tc *coords.TileCoords, recursionDepth int) (
}
log.WithFields(fields).Debug("Cross stitch")
for _, listener := range tr.listeners {
listener.OnRenderedTile(tc)
}
return img, buf.Bytes(), nil
}

View File

@ -20,9 +20,17 @@ func Serve(ctx *app.App) {
mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
mux.Handle("/api/tile/", &Tiles{ctx: ctx})
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
mux.Handle("/api/ws", &WS{ctx: ctx})
ws := NewWS(ctx)
mux.Handle("/api/ws", ws)
ctx.Tilerenderer.AddListener(ws)
if ctx.Config.WebApi.EnableMapblock {
//websocket listener
ctx.BlockAccessor.AddListener(ws)
//mapbloc endpoint
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
}

View File

@ -1,14 +1,39 @@
package web
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"mapserver/app"
"mapserver/coords"
"mapserver/mapblockparser"
"math/rand"
"net/http"
"sync"
)
type WS struct {
ctx *app.App
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"`
}
var upgrader = websocket.Upgrader{
@ -16,23 +41,67 @@ var upgrader = websocket.Upgrader{
WriteBufferSize: 1024,
}
func (t *WS) OnParsedMapBlock(block *mapblockparser.MapBlock) {
e := &ParsedMapBlockEvent{"parsed-mapblock", block}
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:
}
}
}
func (t *WS) OnRenderedTile(tc *coords.TileCoords) {
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:
}
}
}
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 {
// Read message from browser
msgType, msg, err := conn.ReadMessage()
if err != nil {
return
}
// Print the message to the console
fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))
// Write message back to browser
if err = conn.WriteMessage(msgType, msg); err != nil {
return
}
data := <-ch
conn.WriteMessage(websocket.TextMessage, data)
}
}