github.com/golazy/golazy@v0.0.7-0.20221012133820-968fe65a0b65/lazydev/protocolmux/README.md (about)

     1  # protocolmux
     2  
     3  protocolmux allows to peek the first bytes of a connection and decided to which handler forward the connection.
     4  
     5  ```go
     6  l, _ := net.Listen("tcp", addr)
     7  
     8  // Initialize the muuxer
     9  mux := &Mux{L: l}
    10  
    11  // Create listeners just by setting the prefix
    12  helloListener := mux.ListenTo([][]byte{[]byte("ping")})
    13  
    14  for {
    15  	conn, _ := helloListener.Accept()
    16  	conn.Write("pong")
    17  	conn.Close();
    18  }
    19  
    20  // Or use one of the prefixes
    21  go http.Serve(mux.ListenTo(HttpPrefix), nil) // Handle HTTP
    22  go http.ServeTLS(mux.ListenTo(TLSPrefix), nil,...) // Handle HTTPS
    23  ```
    24  
    25  ## Variables
    26  
    27  ```golang
    28  var (
    29      HTTPPrefix = [][]byte{
    30          []byte("GET"),
    31          []byte("HEAD"),
    32          []byte("POST"),
    33          []byte("PUT"),
    34          []byte("DELETE"),
    35          []byte("CONNECT"),
    36          []byte("OPTIONS"),
    37          []byte("TRACE"),
    38          []byte("PATCH"),
    39      }
    40      TLSPrefix = [][]byte{
    41          {22, 3, 0},
    42          {22, 3, 1},
    43          {22, 3, 2},
    44          {22, 3, 3},
    45      }
    46  )
    47  ```
    48  
    49  ## Types
    50  
    51  ### type [Mux](/protocolmux.go#L41)
    52  
    53  `type Mux struct { ... }`
    54  
    55  #### func (*Mux) [Listen](/protocolmux.go#L66)
    56  
    57  `func (m *Mux) Listen() error`
    58  
    59  #### func (*Mux) [ListenTo](/protocolmux.go#L51)
    60  
    61  `func (m *Mux) ListenTo(prefixes [][]byte) net.Listener`
    62  
    63  ---
    64  Readme created from Go doc with [goreadme](https://github.com/posener/goreadme)