Merge pull request #35 from Random-Liu/make-on-close-an-options

Make onclose an option.
This commit is contained in:
Michael Crosby 2019-04-11 14:14:08 -04:00 committed by GitHub
commit 699c4e40d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,7 +49,15 @@ type Client struct {
err error err error
} }
func NewClient(conn net.Conn) *Client { type ClientOpts func(c *Client)
func WithOnClose(onClose func()) ClientOpts {
return func(c *Client) {
c.closeFunc = onClose
}
}
func NewClient(conn net.Conn, opts ...ClientOpts) *Client {
c := &Client{ c := &Client{
codec: codec{}, codec: codec{},
conn: conn, conn: conn,
@ -60,6 +68,10 @@ func NewClient(conn net.Conn) *Client {
closeFunc: func() {}, closeFunc: func() {},
} }
for _, o := range opts {
o(c)
}
go c.run() go c.run()
return c return c
} }
@ -141,11 +153,6 @@ func (c *Client) Close() error {
return nil return nil
} }
// OnClose allows a close func to be called when the server is closed
func (c *Client) OnClose(closer func()) {
c.closeFunc = closer
}
type message struct { type message struct {
messageHeader messageHeader
p []byte p []byte