github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/backend/sockets/ajaxsocket/socket.go (about)

     1  /*
     2   *  Glue - Robust Go and Javascript Socket Library
     3   *  Copyright (C) 2015  Roland Singer <roland.singer[at]desertbit.com>
     4   *
     5   *  This program is free software: you can redistribute it and/or modify
     6   *  it under the terms of the GNU General Public License as published by
     7   *  the Free Software Foundation, either version 3 of the License, or
     8   *  (at your option) any later version.
     9   *
    10   *  This program is distributed in the hope that it will be useful,
    11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   *  GNU General Public License for more details.
    14   *
    15   *  You should have received a copy of the GNU General Public License
    16   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package ajaxsocket
    20  
    21  import (
    22  	"github.com/mdaxf/iac/integration/messagebus/glue/backend/closer"
    23  	"github.com/mdaxf/iac/integration/messagebus/glue/backend/global"
    24  )
    25  
    26  //########################//
    27  //### Ajax Socket type ###//
    28  //########################//
    29  
    30  type Socket struct {
    31  	uid        string
    32  	pollToken  string
    33  	userAgent  string
    34  	remoteAddr string
    35  
    36  	closer *closer.Closer
    37  
    38  	writeChan chan string
    39  	readChan  chan string
    40  }
    41  
    42  // Create a new ajax socket.
    43  func newSocket(s *Server) *Socket {
    44  	a := &Socket{
    45  		writeChan: make(chan string, global.WriteChanSize),
    46  		readChan:  make(chan string, global.ReadChanSize),
    47  	}
    48  
    49  	// Set the closer function.
    50  	a.closer = closer.New(func() {
    51  		// Remove the ajax socket from the map.
    52  		if len(a.uid) > 0 {
    53  			func() {
    54  				s.socketsMutex.Lock()
    55  				defer s.socketsMutex.Unlock()
    56  
    57  				delete(s.sockets, a.uid)
    58  			}()
    59  		}
    60  	})
    61  
    62  	return a
    63  }
    64  
    65  //##############################################//
    66  //### Ajax Socket - Interface implementation ###//
    67  //##############################################//
    68  
    69  func (s *Socket) Type() global.SocketType {
    70  	return global.TypeAjaxSocket
    71  }
    72  
    73  func (s *Socket) RemoteAddr() string {
    74  	return s.remoteAddr
    75  }
    76  
    77  func (s *Socket) UserAgent() string {
    78  	return s.userAgent
    79  }
    80  
    81  func (s *Socket) Close() {
    82  	s.closer.Close()
    83  }
    84  
    85  func (s *Socket) IsClosed() bool {
    86  	return s.closer.IsClosed()
    87  }
    88  
    89  func (s *Socket) ClosedChan() <-chan struct{} {
    90  	return s.closer.IsClosedChan
    91  }
    92  
    93  func (s *Socket) WriteChan() chan string {
    94  	return s.writeChan
    95  }
    96  
    97  func (s *Socket) ReadChan() chan string {
    98  	return s.readChan
    99  }