idk, kinda websockety, really difficult to test atm

This commit is contained in:
Dane Everitt 2019-04-07 16:58:56 -07:00
parent afe2941983
commit ef918a4ffa
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 10 additions and 34 deletions

View File

@ -266,7 +266,7 @@ func (rt *Router) ConfigureRouter() *httprouter.Router {
router.POST("/api/servers/:server/power", rt.AuthenticateToken("s:power", rt.AuthenticateServer(rt.routeServerPower)))
router.GET("/api/ws/:server", rt.AuthenticateServer(rt.routeWebsocket))
router.Handler("GET", "/socket.io/", rt.Socketio)
return router
}

View File

@ -1,52 +1,28 @@
package main
import (
"github.com/googollee/go-engine.io"
"github.com/googollee/go-engine.io/transport"
"github.com/googollee/go-engine.io/transport/websocket"
"fmt"
"github.com/googollee/go-socket.io"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
"net/http"
)
// Configures the websocket connection and attaches it to the Router struct.
func (rt *Router) ConfigureWebsocket() (*socketio.Server, error) {
s, err := socketio.NewServer(&engineio.Options{
Transports: []transport.Transport{
websocket.Default,
},
})
s, err := socketio.NewServer(nil)
if err != nil {
return nil, err
}
s.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
fmt.Println("connected:", s.ID())
return nil
})
s.OnError("/", func(e error) {
zap.S().Error(e)
})
return s, nil
}
func (rt *Router) routeWebsocket(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
server := rt.Servers.Get(ps.ByName("server"))
rt.Socketio.OnConnect("/", func (s socketio.Conn) error {
s.SetContext("")
zap.S().Infof("connected to socket for server: %s", server.Uuid)
s.Emit("initial status", server.State)
return nil
})
rt.Socketio.OnEvent("/", "status", func(s socketio.Conn, msg string) string {
s.Emit("reply", "thanks: " + msg)
return "recv: " + msg
})
rt.Socketio.ServeHTTP(w, r)
}
}