diff options
author | Wim <wim@42.be> | 2017-07-01 17:49:12 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-07-01 17:49:12 +0200 |
commit | 877f0fe2e82ab7f502eb0578dadf0c60b7a472fe (patch) | |
tree | 4b7c22cbaafe5ba7cf339c0d43789802c5add0f9 /matterclient/matterclient.go | |
parent | 003d85772c9e28696dfca82b2eb4f65f232a8e61 (diff) | |
download | matterbridge-msglm-877f0fe2e82ab7f502eb0578dadf0c60b7a472fe.tar.gz matterbridge-msglm-877f0fe2e82ab7f502eb0578dadf0c60b7a472fe.tar.bz2 matterbridge-msglm-877f0fe2e82ab7f502eb0578dadf0c60b7a472fe.zip |
Reestablish the socket when websocket is disconnected. (from @recht matterircd fork)
Diffstat (limited to 'matterclient/matterclient.go')
-rw-r--r-- | matterclient/matterclient.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index de2aa947..afcedcd6 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -100,10 +100,8 @@ func (m *MMClient) Login() error { Jitter: true, } uriScheme := "https://" - wsScheme := "wss://" if m.NoTLS { uriScheme = "http://" - wsScheme = "ws://" } // login to mattermost m.Client = model.NewClient(uriScheme + m.Credentials.Server) @@ -182,6 +180,24 @@ func (m *MMClient) Login() error { // set our team id as default route 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 wsurl := wsScheme + m.Credentials.Server + model.API_URL_SUFFIX_V3 + "/users/websocket" header := http.Header{} @@ -190,6 +206,7 @@ func (m *MMClient) Login() error { m.log.Debugf("WsClient: making connection: %s", wsurl) for { wsDialer := &websocket.Dialer{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}} + var err error m.WsClient, _, err = wsDialer.Dial(wsurl, header) if err != nil { d := b.Duration() @@ -199,15 +216,12 @@ func (m *MMClient) Login() error { } break } - b.Reset() m.log.Debug("WsClient: connected") m.WsSequence = 1 m.WsPingChan = make(chan *model.WebSocketResponse) // only start to parse WS messages when login is completely done m.WsConnected = true - - return nil } func (m *MMClient) Logout() error { @@ -240,7 +254,7 @@ func (m *MMClient) WsReceiver() { if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil { m.log.Error("error:", err) // reconnect - m.Login() + m.wsConnect() } var event model.WebSocketEvent |