option to disable autoreconnect, move magic to constant
This commit is contained in:
parent
8e4a45a2b9
commit
43db3a0d64
@ -21,9 +21,10 @@ type HubConnection struct {
|
|||||||
OnEvent chan HubEvent
|
OnEvent chan HubEvent
|
||||||
|
|
||||||
// Private state
|
// Private state
|
||||||
conn net.Conn // this is an interface
|
conn net.Conn // this is an interface
|
||||||
connValid bool
|
connValid bool
|
||||||
sentOurHello bool
|
sentOurHello bool
|
||||||
|
autoReconnect bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HubConnection) SayPublic(message string) {
|
func (this *HubConnection) SayPublic(message string) {
|
||||||
@ -268,8 +269,12 @@ func (this *HubConnection) worker() {
|
|||||||
this.connValid = false
|
this.connValid = false
|
||||||
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
|
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
|
||||||
|
|
||||||
time.Sleep(30 * time.Second) // Wait before reconnect
|
if this.autoReconnect {
|
||||||
continue
|
time.Sleep(AUTO_RECONNECT_AFTER) // Wait before reconnect
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
return // leave the worker for good
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package libnmdc
|
package libnmdc
|
||||||
|
|
||||||
type HubConnectionOptions struct {
|
type HubConnectionOptions struct {
|
||||||
Address HubAddress
|
Address HubAddress
|
||||||
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
||||||
Self UserInfo
|
SkipAutoReconnect bool // as above
|
||||||
NickPassword string
|
Self UserInfo
|
||||||
|
NickPassword string
|
||||||
|
|
||||||
// Returning messages in async mode
|
// Returning messages in async mode
|
||||||
NumEventsToBuffer uint
|
NumEventsToBuffer uint
|
||||||
@ -23,13 +24,16 @@ func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
|||||||
HubName: DEFAULT_HUB_NAME,
|
HubName: DEFAULT_HUB_NAME,
|
||||||
State: CONNECTIONSTATE_DISCONNECTED,
|
State: CONNECTIONSTATE_DISCONNECTED,
|
||||||
Users: make(map[string]UserInfo),
|
Users: make(map[string]UserInfo),
|
||||||
|
|
||||||
|
autoReconnect: !this.SkipAutoReconnect,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &hc
|
return &hc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connects to an NMDC server, and spawns a background goroutine to handle
|
// Connects to an NMDC server, and spawns a background goroutine to handle
|
||||||
// protocol messages. Client code should select on all the interface channels.
|
// protocol messages. Events will be sent by channel to the returned hc.OnEvent,
|
||||||
|
// the client is responsible for selecting off this.
|
||||||
func (this *HubConnectionOptions) Connect() *HubConnection {
|
func (this *HubConnectionOptions) Connect() *HubConnection {
|
||||||
|
|
||||||
if this.NumEventsToBuffer < 1 {
|
if this.NumEventsToBuffer < 1 {
|
||||||
@ -47,7 +51,7 @@ func (this *HubConnectionOptions) Connect() *HubConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
||||||
// Client code should supply an event handling function.
|
// Client code should supply an event handling function as hco.OnEventSync.
|
||||||
func (this *HubConnectionOptions) ConnectSync() {
|
func (this *HubConnectionOptions) ConnectSync() {
|
||||||
hc := this.prepareConnection()
|
hc := this.prepareConnection()
|
||||||
hc.worker()
|
hc.worker()
|
||||||
|
@ -13,6 +13,7 @@ const (
|
|||||||
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
||||||
DEFAULT_HUB_NAME string = "(unknown)"
|
DEFAULT_HUB_NAME string = "(unknown)"
|
||||||
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
||||||
|
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
var rx_protocolMessage *regexp.Regexp
|
var rx_protocolMessage *regexp.Regexp
|
||||||
|
Loading…
Reference in New Issue
Block a user