func (s *Socket) writeMessagesSync(ctx context.Context, wg *sync.WaitGroup, in <-chan message.Message, pingTicker, httpPingTicker *time.Ticker) {
	defer wg.Done()
	defer s.closeConn() // will cause readMessages() to fail
	skipWrite, stopWrite := false, false
	write := func(writeFunc func() error) error {
		if skipWrite {
			return fmt.Errorf("skipping write for socket (%v) because an error has already occurred", s)
		}
		if err := s.refreshDeadline(s.Conn.SetWriteDeadline, s.WriteWait); err != nil {
			return fmt.Errorf("setting write deadline: %v", err)
		}
		return writeFunc()
	}
	var err error
	for { // BLOCKING
		select {
		case <-ctx.Done():
			s.writeClose(errServerShuttingDown)
			return
		case m, ok := <-in:
			switch {
			case !ok:
				err = errSocketClosed
				stopWrite = true
			default:
				err = write(func() error { return s.writeMessage(m) })
			}
		case <-pingTicker.C:
			err = write(s.Conn.WritePing)
		case <-httpPingTicker.C:
			m := message.Message{
				Type: message.SocketHTTPPing,
			}
			err = write(func() error { return s.writeMessage(m) })
		}
		if err != nil {
			s.writeClose(err)
			s.closeConn() // will cause readMessages() to fail
			if stopWrite {
				return
			}
			skipWrite = true
		}
	}
}
// writeMessagesSync sends messages from the outbound messages channel to the connected socket.
// Messages are not sent if the context is cancelled or an error is encountered and sent to the error channel.
// NOTE: this function does not terminate until the input channel closes.
// The tickers are used to periodically write message different ping messages.
writeMessagesSync sends messages from the outbound messages channel to the connected socket.
Messages are not sent if the context is cancelled or an error is encountered and sent to the error channel.
NOTE: this function does not terminate until the input channel closes.
The tickers are used to periodically write message different ping messages.
// writeMessagesSync sends messages from the outbound messages channel to the connected socket . // Messages are not sent if the context is cancelled or an error is encountered and sent to the error channel . // NOTE : this function does not terminate until the input channel closes . // The tickers are used to periodically write message different ping messages .
func (s *Socket) writeMessagesSync(ctx context.Context, wg *sync.WaitGroup, in <-chan message.Message, pingTicker, httpPingTicker *time.Ticker) {
	defer wg.Done()
	defer s.closeConn() 
	skipWrite, stopWrite := false, false
	write := func(writeFunc func() error) error {
		if skipWrite {
			return fmt.Errorf("skipping write for socket (%v) because an error has already occurred", s)
		}
		if err := s.refreshDeadline(s.Conn.SetWriteDeadline, s.WriteWait); err != nil {
			return fmt.Errorf("setting write deadline: %v", err)
		}
		return writeFunc()
	}
	var err error
	for { 
		select {
		case <-ctx.Done():
			s.writeClose(errServerShuttingDown)
			return
		case m, ok := <-in:
			switch {
			case !ok:
				err = errSocketClosed
				stopWrite = true
			default:
				err = write(func() error { return s.writeMessage(m) })
			}
		case <-pingTicker.C:
			err = write(s.Conn.WritePing)
		case <-httpPingTicker.C:
			m := message.Message{
				Type: message.SocketHTTPPing,
			}
			err = write(func() error { return s.writeMessage(m) })
		}
		if err != nil {
			s.writeClose(err)
			s.closeConn() 
			if stopWrite {
				return
			}
			skipWrite = true
		}
	}
}
func ( s * Socket ) writeMessagesSync ( ctx context . Context , wg * sync . WaitGroup , in <- chan message . Message , pingTicker , httpPingTicker * time . Ticker ) { defer wg . Done ( ) defer s . closeConn ( ) skipWrite , stopWrite := false , false write := func ( writeFunc func ( ) error ) error { if skipWrite { return fmt . Errorf ( " " , s ) } if err := s . refreshDeadline ( s . Conn . SetWriteDeadline , s . WriteWait ) ; err != nil { return fmt . Errorf ( " " , err ) } return writeFunc ( ) } var err error for { select { case <- ctx . Done ( ) : s . writeClose ( errServerShuttingDown ) return case m , ok := <- in : switch { case ! ok : err = errSocketClosed stopWrite = true default : err = write ( func ( ) error { return s . writeMessage ( m ) } ) } case <- pingTicker . C : err = write ( s . Conn . WritePing ) case <- httpPingTicker . C : m := message . Message { Type : message . SocketHTTPPing , } err = write ( func ( ) error { return s . writeMessage ( m ) } ) } if err != nil { s . writeClose ( err ) s . closeConn ( ) if stopWrite { return } skipWrite = true } } }
writeMessagesSync sends messages from the outbound messages channel to the connected socket.
writeMessagesSync sends messages from the outbound messages channel to the connected socket .
// will cause readMessages() to fail // BLOCKING // will cause readMessages() to fail