mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
fixes #14
This commit is contained in:
37
server/internal/websocket/socket.go
Normal file
37
server/internal/websocket/socket.go
Normal file
@ -0,0 +1,37 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type WebSocket struct {
|
||||
id string
|
||||
connection *websocket.Conn
|
||||
}
|
||||
|
||||
func (socket *WebSocket) Address() *string {
|
||||
remote := socket.connection.RemoteAddr()
|
||||
address := strings.SplitN(remote.String(), ":", -1)
|
||||
if len(address[0]) < 1 {
|
||||
return nil
|
||||
}
|
||||
return &address[0]
|
||||
}
|
||||
|
||||
func (socket *WebSocket) Send(v interface{}) error {
|
||||
if socket.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return socket.connection.WriteJSON(v)
|
||||
}
|
||||
|
||||
func (socket *WebSocket) Destroy() error {
|
||||
if socket.connection == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return socket.connection.Close()
|
||||
}
|
Reference in New Issue
Block a user