mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
socket -> websocket_peer.
This commit is contained in:
parent
d49542013b
commit
ff7efe9f3b
@ -15,7 +15,7 @@ type SessionCtx struct {
|
||||
admin bool
|
||||
connected bool
|
||||
manager *SessionManagerCtx
|
||||
socket types.WebSocket
|
||||
websocket_peer types.WebSocketPeer
|
||||
peer types.Peer
|
||||
}
|
||||
|
||||
@ -43,8 +43,8 @@ func (session *SessionCtx) SetName(name string) {
|
||||
session.name = name
|
||||
}
|
||||
|
||||
func (session *SessionCtx) SetSocket(socket types.WebSocket) {
|
||||
session.socket = socket
|
||||
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
|
||||
session.websocket_peer = websocket_peer
|
||||
session.manager.emmiter.Emit("created", session)
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ func (session *SessionCtx) SetConnected(connected bool) {
|
||||
session.manager.emmiter.Emit("connected", session)
|
||||
} else {
|
||||
session.manager.emmiter.Emit("disconnected", session)
|
||||
session.socket = nil
|
||||
session.websocket_peer = nil
|
||||
|
||||
// TODO: Refactor.
|
||||
_ = session.manager.Destroy(session.id)
|
||||
@ -77,11 +77,11 @@ func (session *SessionCtx) Disconnect(reason string) error {
|
||||
}
|
||||
|
||||
func (session *SessionCtx) Send(v interface{}) error {
|
||||
if session.socket == nil {
|
||||
if session.websocket_peer == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return session.socket.Send(v)
|
||||
return session.websocket_peer.Send(v)
|
||||
}
|
||||
|
||||
func (session *SessionCtx) SignalAnswer(sdp string) error {
|
||||
@ -93,8 +93,8 @@ func (session *SessionCtx) SignalAnswer(sdp string) error {
|
||||
}
|
||||
|
||||
func (session *SessionCtx) destroy() error {
|
||||
if session.socket != nil {
|
||||
if err := session.socket.Destroy(); err != nil {
|
||||
if session.websocket_peer != nil {
|
||||
if err := session.websocket_peer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ type Session interface {
|
||||
IsHost() bool
|
||||
Connected() bool
|
||||
SetName(name string)
|
||||
SetSocket(socket WebSocket)
|
||||
SetWebSocketPeer(websocket_peer WebSocketPeer)
|
||||
SetPeer(peer Peer)
|
||||
SetConnected(connected bool)
|
||||
Disconnect(reason string) error
|
||||
|
@ -2,7 +2,7 @@ package types
|
||||
|
||||
import "net/http"
|
||||
|
||||
type WebSocket interface {
|
||||
type WebSocketPeer interface {
|
||||
Send(v interface{}) error
|
||||
Destroy() error
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ type MessageHandlerCtx struct {
|
||||
locked bool
|
||||
}
|
||||
|
||||
func (h *MessageHandlerCtx) Connected(session types.Session, socket types.WebSocket) (bool, string) {
|
||||
func (h *MessageHandlerCtx) Connected(session types.Session, websocket_peer types.WebSocketPeer) (bool, string) {
|
||||
if h.locked && !session.Admin(){
|
||||
h.logger.Debug().Msg("server locked")
|
||||
return false, "locked"
|
||||
|
@ -142,13 +142,13 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) e
|
||||
return connection.Close()
|
||||
}
|
||||
|
||||
socket := &WebSocketCtx{
|
||||
websocket_peer := &WebSocketPeerCtx{
|
||||
session: session,
|
||||
ws: ws,
|
||||
connection: connection,
|
||||
}
|
||||
|
||||
ok, reason := ws.handler.Connected(session, socket)
|
||||
ok, reason := ws.handler.Connected(session, websocket_peer)
|
||||
if !ok {
|
||||
// TODO: Refactor
|
||||
if err = connection.WriteJSON(message.Disconnect{
|
||||
@ -161,7 +161,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) e
|
||||
return connection.Close()
|
||||
}
|
||||
|
||||
session.SetSocket(socket)
|
||||
session.SetWebSocketPeer(websocket_peer)
|
||||
|
||||
ws.logger.
|
||||
Debug().
|
||||
|
47
internal/websocket/peer.go
Normal file
47
internal/websocket/peer.go
Normal file
@ -0,0 +1,47 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
type WebSocketPeerCtx struct {
|
||||
session types.Session
|
||||
ws *WebSocketManagerCtx
|
||||
connection *websocket.Conn
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (websocket_peer *WebSocketPeerCtx) Send(v interface{}) error {
|
||||
websocket_peer.mu.Lock()
|
||||
defer websocket_peer.mu.Unlock()
|
||||
|
||||
if websocket_peer.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
websocket_peer.ws.logger.Debug().
|
||||
Str("session", websocket_peer.session.ID()).
|
||||
Str("address", websocket_peer.connection.RemoteAddr().String()).
|
||||
Str("raw", string(raw)).
|
||||
Msg("sending message to client")
|
||||
|
||||
return websocket_peer.connection.WriteMessage(websocket.TextMessage, raw)
|
||||
}
|
||||
|
||||
func (websocket_peer *WebSocketPeerCtx) Destroy() error {
|
||||
if websocket_peer.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return websocket_peer.connection.Close()
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
type WebSocketCtx struct {
|
||||
session types.Session
|
||||
ws *WebSocketManagerCtx
|
||||
connection *websocket.Conn
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (socket *WebSocketCtx) Send(v interface{}) error {
|
||||
socket.mu.Lock()
|
||||
defer socket.mu.Unlock()
|
||||
|
||||
if socket.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
socket.ws.logger.Debug().
|
||||
Str("session", socket.session.ID()).
|
||||
Str("address", socket.connection.RemoteAddr().String()).
|
||||
Str("raw", string(raw)).
|
||||
Msg("sending message to client")
|
||||
|
||||
return socket.connection.WriteMessage(websocket.TextMessage, raw)
|
||||
}
|
||||
|
||||
func (socket *WebSocketCtx) Destroy() error {
|
||||
if socket.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return socket.connection.Close()
|
||||
}
|
Loading…
Reference in New Issue
Block a user