63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package libnmdc
|
|
|
|
type HubConnectionOptions struct {
|
|
Address HubAddress
|
|
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
|
SkipAutoReconnect bool // as above
|
|
Self UserInfo
|
|
NickPassword string
|
|
|
|
// Returning messages in async mode
|
|
NumEventsToBuffer uint
|
|
|
|
// Returning messages in sync mode
|
|
OnEventSync func(*HubConnection, HubEvent)
|
|
}
|
|
|
|
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
|
if this.Self.ClientTag == "" {
|
|
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
|
}
|
|
|
|
hc := HubConnection{
|
|
Hco: this,
|
|
HubName: DEFAULT_HUB_NAME,
|
|
State: CONNECTIONSTATE_DISCONNECTED,
|
|
users: make(map[string]UserInfo),
|
|
|
|
autoReconnect: !this.SkipAutoReconnect,
|
|
}
|
|
|
|
return &hc
|
|
}
|
|
|
|
// Connects to an NMDC server, and spawns a background goroutine to handle
|
|
// 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 {
|
|
|
|
if this.NumEventsToBuffer < 1 {
|
|
this.NumEventsToBuffer = 1
|
|
}
|
|
|
|
hc := this.prepareConnection()
|
|
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
|
|
hc.processEvent = func(ev HubEvent) {
|
|
hc.OnEvent <- ev
|
|
}
|
|
|
|
go hc.worker()
|
|
return hc
|
|
}
|
|
|
|
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
|
// Client code should supply an event handling function as hco.OnEventSync.
|
|
func (this *HubConnectionOptions) ConnectSync() {
|
|
hc := this.prepareConnection()
|
|
hc.OnEvent = nil
|
|
hc.processEvent = func(ev HubEvent) {
|
|
this.OnEventSync(hc, ev)
|
|
}
|
|
hc.worker()
|
|
}
|