mirror of https://github.com/1Panel-dev/1Panel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
663 B
46 lines
663 B
package websocket
|
|
|
|
import (
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type Client struct {
|
|
ID string
|
|
Socket *websocket.Conn
|
|
Msg chan []byte
|
|
}
|
|
|
|
func NewWsClient(ID string, socket *websocket.Conn) *Client {
|
|
return &Client{
|
|
ID: ID,
|
|
Socket: socket,
|
|
Msg: make(chan []byte, 100),
|
|
}
|
|
}
|
|
|
|
func (c *Client) Read() {
|
|
defer func() {
|
|
close(c.Msg)
|
|
}()
|
|
for {
|
|
_, message, err := c.Socket.ReadMessage()
|
|
if err != nil {
|
|
return
|
|
}
|
|
ProcessData(c, message)
|
|
}
|
|
}
|
|
|
|
func (c *Client) Write() {
|
|
defer func() {
|
|
c.Socket.Close()
|
|
}()
|
|
for {
|
|
message, ok := <-c.Msg
|
|
if !ok {
|
|
return
|
|
}
|
|
_ = c.Socket.WriteMessage(websocket.TextMessage, message)
|
|
}
|
|
}
|