updated
This commit is contained in:
@@ -1,51 +1,44 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// type Clients map[string]Client
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
// type Client struct {
|
||||
// Connected *bool `json:"connected"`
|
||||
// SndConn *websocket.Conn `json:"-"` //sending connection
|
||||
// RvcConn *websocket.Conn `json:"-"` // revieving connection
|
||||
// }
|
||||
|
||||
type Clients map[string]Client
|
||||
// func NewClients() Clients {
|
||||
// return make(Clients)
|
||||
// }
|
||||
|
||||
type Client struct {
|
||||
Connected *bool `json:"connected"`
|
||||
SndConn *websocket.Conn `json:"-"` //sending connection
|
||||
RvcConn *websocket.Conn `json:"-"` // revieving connection
|
||||
}
|
||||
// // Connect a recieving websocket connection
|
||||
// func (c *Clients) ConnectRecievingWsConnection(id string, ctx *gin.Context) (*websocket.Conn, error) {
|
||||
// conn, err := websocket.Accept(ctx.Writer, ctx.Request, &websocket.AcceptOptions{
|
||||
// OriginPatterns: []string{"*"},
|
||||
// })
|
||||
|
||||
func NewClients() Clients {
|
||||
return make(Clients)
|
||||
}
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("error accept websocket client: %s", err)
|
||||
// }
|
||||
|
||||
// Connect a recieving websocket connection
|
||||
func (c *Clients) ConnectRecievingWsConnection(id string, ctx *gin.Context) (*websocket.Conn, error) {
|
||||
conn, err := websocket.Accept(ctx.Writer, ctx.Request, &websocket.AcceptOptions{
|
||||
OriginPatterns: []string{"*"},
|
||||
})
|
||||
// b := true
|
||||
// (*c)[id] = Client{
|
||||
// Connected: &b,
|
||||
// RvcConn: conn,
|
||||
// }
|
||||
// return conn, nil
|
||||
// }
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error accept websocket client: %s", err)
|
||||
}
|
||||
// func (c *Clients) RemoveClient(id string) {
|
||||
// delete(*c, id)
|
||||
// }
|
||||
|
||||
b := true
|
||||
(*c)[id] = Client{
|
||||
Connected: &b,
|
||||
RvcConn: conn,
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
// func (c *Clients) GetClientPointer(id string) *bool {
|
||||
// return (*c)[id].Connected
|
||||
// }
|
||||
|
||||
func (c *Clients) RemoveClient(id string) {
|
||||
delete(*c, id)
|
||||
}
|
||||
|
||||
func (c *Clients) GetClientPointer(id string) *bool {
|
||||
return (*c)[id].Connected
|
||||
}
|
||||
|
||||
func (c *Clients) DisconnectRecievingWsConnection(id string, code websocket.StatusCode, reason string) {
|
||||
*(*c)[id].Connected = false
|
||||
(*c)[id].RvcConn.Close(code, reason)
|
||||
}
|
||||
// func (c *Clients) DisconnectRecievingWsConnection(id string, code websocket.StatusCode, reason string) {
|
||||
// *(*c)[id].Connected = false
|
||||
// (*c)[id].RvcConn.Close(code, reason)
|
||||
// }
|
||||
|
Reference in New Issue
Block a user