mirror of
https://github.com/m1k1o/neko.git
synced 2025-05-12 00:27:28 +02:00
initial commit - from neko open source repository..
This commit is contained in:
commit
56de805f54
66 changed files with 5498 additions and 0 deletions
55
internal/websocket/socket.go
Normal file
55
internal/websocket/socket.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type WebSocket struct {
|
||||
id string
|
||||
address string
|
||||
ws *WebSocketHandler
|
||||
connection *websocket.Conn
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (socket *WebSocket) Address() string {
|
||||
//remote := socket.connection.RemoteAddr()
|
||||
address := strings.SplitN(socket.address, ":", -1)
|
||||
if len(address[0]) < 1 {
|
||||
return socket.address
|
||||
}
|
||||
return address[0]
|
||||
}
|
||||
|
||||
func (socket *WebSocket) 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.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 *WebSocket) Destroy() error {
|
||||
if socket.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return socket.connection.Close()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue