DrmagicE / gmqtt

Gmqtt is a flexible, high-performance MQTT broker library that fully implements the MQTT protocol V3.x and V5 in golang
MIT License
989 stars 220 forks source link

how to setup gmqtt to support tls/ssl #157

Closed champ-isaac closed 2 years ago

champ-isaac commented 2 years ago

in my config.yaml file,

listeners:
  - address: ":1883"
    tls:
      cacert: <cacert file path>
      cert: <cert file path>
      key: <cert key file path>
      verify: true
    websocket:
      path: "/"
......

After gmqtt server beginning, I try to connect to this url wss://serverip:8883, but it's failed to connect to this url. However, if I connect to ws://serverrip:8883, it's work.

How can I connect to gmqtt server with TLS?

champ-isaac commented 2 years ago

Sorry about that guys!

I forget to config websocket TLSOptions.....Orz

All you need to do is setup websocket TLSOptions, for example

ws := server.WsServer{
        Server: &http.Server{Addr: ":8883"},
        Path:   listener.Websocket.Path,
}
// setup TLSOptions
if listener.TLSOptions != nil {
        ws.CertFile = listener.TLSOptions.Cert //cert file path
        ws.KeyFile = listener.TLSOptions.Key //cert key file path
}

srv := server.New(
    server.WithTCPListener(ln),
    server.WithWebsocketServer(&ws),
    server.WithLogger(l),
    server.WithConfig(c))