@@ -26,9 +26,9 @@ type CheckClientHandler func(id string, r *http.Request) bool
2626//
2727// server := NewServer()
2828//
29- // If you need a TLS ws server instead, use :
29+ // If you need a server with TLS support, pass the following option :
3030//
31- // server := NewTLSServer( "cert.pem", "privateKey.pem")
31+ // server := NewServer(WithServerTLSConfig( "cert.pem", "privateKey.pem", nil) )
3232//
3333// To support client basic authentication, use:
3434//
@@ -125,7 +125,7 @@ type Server interface {
125125
126126// Default implementation of a Websocket server.
127127//
128- // Use the NewServer or NewTLSServer functions to create a new server.
128+ // Use the NewServer function to create a new server.
129129type server struct {
130130 connections map [string ]* webSocket
131131 httpServer * http.Server
@@ -144,43 +144,50 @@ type server struct {
144144 httpHandler * mux.Router
145145}
146146
147- // Creates a new simple websocket server (the websockets are not secured).
148- func NewServer () Server {
149- router := mux .NewRouter ()
150- return & server {
151- httpServer : & http.Server {},
152- timeoutConfig : NewServerTimeoutConfig (),
153- upgrader : websocket.Upgrader {Subprotocols : []string {}},
154- httpHandler : router ,
147+ // ServerOpt is a function that can be used to set options on a server during creation.
148+ type ServerOpt func (s * server )
149+
150+ // WithServerTLSConfig sets the TLS configuration for the server.
151+ // If the passed tlsConfig is nil, the client will not use TLS.
152+ func WithServerTLSConfig (certificatePath string , certificateKey string , tlsConfig * tls.Config ) ServerOpt {
153+ return func (s * server ) {
154+ s .tlsCertificatePath = certificatePath
155+ s .tlsCertificateKey = certificateKey
156+ if tlsConfig != nil {
157+ s .httpServer .TLSConfig = tlsConfig
158+ }
155159 }
156160}
157161
158- // NewTLSServer creates a new secure websocket server. All created websocket channels will use TLS.
162+ // NewServer Creates a new websocket server.
163+ //
164+ // Additional options may be added using the AddOption function.
159165//
160- // You need to pass a filepath to the server TLS certificate and key .
166+ // By default, the websockets are not secure, and the server will not perform any client certificate verification .
161167//
162- // It is recommended to pass a valid TLSConfig for the server to use.
163- // For example to require client certificate verification:
168+ // To add TLS support to the server, a valid server certificate path and key must be passed.
169+ // To also add support for client certificate verification, a valid TLSConfig needs to be configured.
170+ // For example:
164171//
165- // tlsConfig := &tls.Config{
166- // ClientAuth: tls.RequireAndVerifyClientCert,
167- // ClientCAs: clientCAs,
168- // }
172+ // tlsConfig := &tls.Config{
173+ // ClientAuth: tls.RequireAndVerifyClientCert,
174+ // ClientCAs: clientCAs,
175+ // }
176+ // server := ws.NewServer(ws.WithServerTLSConfig("cert.pem", "privateKey.pem", tlsConfig))
169177//
170- // If no tlsConfig parameter is passed, the server will by default
171- // not perform any client certificate verification.
172- func NewTLSServer (certificatePath string , certificateKey string , tlsConfig * tls.Config ) Server {
178+ // When TLS is correctly configured, the server will automatically use it for all created websocket channels.
179+ func NewServer (opts ... ServerOpt ) Server {
173180 router := mux .NewRouter ()
174- return & server {
175- tlsCertificatePath : certificatePath ,
176- tlsCertificateKey : certificateKey ,
177- httpServer : & http.Server {
178- TLSConfig : tlsConfig ,
179- },
181+ s := & server {
182+ httpServer : & http.Server {},
180183 timeoutConfig : NewServerTimeoutConfig (),
181184 upgrader : websocket.Upgrader {Subprotocols : []string {}},
182185 httpHandler : router ,
183186 }
187+ for _ , o := range opts {
188+ o (s )
189+ }
190+ return s
184191}
185192
186193func (s * server ) SetMessageHandler (handler MessageHandler ) {
0 commit comments