2020-02-10 09:23:10 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ChronosX88/zirconium/internal/models"
|
|
|
|
"github.com/google/logger"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConnectionHandler struct {
|
|
|
|
connections map[string]*OriginC2S
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnectionHandler() *ConnectionHandler {
|
2020-02-10 10:44:40 +00:00
|
|
|
return &ConnectionHandler{
|
|
|
|
connections: make(map[string]*OriginC2S),
|
|
|
|
}
|
2020-02-10 09:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *ConnectionHandler) HandleNewConnection(wsocket *websocket.Conn) {
|
|
|
|
uuid, _ := uuid.NewRandom()
|
|
|
|
uuidStr := uuid.String()
|
|
|
|
o := &OriginC2S{
|
|
|
|
wsConn: wsocket,
|
|
|
|
connID: uuidStr,
|
|
|
|
}
|
|
|
|
ch.connections[o.connID] = o
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
var msg models.BaseMessage
|
|
|
|
err := o.wsConn.ReadJSON(&msg)
|
|
|
|
if err != nil {
|
|
|
|
delete(ch.connections, o.connID)
|
|
|
|
o.wsConn.Close()
|
|
|
|
logger.Infof("Connection %s was closed. (Reason: %s)", o.connID, err.Error())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
router.RouteMessage(o, msg)
|
|
|
|
}
|
|
|
|
}()
|
2020-02-10 10:55:43 +00:00
|
|
|
logger.Infof("Connection %s was created", o.connID)
|
2020-02-10 09:23:10 +00:00
|
|
|
}
|