123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package main
- import (
- "context"
- "log"
- "sync"
- "sync/atomic"
- "git.ali33.ru/fcg-xvii/go-tools/containers/concurrent"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- "github.com/gorilla/websocket"
- )
- func NewWSClients() *WSClients {
- return &WSClients{
- List: concurrent.NewList(),
- rStore: new(sync.Map),
- }
- }
- type WSClients struct {
- *concurrent.List
- rStore *sync.Map
- counter uint32
- }
- func (s *WSClients) Broadcast(mes json.Map) {
- for e := s.First(); e != nil; e = e.Next() {
- e.Val().(*WSClient).SendSingle(mes)
- }
- }
- func (s *WSClients) clientConnected(conn *websocket.Conn) *WSClient {
- cctx, cancel := context.WithCancel(context.Background())
- cl := &WSClient{
- conn: conn,
- clients: s,
- ctx: cctx,
- cancel: cancel,
- }
- s.PushBack(cl)
- return cl
- }
- func (s *WSClients) clientDisconnected(cl *WSClient) {
- log.Println("DISCONNECTED")
- if e := s.Search(cl); e != nil {
- s.Remove(e)
- }
- }
- func (s *WSClients) nextID() int64 {
- return int64(atomic.AddUint32(&s.counter, 1))
- }
|