idk, kinda websockety, really difficult to test atm
This commit is contained in:
parent
afe2941983
commit
ef918a4ffa
2
http.go
2
http.go
|
@ -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.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
|
return router
|
||||||
}
|
}
|
||||||
|
|
40
websocket.go
40
websocket.go
|
@ -1,52 +1,28 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/googollee/go-engine.io"
|
"fmt"
|
||||||
"github.com/googollee/go-engine.io/transport"
|
|
||||||
"github.com/googollee/go-engine.io/transport/websocket"
|
|
||||||
"github.com/googollee/go-socket.io"
|
"github.com/googollee/go-socket.io"
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configures the websocket connection and attaches it to the Router struct.
|
// Configures the websocket connection and attaches it to the Router struct.
|
||||||
func (rt *Router) ConfigureWebsocket() (*socketio.Server, error) {
|
func (rt *Router) ConfigureWebsocket() (*socketio.Server, error) {
|
||||||
s, err := socketio.NewServer(&engineio.Options{
|
s, err := socketio.NewServer(nil)
|
||||||
Transports: []transport.Transport{
|
|
||||||
websocket.Default,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.OnConnect("/", func(s socketio.Conn) error {
|
||||||
|
s.SetContext("")
|
||||||
|
fmt.Println("connected:", s.ID())
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
s.OnError("/", func(e error) {
|
s.OnError("/", func(e error) {
|
||||||
zap.S().Error(e)
|
zap.S().Error(e)
|
||||||
})
|
})
|
||||||
|
|
||||||
return s, nil
|
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)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user