2017-05-08 19:13:29 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
wsLib "github.com/genofire/golang-lib/websocket"
|
|
|
|
"github.com/google/uuid"
|
2017-05-08 19:13:29 +02:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/freifunkmanager/runtime"
|
|
|
|
)
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
type WebsocketServer struct {
|
|
|
|
nodes *runtime.Nodes
|
|
|
|
secret string
|
|
|
|
loggedIn map[uuid.UUID]bool
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
inputMSG chan *wsLib.Message
|
|
|
|
ws *wsLib.Server
|
|
|
|
handlers map[string]WebsocketHandlerFunc
|
|
|
|
}
|
2017-05-09 01:03:44 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func NewWebsocketServer(secret string, nodes *runtime.Nodes) *WebsocketServer {
|
|
|
|
ownWS := WebsocketServer{
|
|
|
|
nodes: nodes,
|
|
|
|
handlers: make(map[string]WebsocketHandlerFunc),
|
|
|
|
loggedIn: make(map[uuid.UUID]bool),
|
|
|
|
inputMSG: make(chan *wsLib.Message),
|
|
|
|
secret: secret,
|
|
|
|
}
|
|
|
|
ownWS.ws = wsLib.NewServer(ownWS.inputMSG, wsLib.NewSessionManager())
|
2017-05-09 01:03:44 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
// Register Handlers
|
|
|
|
ownWS.handlers[MessageTypeConnect] = ownWS.connectHandler
|
2017-05-09 01:03:44 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
ownWS.handlers[MessageTypeLogin] = ownWS.loginHandler
|
|
|
|
ownWS.handlers[MessageTypeAuthStatus] = ownWS.authStatusHandler
|
|
|
|
ownWS.handlers[MessageTypeLogout] = ownWS.logoutHandler
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
ownWS.handlers[MessageTypeSystemNode] = ownWS.nodeHandler
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
http.HandleFunc("/ws", ownWS.ws.Handler)
|
|
|
|
go ownWS.MessageHandler()
|
|
|
|
return &ownWS
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (ws *WebsocketServer) Close() {
|
|
|
|
close(ws.inputMSG)
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|