diff --git a/HubConnection.go b/HubConnection.go index 126aa28..cec8d47 100644 --- a/HubConnection.go +++ b/HubConnection.go @@ -21,9 +21,8 @@ type HubConnection struct { proto Protocol - // Streamed events + // Event callback processEvent func(HubEvent) - OnEvent chan HubEvent // Private state conn net.Conn // this is an interface diff --git a/HubConnectionOptions.go b/HubConnectionOptions.go index 0cee9bd..997c86e 100644 --- a/HubConnectionOptions.go +++ b/HubConnectionOptions.go @@ -6,12 +6,6 @@ type HubConnectionOptions struct { 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 { @@ -37,32 +31,25 @@ func (this *HubConnectionOptions) prepareConnection() *HubConnection { 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, +// ConnectAsync connects to a hub server, and spawns a background goroutine to handle +// protocol messages. Events will be sent by channel to the supplied onEvent channel, // 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) +func ConnectAsync(opts *HubConnectionOptions, onEvent chan HubEvent) *HubConnection { + hc := opts.prepareConnection() hc.processEvent = func(ev HubEvent) { - hc.OnEvent <- ev + onEvent <- ev } go hc.worker() return hc } -// Connects to an NMDC server, and blocks forever to handle protocol messages. +// ConnectSync connects to a hub 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 +func ConnectSync(opts *HubConnectionOptions, onEvent func(hub *HubConnection, ev HubEvent)) { + hc := opts.prepareConnection() hc.processEvent = func(ev HubEvent) { - this.OnEventSync(hc, ev) + onEvent(hc, ev) } hc.worker() }