From d0e23bb114191e24bd9080984eb755de6b912bee Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Mon, 21 Jan 2019 15:44:53 +0100 Subject: [PATCH] websocket stub --- go.mod | 1 + go.sum | 2 ++ web/serve.go | 1 + web/ws.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 web/ws.go diff --git a/go.mod b/go.mod index 106e83c..f87a305 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/disintegration/imaging v1.5.0 github.com/google/go-cmp v0.2.0 // indirect github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 // indirect + github.com/gorilla/websocket v1.4.0 github.com/mattn/go-sqlite3 v1.10.0 github.com/mjibson/esc v0.1.0 // indirect github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect diff --git a/go.sum b/go.sum index 27ea740..fd497c5 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 h1:beB+Da4k9B1zmgag78k3k1Bx4L/fdWr5FwNa0f8RxmY= github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= diff --git a/web/serve.go b/web/serve.go index 2004af4..7f43864 100644 --- a/web/serve.go +++ b/web/serve.go @@ -20,6 +20,7 @@ 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}) if ctx.Config.WebApi.EnableMapblock { mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx}) diff --git a/web/ws.go b/web/ws.go new file mode 100644 index 0000000..c12aa96 --- /dev/null +++ b/web/ws.go @@ -0,0 +1,38 @@ +package web + +import ( + "mapserver/app" + "net/http" + "github.com/gorilla/websocket" + "fmt" +) + +type WS struct { + ctx *app.App +} + +var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, +} + +func (t *WS) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + conn, _ := upgrader.Upgrade(resp, req, nil) + + 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 + } + } + +}