Reestablish the socket when websocket is disconnected. (from @recht matterircd fork)

This commit is contained in:
Wim 2017-07-01 17:49:12 +02:00
parent 003d85772c
commit 877f0fe2e8
1 changed files with 20 additions and 6 deletions

View File

@ -100,10 +100,8 @@ func (m *MMClient) Login() error {
Jitter: true, Jitter: true,
} }
uriScheme := "https://" uriScheme := "https://"
wsScheme := "wss://"
if m.NoTLS { if m.NoTLS {
uriScheme = "http://" uriScheme = "http://"
wsScheme = "ws://"
} }
// login to mattermost // login to mattermost
m.Client = model.NewClient(uriScheme + m.Credentials.Server) m.Client = model.NewClient(uriScheme + m.Credentials.Server)
@ -182,6 +180,24 @@ func (m *MMClient) Login() error {
// set our team id as default route // set our team id as default route
m.Client.SetTeamId(m.Team.Id) m.Client.SetTeamId(m.Team.Id)
m.wsConnect()
return nil
}
func (m *MMClient) wsConnect() {
b := &backoff.Backoff{
Min: time.Second,
Max: 5 * time.Minute,
Jitter: true,
}
m.WsConnected = false
wsScheme := "wss://"
if m.NoTLS {
wsScheme = "ws://"
}
// setup websocket connection // setup websocket connection
wsurl := wsScheme + m.Credentials.Server + model.API_URL_SUFFIX_V3 + "/users/websocket" wsurl := wsScheme + m.Credentials.Server + model.API_URL_SUFFIX_V3 + "/users/websocket"
header := http.Header{} header := http.Header{}
@ -190,6 +206,7 @@ func (m *MMClient) Login() error {
m.log.Debugf("WsClient: making connection: %s", wsurl) m.log.Debugf("WsClient: making connection: %s", wsurl)
for { for {
wsDialer := &websocket.Dialer{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}} wsDialer := &websocket.Dialer{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}}
var err error
m.WsClient, _, err = wsDialer.Dial(wsurl, header) m.WsClient, _, err = wsDialer.Dial(wsurl, header)
if err != nil { if err != nil {
d := b.Duration() d := b.Duration()
@ -199,15 +216,12 @@ func (m *MMClient) Login() error {
} }
break break
} }
b.Reset()
m.log.Debug("WsClient: connected") m.log.Debug("WsClient: connected")
m.WsSequence = 1 m.WsSequence = 1
m.WsPingChan = make(chan *model.WebSocketResponse) m.WsPingChan = make(chan *model.WebSocketResponse)
// only start to parse WS messages when login is completely done // only start to parse WS messages when login is completely done
m.WsConnected = true m.WsConnected = true
return nil
} }
func (m *MMClient) Logout() error { func (m *MMClient) Logout() error {
@ -240,7 +254,7 @@ func (m *MMClient) WsReceiver() {
if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil { if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil {
m.log.Error("error:", err) m.log.Error("error:", err)
// reconnect // reconnect
m.Login() m.wsConnect()
} }
var event model.WebSocketEvent var event model.WebSocketEvent