mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
fix naming convention.
This commit is contained in:
parent
45679f1b86
commit
2b693f4e52
@ -78,24 +78,24 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HttpManagerCtx) Start() {
|
||||
if s.conf.Cert != "" && s.conf.Key != "" {
|
||||
func (manager *HttpManagerCtx) Start() {
|
||||
if manager.conf.Cert != "" && manager.conf.Key != "" {
|
||||
go func() {
|
||||
if err := s.http.ListenAndServeTLS(s.conf.Cert, s.conf.Key); err != http.ErrServerClosed {
|
||||
s.logger.Panic().Err(err).Msg("unable to start https server")
|
||||
if err := manager.http.ListenAndServeTLS(manager.conf.Cert, manager.conf.Key); err != http.ErrServerClosed {
|
||||
manager.logger.Panic().Err(err).Msg("unable to start https server")
|
||||
}
|
||||
}()
|
||||
s.logger.Info().Msgf("https listening on %s", s.http.Addr)
|
||||
manager.logger.Info().Msgf("https listening on %s", manager.http.Addr)
|
||||
} else {
|
||||
go func() {
|
||||
if err := s.http.ListenAndServe(); err != http.ErrServerClosed {
|
||||
s.logger.Panic().Err(err).Msg("unable to start http server")
|
||||
if err := manager.http.ListenAndServe(); err != http.ErrServerClosed {
|
||||
manager.logger.Panic().Err(err).Msg("unable to start http server")
|
||||
}
|
||||
}()
|
||||
s.logger.Info().Msgf("http listening on %s", s.http.Addr)
|
||||
manager.logger.Info().Msgf("http listening on %s", manager.http.Addr)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HttpManagerCtx) Shutdown() error {
|
||||
return s.http.Shutdown(context.Background())
|
||||
func (manager *HttpManagerCtx) Shutdown() error {
|
||||
return manager.http.Shutdown(context.Background())
|
||||
}
|
||||
|
@ -9,14 +9,14 @@ import (
|
||||
)
|
||||
|
||||
type SessionCtx struct {
|
||||
id string
|
||||
logger zerolog.Logger
|
||||
manager *SessionManagerCtx
|
||||
profile types.MemberProfile
|
||||
websocket_peer types.WebSocketPeer
|
||||
websocket_connected bool
|
||||
webrtc_peer types.WebRTCPeer
|
||||
webrtc_connected bool
|
||||
id string
|
||||
logger zerolog.Logger
|
||||
manager *SessionManagerCtx
|
||||
profile types.MemberProfile
|
||||
websocketPeer types.WebSocketPeer
|
||||
websocketConnected bool
|
||||
webrtcPeer types.WebRTCPeer
|
||||
webrtcConnected bool
|
||||
}
|
||||
|
||||
func (session *SessionCtx) ID() string {
|
||||
@ -77,7 +77,7 @@ func (session *SessionCtx) profileChanged() {
|
||||
}
|
||||
|
||||
if !session.CanWatch() && session.IsWatching() {
|
||||
if err := session.webrtc_peer.Destroy(); err != nil {
|
||||
if err := session.webrtcPeer.Destroy(); err != nil {
|
||||
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
|
||||
}
|
||||
}
|
||||
@ -98,11 +98,11 @@ func (session *SessionCtx) IsHost() bool {
|
||||
}
|
||||
|
||||
func (session *SessionCtx) IsConnected() bool {
|
||||
return session.websocket_connected
|
||||
return session.websocketConnected
|
||||
}
|
||||
|
||||
func (session *SessionCtx) IsWatching() bool {
|
||||
return session.webrtc_connected
|
||||
return session.webrtcConnected
|
||||
}
|
||||
|
||||
func (session *SessionCtx) GetState() types.MemberState {
|
||||
@ -117,18 +117,18 @@ func (session *SessionCtx) GetState() types.MemberState {
|
||||
// webscoket
|
||||
// ---
|
||||
|
||||
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
|
||||
if session.websocket_peer != nil {
|
||||
if err := session.websocket_peer.Destroy(); err != nil {
|
||||
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
|
||||
if session.websocketPeer != nil {
|
||||
if err := session.websocketPeer.Destroy(); err != nil {
|
||||
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
|
||||
}
|
||||
}
|
||||
|
||||
session.websocket_peer = websocket_peer
|
||||
session.websocketPeer = websocketPeer
|
||||
}
|
||||
|
||||
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
|
||||
session.websocket_connected = connected
|
||||
session.websocketConnected = connected
|
||||
|
||||
if connected {
|
||||
session.manager.emmiter.Emit("connected", session)
|
||||
@ -136,21 +136,21 @@ func (session *SessionCtx) SetWebSocketConnected(connected bool) {
|
||||
}
|
||||
|
||||
session.manager.emmiter.Emit("disconnected", session)
|
||||
session.websocket_peer = nil
|
||||
session.websocketPeer = nil
|
||||
|
||||
if session.webrtc_peer != nil {
|
||||
if err := session.webrtc_peer.Destroy(); err != nil {
|
||||
if session.webrtcPeer != nil {
|
||||
if err := session.webrtcPeer.Destroy(); err != nil {
|
||||
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (session *SessionCtx) Send(v interface{}) error {
|
||||
if session.websocket_peer == nil {
|
||||
if session.websocketPeer == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return session.websocket_peer.Send(v)
|
||||
return session.websocketPeer.Send(v)
|
||||
}
|
||||
|
||||
func (session *SessionCtx) Disconnect(reason string) error {
|
||||
@ -162,14 +162,14 @@ func (session *SessionCtx) Disconnect(reason string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if session.websocket_peer != nil {
|
||||
if err := session.websocket_peer.Destroy(); err != nil {
|
||||
if session.websocketPeer != nil {
|
||||
if err := session.websocketPeer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if session.webrtc_peer != nil {
|
||||
if err := session.webrtc_peer.Destroy(); err != nil {
|
||||
if session.webrtcPeer != nil {
|
||||
if err := session.webrtcPeer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -181,25 +181,25 @@ func (session *SessionCtx) Disconnect(reason string) error {
|
||||
// webrtc
|
||||
// ---
|
||||
|
||||
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
|
||||
if session.webrtc_peer != nil {
|
||||
if err := session.webrtc_peer.Destroy(); err != nil {
|
||||
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
|
||||
if session.webrtcPeer != nil {
|
||||
if err := session.webrtcPeer.Destroy(); err != nil {
|
||||
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
|
||||
}
|
||||
}
|
||||
|
||||
session.webrtc_peer = webrtc_peer
|
||||
session.webrtcPeer = webrtcPeer
|
||||
}
|
||||
|
||||
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
|
||||
session.webrtc_connected = connected
|
||||
session.webrtcConnected = connected
|
||||
session.manager.emmiter.Emit("state_changed", session)
|
||||
|
||||
if !connected {
|
||||
session.webrtc_peer = nil
|
||||
session.webrtcPeer = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
|
||||
return session.webrtc_peer
|
||||
return session.webrtcPeer
|
||||
}
|
||||
|
@ -49,13 +49,13 @@ type Session interface {
|
||||
GetState() MemberState
|
||||
|
||||
// websocket
|
||||
SetWebSocketPeer(websocket_peer WebSocketPeer)
|
||||
SetWebSocketPeer(websocketPeer WebSocketPeer)
|
||||
SetWebSocketConnected(connected bool)
|
||||
Send(v interface{}) error
|
||||
Disconnect(reason string) error
|
||||
|
||||
// webrtc
|
||||
SetWebRTCPeer(webrtc_peer WebRTCPeer)
|
||||
SetWebRTCPeer(webrtcPeer WebRTCPeer)
|
||||
SetWebRTCConnected(connected bool)
|
||||
GetWebRTCPeer() WebRTCPeer
|
||||
}
|
||||
|
@ -9,25 +9,25 @@ type WebRTCPeerCtx struct {
|
||||
changeVideo func(videoID string) error
|
||||
}
|
||||
|
||||
func (webrtc_peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
|
||||
return webrtc_peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
||||
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
|
||||
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
||||
SDP: sdp,
|
||||
Type: webrtc.SDPTypeAnswer,
|
||||
})
|
||||
}
|
||||
|
||||
func (webrtc_peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) error {
|
||||
return webrtc_peer.connection.AddICECandidate(candidate)
|
||||
func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) error {
|
||||
return peer.connection.AddICECandidate(candidate)
|
||||
}
|
||||
|
||||
func (webrtc_peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
||||
return webrtc_peer.changeVideo(videoID)
|
||||
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
||||
return peer.changeVideo(videoID)
|
||||
}
|
||||
|
||||
func (webrtc_peer *WebRTCPeerCtx) Destroy() error {
|
||||
if webrtc_peer.connection == nil || webrtc_peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
||||
func (peer *WebRTCPeerCtx) Destroy() error {
|
||||
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
||||
return nil
|
||||
}
|
||||
|
||||
return webrtc_peer.connection.Close()
|
||||
return peer.connection.Close()
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ type PayloadCursorImage struct {
|
||||
Yhot uint16
|
||||
}
|
||||
|
||||
func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
|
||||
if webrtc_peer.dataChannel == nil {
|
||||
func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
|
||||
if peer.dataChannel == nil {
|
||||
return fmt.Errorf("no data channel")
|
||||
}
|
||||
|
||||
@ -47,11 +47,11 @@ func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return webrtc_peer.dataChannel.Send(buffer.Bytes())
|
||||
return peer.dataChannel.Send(buffer.Bytes())
|
||||
}
|
||||
|
||||
func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
|
||||
if webrtc_peer.dataChannel == nil {
|
||||
func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
|
||||
if peer.dataChannel == nil {
|
||||
return fmt.Errorf("no data channel")
|
||||
}
|
||||
|
||||
@ -81,5 +81,5 @@ func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error
|
||||
return err
|
||||
}
|
||||
|
||||
return webrtc_peer.dataChannel.Send(buffer.Bytes())
|
||||
return peer.dataChannel.Send(buffer.Bytes())
|
||||
}
|
||||
|
@ -6,41 +6,41 @@ import (
|
||||
"demodesk/neko/internal/types/message"
|
||||
)
|
||||
|
||||
func (ws *WebSocketManagerCtx) fileChooserDialogEvents() {
|
||||
func (manager *WebSocketManagerCtx) fileChooserDialogEvents() {
|
||||
var file_chooser_dialog_member types.Session
|
||||
|
||||
// when dialog opens, everyone should be notified.
|
||||
ws.desktop.OnFileChooserDialogOpened(func() {
|
||||
ws.logger.Info().Msg("FileChooserDialog opened")
|
||||
manager.desktop.OnFileChooserDialogOpened(func() {
|
||||
manager.logger.Info().Msg("FileChooserDialog opened")
|
||||
|
||||
host := ws.sessions.GetHost()
|
||||
host := manager.sessions.GetHost()
|
||||
if host == nil {
|
||||
ws.logger.Warn().Msg("no host for FileChooserDialog found, closing")
|
||||
go ws.desktop.CloseFileChooserDialog()
|
||||
manager.logger.Warn().Msg("no host for FileChooserDialog found, closing")
|
||||
go manager.desktop.CloseFileChooserDialog()
|
||||
return
|
||||
}
|
||||
|
||||
file_chooser_dialog_member = host
|
||||
|
||||
go ws.sessions.Broadcast(message.MemberID{
|
||||
go manager.sessions.Broadcast(message.MemberID{
|
||||
Event: event.FILE_CHOOSER_DIALOG_OPENED,
|
||||
ID: host.ID(),
|
||||
}, nil)
|
||||
})
|
||||
|
||||
// when dialog closes, everyone should be notified.
|
||||
ws.desktop.OnFileChooserDialogClosed(func() {
|
||||
ws.logger.Info().Msg("FileChooserDialog closed")
|
||||
manager.desktop.OnFileChooserDialogClosed(func() {
|
||||
manager.logger.Info().Msg("FileChooserDialog closed")
|
||||
|
||||
file_chooser_dialog_member = nil
|
||||
|
||||
go ws.sessions.Broadcast(message.MemberID{
|
||||
go manager.sessions.Broadcast(message.MemberID{
|
||||
Event: event.FILE_CHOOSER_DIALOG_CLOSED,
|
||||
}, nil)
|
||||
})
|
||||
|
||||
// when new user joins, and someone holds dialog, he shouldd be notified about it.
|
||||
ws.sessions.OnConnected(func(session types.Session) {
|
||||
manager.sessions.OnConnected(func(session types.Session) {
|
||||
if file_chooser_dialog_member == nil {
|
||||
return
|
||||
}
|
||||
@ -49,7 +49,7 @@ func (ws *WebSocketManagerCtx) fileChooserDialogEvents() {
|
||||
Event: event.FILE_CHOOSER_DIALOG_OPENED,
|
||||
ID: file_chooser_dialog_member.ID(),
|
||||
}); err != nil {
|
||||
ws.logger.Warn().
|
||||
manager.logger.Warn().
|
||||
Str("id", session.ID()).
|
||||
Err(err).
|
||||
Msgf("could not send event `%s` to session", event.FILE_CHOOSER_DIALOG_OPENED)
|
||||
@ -57,7 +57,7 @@ func (ws *WebSocketManagerCtx) fileChooserDialogEvents() {
|
||||
})
|
||||
|
||||
// when user, that holds dialog, disconnects, it should be closed.
|
||||
ws.sessions.OnDisconnected(func(session types.Session) {
|
||||
manager.sessions.OnDisconnected(func(session types.Session) {
|
||||
if file_chooser_dialog_member == nil {
|
||||
return
|
||||
}
|
||||
@ -66,6 +66,6 @@ func (ws *WebSocketManagerCtx) fileChooserDialogEvents() {
|
||||
return
|
||||
}
|
||||
|
||||
ws.desktop.CloseFileChooserDialog()
|
||||
manager.desktop.CloseFileChooserDialog()
|
||||
})
|
||||
}
|
||||
|
@ -43,64 +43,64 @@ type WebSocketManagerCtx struct {
|
||||
shutdown chan bool
|
||||
}
|
||||
|
||||
func (ws *WebSocketManagerCtx) Start() {
|
||||
ws.sessions.OnCreated(func(session types.Session) {
|
||||
if err := ws.handler.SessionCreated(session); err != nil {
|
||||
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session created with an error")
|
||||
func (manager *WebSocketManagerCtx) Start() {
|
||||
manager.sessions.OnCreated(func(session types.Session) {
|
||||
if err := manager.handler.SessionCreated(session); err != nil {
|
||||
manager.logger.Warn().Str("id", session.ID()).Err(err).Msg("session created with an error")
|
||||
} else {
|
||||
ws.logger.Debug().Str("id", session.ID()).Msg("session created")
|
||||
manager.logger.Debug().Str("id", session.ID()).Msg("session created")
|
||||
}
|
||||
})
|
||||
|
||||
ws.sessions.OnDeleted(func(session types.Session) {
|
||||
if err := ws.handler.SessionDeleted(session); err != nil {
|
||||
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session deleted with an error")
|
||||
manager.sessions.OnDeleted(func(session types.Session) {
|
||||
if err := manager.handler.SessionDeleted(session); err != nil {
|
||||
manager.logger.Warn().Str("id", session.ID()).Err(err).Msg("session deleted with an error")
|
||||
} else {
|
||||
ws.logger.Debug().Str("id", session.ID()).Msg("session deleted")
|
||||
manager.logger.Debug().Str("id", session.ID()).Msg("session deleted")
|
||||
}
|
||||
})
|
||||
|
||||
ws.sessions.OnConnected(func(session types.Session) {
|
||||
if err := ws.handler.SessionConnected(session); err != nil {
|
||||
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session connected with an error")
|
||||
manager.sessions.OnConnected(func(session types.Session) {
|
||||
if err := manager.handler.SessionConnected(session); err != nil {
|
||||
manager.logger.Warn().Str("id", session.ID()).Err(err).Msg("session connected with an error")
|
||||
} else {
|
||||
ws.logger.Debug().Str("id", session.ID()).Msg("session connected")
|
||||
manager.logger.Debug().Str("id", session.ID()).Msg("session connected")
|
||||
}
|
||||
})
|
||||
|
||||
ws.sessions.OnDisconnected(func(session types.Session) {
|
||||
if err := ws.handler.SessionDisconnected(session); err != nil {
|
||||
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session disconnected with an error")
|
||||
manager.sessions.OnDisconnected(func(session types.Session) {
|
||||
if err := manager.handler.SessionDisconnected(session); err != nil {
|
||||
manager.logger.Warn().Str("id", session.ID()).Err(err).Msg("session disconnected with an error")
|
||||
} else {
|
||||
ws.logger.Debug().Str("id", session.ID()).Msg("session disconnected")
|
||||
manager.logger.Debug().Str("id", session.ID()).Msg("session disconnected")
|
||||
}
|
||||
})
|
||||
|
||||
ws.sessions.OnProfileChanged(func(session types.Session) {
|
||||
if err := ws.handler.SessionProfileChanged(session); err != nil {
|
||||
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile changed with an error")
|
||||
manager.sessions.OnProfileChanged(func(session types.Session) {
|
||||
if err := manager.handler.SessionProfileChanged(session); err != nil {
|
||||
manager.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile changed with an error")
|
||||
} else {
|
||||
ws.logger.Debug().Str("id", session.ID()).Msg("session profile changed")
|
||||
manager.logger.Debug().Str("id", session.ID()).Msg("session profile changed")
|
||||
}
|
||||
})
|
||||
|
||||
ws.sessions.OnStateChanged(func(session types.Session) {
|
||||
if err := ws.handler.SessionStateChanged(session); err != nil {
|
||||
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session state changed with an error")
|
||||
manager.sessions.OnStateChanged(func(session types.Session) {
|
||||
if err := manager.handler.SessionStateChanged(session); err != nil {
|
||||
manager.logger.Warn().Str("id", session.ID()).Err(err).Msg("session state changed with an error")
|
||||
} else {
|
||||
ws.logger.Debug().Str("id", session.ID()).Msg("session state changed")
|
||||
manager.logger.Debug().Str("id", session.ID()).Msg("session state changed")
|
||||
}
|
||||
})
|
||||
|
||||
ws.desktop.OnClipboardUpdated(func() {
|
||||
session := ws.sessions.GetHost()
|
||||
manager.desktop.OnClipboardUpdated(func() {
|
||||
session := manager.sessions.GetHost()
|
||||
if session == nil || !session.CanAccessClipboard() {
|
||||
return
|
||||
}
|
||||
|
||||
data, err := ws.desktop.ClipboardGetText()
|
||||
data, err := manager.desktop.ClipboardGetText()
|
||||
if err != nil {
|
||||
ws.logger.Warn().Err(err).Msg("could not get clipboard content")
|
||||
manager.logger.Warn().Err(err).Msg("could not get clipboard content")
|
||||
return
|
||||
}
|
||||
|
||||
@ -109,24 +109,24 @@ func (ws *WebSocketManagerCtx) Start() {
|
||||
Text: data.Text,
|
||||
// TODO: Send HTML?
|
||||
}); err != nil {
|
||||
ws.logger.Warn().Err(err).Msg("could not sync clipboard")
|
||||
manager.logger.Warn().Err(err).Msg("could not sync clipboard")
|
||||
}
|
||||
})
|
||||
|
||||
ws.fileChooserDialogEvents()
|
||||
manager.fileChooserDialogEvents()
|
||||
}
|
||||
|
||||
func (ws *WebSocketManagerCtx) Shutdown() error {
|
||||
ws.shutdown <- true
|
||||
func (manager *WebSocketManagerCtx) Shutdown() error {
|
||||
manager.shutdown <- true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WebSocketManagerCtx) AddHandler(handler types.HandlerFunction) {
|
||||
ws.handlers = append(ws.handlers, handler)
|
||||
func (manager *WebSocketManagerCtx) AddHandler(handler types.HandlerFunction) {
|
||||
manager.handlers = append(manager.handlers, handler)
|
||||
}
|
||||
|
||||
func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin types.CheckOrigin) error {
|
||||
ws.logger.Debug().Msg("attempting to upgrade connection")
|
||||
func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin types.CheckOrigin) error {
|
||||
manager.logger.Debug().Msg("attempting to upgrade connection")
|
||||
|
||||
upgrader := websocket.Upgrader{
|
||||
CheckOrigin: checkOrigin,
|
||||
@ -134,13 +134,13 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
|
||||
connection, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
ws.logger.Error().Err(err).Msg("failed to upgrade connection")
|
||||
manager.logger.Error().Err(err).Msg("failed to upgrade connection")
|
||||
return err
|
||||
}
|
||||
|
||||
session, err := ws.sessions.AuthenticateRequest(r)
|
||||
session, err := manager.sessions.AuthenticateRequest(r)
|
||||
if err != nil {
|
||||
ws.logger.Warn().Err(err).Msg("authentication failed")
|
||||
manager.logger.Warn().Err(err).Msg("authentication failed")
|
||||
|
||||
// TODO: Refactor, return error code.
|
||||
if err = connection.WriteJSON(
|
||||
@ -148,7 +148,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
Event: event.SYSTEM_DISCONNECT,
|
||||
Message: err.Error(),
|
||||
}); err != nil {
|
||||
ws.logger.Error().Err(err).Msg("failed to send disconnect")
|
||||
manager.logger.Error().Err(err).Msg("failed to send disconnect")
|
||||
}
|
||||
|
||||
return connection.Close()
|
||||
@ -161,7 +161,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
Event: event.SYSTEM_DISCONNECT,
|
||||
Message: "connection disabled",
|
||||
}); err != nil {
|
||||
ws.logger.Error().Err(err).Msg("failed to send disconnect")
|
||||
manager.logger.Error().Err(err).Msg("failed to send disconnect")
|
||||
}
|
||||
|
||||
return connection.Close()
|
||||
@ -174,7 +174,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
Event: event.SYSTEM_DISCONNECT,
|
||||
Message: "already connected",
|
||||
}); err != nil {
|
||||
ws.logger.Error().Err(err).Msg("failed to send disconnect")
|
||||
manager.logger.Error().Err(err).Msg("failed to send disconnect")
|
||||
}
|
||||
|
||||
return connection.Close()
|
||||
@ -182,11 +182,11 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
|
||||
session.SetWebSocketPeer(&WebSocketPeerCtx{
|
||||
session: session,
|
||||
ws: ws,
|
||||
manager: manager,
|
||||
connection: connection,
|
||||
})
|
||||
|
||||
ws.logger.
|
||||
manager.logger.
|
||||
Debug().
|
||||
Str("session", session.ID()).
|
||||
Str("address", connection.RemoteAddr().String()).
|
||||
@ -195,7 +195,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
session.SetWebSocketConnected(true)
|
||||
|
||||
defer func() {
|
||||
ws.logger.
|
||||
manager.logger.
|
||||
Debug().
|
||||
Str("session", session.ID()).
|
||||
Str("address", connection.RemoteAddr().String()).
|
||||
@ -204,11 +204,11 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, c
|
||||
session.SetWebSocketConnected(false)
|
||||
}()
|
||||
|
||||
ws.handle(connection, session)
|
||||
manager.handle(connection, session)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.Session) {
|
||||
func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.Session) {
|
||||
bytes := make(chan []byte)
|
||||
cancel := make(chan struct{})
|
||||
|
||||
@ -220,9 +220,9 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.
|
||||
_, raw, err := connection.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
ws.logger.Warn().Err(err).Msg("read message error")
|
||||
manager.logger.Warn().Err(err).Msg("read message error")
|
||||
} else {
|
||||
ws.logger.Debug().Err(err).Msg("read message error")
|
||||
manager.logger.Debug().Err(err).Msg("read message error")
|
||||
}
|
||||
|
||||
close(cancel)
|
||||
@ -236,14 +236,14 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.
|
||||
for {
|
||||
select {
|
||||
case raw := <-bytes:
|
||||
ws.logger.Debug().
|
||||
manager.logger.Debug().
|
||||
Str("session", session.ID()).
|
||||
Str("address", connection.RemoteAddr().String()).
|
||||
Str("raw", string(raw)).
|
||||
Msg("received message from client")
|
||||
|
||||
handled := ws.handler.Message(session, raw)
|
||||
for _, handler := range ws.handlers {
|
||||
handled := manager.handler.Message(session, raw)
|
||||
for _, handler := range manager.handlers {
|
||||
if handled {
|
||||
break
|
||||
}
|
||||
@ -252,13 +252,13 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.
|
||||
}
|
||||
|
||||
if !handled {
|
||||
ws.logger.Warn().Msg("unhandled message")
|
||||
manager.logger.Warn().Msg("unhandled message")
|
||||
}
|
||||
case <-cancel:
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := connection.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
ws.logger.Error().Err(err).Msg("ping message has failed")
|
||||
manager.logger.Error().Err(err).Msg("ping message has failed")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -10,17 +10,17 @@ import (
|
||||
)
|
||||
|
||||
type WebSocketPeerCtx struct {
|
||||
session types.Session
|
||||
ws *WebSocketManagerCtx
|
||||
connection *websocket.Conn
|
||||
mu sync.Mutex
|
||||
session types.Session
|
||||
manager *WebSocketManagerCtx
|
||||
connection *websocket.Conn
|
||||
}
|
||||
|
||||
func (websocket_peer *WebSocketPeerCtx) Send(v interface{}) error {
|
||||
websocket_peer.mu.Lock()
|
||||
defer websocket_peer.mu.Unlock()
|
||||
func (peer *WebSocketPeerCtx) Send(v interface{}) error {
|
||||
peer.mu.Lock()
|
||||
defer peer.mu.Unlock()
|
||||
|
||||
if websocket_peer.connection == nil {
|
||||
if peer.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -29,19 +29,19 @@ func (websocket_peer *WebSocketPeerCtx) Send(v interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
websocket_peer.ws.logger.Debug().
|
||||
Str("session", websocket_peer.session.ID()).
|
||||
Str("address", websocket_peer.connection.RemoteAddr().String()).
|
||||
peer.manager.logger.Debug().
|
||||
Str("session", peer.session.ID()).
|
||||
Str("address", peer.connection.RemoteAddr().String()).
|
||||
Str("raw", string(raw)).
|
||||
Msg("sending message to client")
|
||||
|
||||
return websocket_peer.connection.WriteMessage(websocket.TextMessage, raw)
|
||||
return peer.connection.WriteMessage(websocket.TextMessage, raw)
|
||||
}
|
||||
|
||||
func (websocket_peer *WebSocketPeerCtx) Destroy() error {
|
||||
if websocket_peer.connection == nil {
|
||||
func (peer *WebSocketPeerCtx) Destroy() error {
|
||||
if peer.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return websocket_peer.connection.Close()
|
||||
return peer.connection.Close()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user