forked from MTSR/mapserver
provide fallback polling if websocket connection could not be established
This commit is contained in:
parent
c2d2a183e4
commit
cce4eb267e
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
import { getStats } from './api.js';
|
||||||
|
|
||||||
class WebSocketChannel {
|
class WebSocketChannel {
|
||||||
constructor(){
|
constructor(){
|
||||||
this.wsUrl = window.location.protocol.replace("http", "ws") +
|
this.wsUrl = window.location.protocol.replace("http", "ws") +
|
||||||
@ -44,9 +46,26 @@ class WebSocketChannel {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function fallbackPolling(){
|
||||||
|
getStats()
|
||||||
|
.then(function(stats){
|
||||||
|
if (!stats){
|
||||||
|
// no stats (yet)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var listeners = self.listenerMap["minetest-info"];
|
||||||
|
if (listeners){
|
||||||
|
listeners.forEach(function(listener){
|
||||||
|
listener(stats);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
ws.onerror = function(){
|
ws.onerror = function(){
|
||||||
//reconnect after some time
|
//fallback to polling stats
|
||||||
setTimeout(self.connect.bind(self), 1000);
|
setInterval(fallbackPolling, 2000);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,3 +10,7 @@ export function getMapObjects(query){
|
|||||||
export function getConfig(){
|
export function getConfig(){
|
||||||
return m.request("api/config");
|
return m.request("api/config");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getStats(){
|
||||||
|
return m.request("api/stats");
|
||||||
|
}
|
||||||
|
@ -68,6 +68,8 @@ type Minetest struct {
|
|||||||
ctx *app.App
|
ctx *app.App
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var LastStats *MinetestInfo
|
||||||
|
|
||||||
func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||||
if req.Header.Get("Authorization") != this.ctx.Config.WebApi.SecretKey {
|
if req.Header.Get("Authorization") != this.ctx.Config.WebApi.SecretKey {
|
||||||
resp.WriteHeader(403)
|
resp.WriteHeader(403)
|
||||||
@ -91,6 +93,7 @@ func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
|||||||
mintestPlayers.Set(float64(len(data.Players)))
|
mintestPlayers.Set(float64(len(data.Players)))
|
||||||
mintestMaxLag.Set(data.MaxLag)
|
mintestMaxLag.Set(data.MaxLag)
|
||||||
|
|
||||||
|
LastStats = data
|
||||||
this.ctx.WebEventbus.Emit("minetest-info", data)
|
this.ctx.WebEventbus.Emit("minetest-info", data)
|
||||||
|
|
||||||
json.NewEncoder(resp).Encode("stub")
|
json.NewEncoder(resp).Encode("stub")
|
||||||
|
@ -25,6 +25,7 @@ func Serve(ctx *app.App) {
|
|||||||
tiles.Init()
|
tiles.Init()
|
||||||
mux.Handle("/api/tile/", tiles)
|
mux.Handle("/api/tile/", tiles)
|
||||||
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
|
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
|
||||||
|
mux.Handle("/api/stats", &StatsHandler{ctx: ctx})
|
||||||
mux.Handle("/api/media/", &MediaHandler{ctx: ctx})
|
mux.Handle("/api/media/", &MediaHandler{ctx: ctx})
|
||||||
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
|
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
|
||||||
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
|
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
|
||||||
|
16
web/stats.go
Normal file
16
web/stats.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"mapserver/app"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StatsHandler struct {
|
||||||
|
ctx *app.App
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *StatsHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||||
|
resp.Header().Add("content-type", "application/json")
|
||||||
|
json.NewEncoder(resp).Encode(LastStats)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user