// Create a new ajax socket. func newSocket(s *Server) *Socket { a := &Socket{ writeChan: make(chan string, global.WriteChanSize), readChan: make(chan string, global.ReadChanSize), } // Set the closer function. a.closer = closer.New(func() { // Remove the ajax socket from the map. if len(a.uid) > 0 { func() { s.socketsMutex.Lock() defer s.socketsMutex.Unlock() delete(s.sockets, a.uid) }() } // Trigger the onClose function if defined. if a.onClose != nil { a.onClose() } }) return a }
// Create a new websocket value. func newSocket(ws *websocket.Conn) *Socket { w := &Socket{ ws: ws, writeChan: make(chan string, global.WriteChanSize), readChan: make(chan string, global.ReadChanSize), } // Set the closer function. w.closer = closer.New(func() { // Send a close message to the client. // Ignore errors. w.write(websocket.CloseMessage, []byte{}) // Close the socket. w.ws.Close() }) return w }
// Create a new ajax socket. func newAjaxSocket() *ajaxSocket { a := &ajaxSocket{ writeChan: make(chan string, writeChanSize), readChan: make(chan string, readChanSize), } // Set the closer function. a.closer = closer.New(func() { // Remove the ajax socket from the map. if len(a.uid) > 0 { ajaxMutex.Lock() delete(ajaxSockets, a.uid) ajaxMutex.Unlock() } // Trigger the onClose function if defined. if a.onClose != nil { a.onClose() } }) return a }
// Create a new websocket type. func newWebSocket(ws *websocket.Conn) *webSocket { w := &webSocket{ ws: ws, writeChan: make(chan string, writeChanSize), readChan: make(chan string, readChanSize), } // Set the closer function. w.closer = closer.New(func() { // Send a close message to the client. // Ignore errors. w.write(websocket.CloseMessage, []byte{}) // Close the socket. w.ws.Close() // Trigger the onClose function if defined. if w.onClose != nil { w.onClose() } }) return w }