From 25fd476328ca51252430eedf90e60c7b814f40d4 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Wed, 16 Oct 2019 14:44:22 +0200 Subject: [PATCH] Negotiate xmpp websocket subprotocol --- websocket_transport.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/websocket_transport.go b/websocket_transport.go index 26ab511..690bc1d 100644 --- a/websocket_transport.go +++ b/websocket_transport.go @@ -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