diff --git a/src/libnmdc/libnmdc.go b/src/libnmdc/libnmdc.go index 427517c..4a990b7 100644 --- a/src/libnmdc/libnmdc.go +++ b/src/libnmdc/libnmdc.go @@ -375,17 +375,6 @@ func (this *HubConnection) worker() { } } - // Maybe we disconnected - if err != nil { - this.State = CONNECTIONSTATE_DISCONNECTED - this.conn = nil - this.connValid = false - this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) - - time.Sleep(30 * time.Second) // Wait before reconnect - continue - } - // Attempt to parse a message block for len(fullBuffer) > 0 { for len(fullBuffer) > 0 && fullBuffer[0] == '|' { @@ -400,6 +389,19 @@ func (this *HubConnection) worker() { } } + // Maybe we disconnected + // Perform this check *last*, to ensure we've had a final shot at + // clearing out any queued messages + if err != nil { + this.State = CONNECTIONSTATE_DISCONNECTED + this.conn = nil + this.connValid = false + this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) + + time.Sleep(30 * time.Second) // Wait before reconnect + continue + } + } }