123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package application
- import (
- "context"
- "log"
- "sync"
- "sync/atomic"
- "git.ali33.ru/fcg-xvii/rest"
- )
- func NewSockets() *Sockets {
- res := &Sockets{
- counter: &atomic.Int64{},
- chConnect: make(chan rest.IStream, 5),
- clients: &sync.Map{},
- }
- go res.work()
- return res
- }
- type Sockets struct {
- ctx context.Context
- cancel context.CancelFunc
- counter *atomic.Int64
- chConnect chan rest.IStream
- clients *sync.Map
- }
- func (s *Sockets) work() {
- s.ctx, s.cancel = context.WithCancel(context.Background())
- for {
- select {
- case <-s.ctx.Done():
- return
- case stream := <-s.chConnect:
- log.Println("CONNECT-------", s.counter.Add(1))
- go func() {
- <-stream.Context().Done()
- log.Println("DISCONNECT-------", s.counter.Add(-1))
- /*
- // auth
- auth := stream.Auth()
- if auth != nil {
- id := auth.Int32("id", 0)
- iStore, ok := s.clients.Load(id)
- if ok {
- store := iStore.(*StreamStore)
- if store.remove(stream) == 0 {
- s.clients.Delete(id)
- }
- }
- }
- */
- }()
- }
- }
- }
- func (s *Sockets) Connect() chan<- rest.IStream {
- return s.chConnect
- }
- func (s *Sockets) OnlineCount() int64 {
- return s.counter.Load()
- }
|