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 { type TileCoords struct {
X, Y int X int `json:"x"`
Zoom int Y int `json:"y"`
LayerId int Zoom int `json:"zoom"`
LayerId int `json:"layerid"`
} }
type TileQuadrants struct { type TileQuadrants struct {

View File

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

View File

@ -22,7 +22,7 @@ type TileRenderer struct {
layers []layer.Layer layers []layer.Layer
tdb mapobjectdb.DBAccessor tdb mapobjectdb.DBAccessor
dba db.DBAccessor dba db.DBAccessor
listeners []TileListener listeners []TileListener
} }
type TileListener interface { type TileListener interface {
@ -252,11 +252,9 @@ func (tr *TileRenderer) RenderImage(tc *coords.TileCoords, recursionDepth int) (
} }
log.WithFields(fields).Debug("Cross stitch") log.WithFields(fields).Debug("Cross stitch")
for _, listener := range tr.listeners { for _, listener := range tr.listeners {
listener.OnRenderedTile(tc) listener.OnRenderedTile(tc)
} }
return img, buf.Bytes(), nil 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("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
mux.Handle("/api/tile/", &Tiles{ctx: ctx}) mux.Handle("/api/tile/", &Tiles{ctx: ctx})
mux.Handle("/api/config", &ConfigHandler{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 { if ctx.Config.WebApi.EnableMapblock {
//websocket listener
ctx.BlockAccessor.AddListener(ws)
//mapbloc endpoint
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx}) mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
} }

View File

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