2017-05-08 19:13:29 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-08-10 17:17:00 +02:00
|
|
|
"time"
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
|
|
|
|
"github.com/jinzhu/gorm"
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2017-05-08 19:13:29 +02:00
|
|
|
)
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
type WebsocketServer struct {
|
2018-08-10 17:17:00 +02:00
|
|
|
nodes *runtime.Nodes
|
|
|
|
db *gorm.DB
|
|
|
|
blacklistFor time.Duration
|
|
|
|
secret string
|
|
|
|
ipPrefix string
|
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-08-10 17:17:00 +02:00
|
|
|
func NewWebsocketServer(secret string, ipPrefix string, db *gorm.DB, blacklistFor time.Duration, nodes *runtime.Nodes) *WebsocketServer {
|
2018-06-30 16:20:54 +02:00
|
|
|
ownWS := WebsocketServer{
|
2018-08-10 17:17:00 +02:00
|
|
|
nodes: nodes,
|
|
|
|
db: db,
|
|
|
|
blacklistFor: blacklistFor,
|
|
|
|
handlers: make(map[string]WebsocketHandlerFunc),
|
|
|
|
inputMSG: make(chan *wsLib.Message),
|
|
|
|
secret: secret,
|
|
|
|
ipPrefix: ipPrefix,
|
2018-06-30 16:20:54 +02:00
|
|
|
}
|
|
|
|
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-08-10 13:46:18 +02:00
|
|
|
ownWS.handlers[MessageTypeNode] = 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
|
|
|
}
|