Negotiate xmpp websocket subprotocol

This commit is contained in:
Wichert Akkerman 2019-10-16 14:44:22 +02:00 committed by Mickaël Rémond
parent 36e153f981
commit 25fd476328

View file

@ -2,6 +2,7 @@ package xmpp
import ( import (
"context" "context"
"errors"
"net" "net"
"strings" "strings"
"time" "time"
@ -11,6 +12,8 @@ import (
const pingTimeout = time.Duration(5) * time.Second const pingTimeout = time.Duration(5) * time.Second
var ServerDoesNotSupportXmppOverWebsocket = errors.New("The websocket server does not support the xmpp subprotocol")
type WebsocketTransport struct { type WebsocketTransport struct {
Config TransportConfiguration Config TransportConfiguration
wsConn *websocket.Conn wsConn *websocket.Conn
@ -27,10 +30,15 @@ func (t *WebsocketTransport) Connect() error {
defer cancel() defer cancel()
} }
wsConn, _, err := websocket.Dial(t.ctx, t.Config.Address, nil) wsConn, response, err := websocket.Dial(t.ctx, t.Config.Address, &websocket.DialOptions{
Subprotocols: []string{"xmpp"},
})
if err != nil { if err != nil {
return NewConnError(err, true) return NewConnError(err, true)
} }
if response.Header.Get("Sec-WebSocket-Protocol") != "xmpp" {
return ServerDoesNotSupportXmppOverWebsocket
}
t.wsConn = wsConn t.wsConn = wsConn
t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText) t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText)
return nil return nil