Skip to content

Commit 0b6c485

Browse files
authored
NOISSUE - Fix MQTT WebSocket path (#115)
* fix mqtt websocket route Signed-off-by: Arvindh <arvindh91@gmail.com> * fix mqtt ws example clients Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com>
1 parent c33495e commit 0b6c485

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

examples/client/websocket/with_mtls/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
var (
14-
brokerAddress = "wss://localhost:8085/mqtt"
14+
brokerAddress = "wss://localhost:8085/mgate-ws"
1515
topic = "test/topic"
1616
payload = "Hello mGate"
1717
certFile = "ssl/certs/client.crt"

examples/client/websocket/with_tls/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
var (
14-
brokerAddress = "wss://localhost:8084"
14+
brokerAddress = "wss://localhost:8084/mgate-ws"
1515
topic = "test/topic"
1616
payload = "Hello mGate"
1717
certFile = ""

examples/client/websocket/without_tls/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
var (
14-
brokerAddress = "ws://localhost:8083"
14+
brokerAddress = "ws://localhost:8083/mgate-ws"
1515
topic = "test/topic"
1616
payload = "Hello mGate"
1717
)

pkg/mqtt/websocket/websocket.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/absmach/mgate"
1717
"github.com/absmach/mgate/pkg/session"
1818
mptls "github.com/absmach/mgate/pkg/tls"
19-
"github.com/absmach/mgate/pkg/transport"
2019
"github.com/gorilla/websocket"
2120
"golang.org/x/sync/errgroup"
2221
)
@@ -53,13 +52,10 @@ var upgrader = websocket.Upgrader{
5352
}
5453

5554
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
56-
if !strings.HasPrefix(r.URL.Path, transport.AddSuffixSlash(p.config.PathPrefix+p.config.TargetPath)) {
55+
if !strings.HasPrefix(r.URL.Path, p.config.PathPrefix) {
5756
http.NotFound(w, r)
5857
return
5958
}
60-
61-
r.URL.Path = strings.TrimPrefix(r.URL.Path, p.config.PathPrefix)
62-
6359
cconn, err := upgrader.Upgrade(w, r, nil)
6460
if err != nil {
6561
p.logger.Error("Error upgrading connection", slog.Any("error", err))
@@ -80,7 +76,8 @@ func (p Proxy) pass(in *websocket.Conn) {
8076
dialer := &websocket.Dialer{
8177
Subprotocols: []string{"mqtt"},
8278
}
83-
target := fmt.Sprintf("%s://%s:%s", p.config.TargetProtocol, p.config.TargetHost, p.config.TargetPath)
79+
80+
target := fmt.Sprintf("%s://%s:%s%s", p.config.TargetProtocol, p.config.TargetHost, p.config.TargetPort, p.config.TargetPath)
8481

8582
srv, _, err := dialer.Dial(target, nil)
8683
if err != nil {
@@ -122,7 +119,7 @@ func (p Proxy) Listen(ctx context.Context) error {
122119

123120
mux := http.NewServeMux()
124121

125-
mux.Handle(transport.AddSuffixSlash(p.config.PathPrefix), p)
122+
mux.Handle(p.config.PathPrefix, p)
126123
server.Handler = mux
127124

128125
g.Go(func() error {

0 commit comments

Comments
 (0)