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 (
"context"
"errors"
"net"
"strings"
"time"
@ -11,6 +12,8 @@ import (
const pingTimeout = time.Duration(5) * time.Second
var ServerDoesNotSupportXmppOverWebsocket = errors.New("The websocket server does not support the xmpp subprotocol")
type WebsocketTransport struct {
Config TransportConfiguration
wsConn *websocket.Conn
@ -27,10 +30,15 @@ func (t *WebsocketTransport) Connect() error {
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 {
return NewConnError(err, true)
}
if response.Header.Get("Sec-WebSocket-Protocol") != "xmpp" {
return ServerDoesNotSupportXmppOverWebsocket
}
t.wsConn = wsConn
t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText)
return nil