From ba15956d22fc5bfe9dd6caaefcc013cd29da38b7 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Thu, 11 Apr 2019 10:57:14 -0700 Subject: [PATCH] Make onclose an option. Signed-off-by: Lantao Liu --- client.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 41e83c2..35ca91f 100644 --- a/client.go +++ b/client.go @@ -49,7 +49,15 @@ type Client struct { 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{ codec: codec{}, conn: conn, @@ -60,6 +68,10 @@ func NewClient(conn net.Conn) *Client { closeFunc: func() {}, } + for _, o := range opts { + o(c) + } + go c.run() return c } @@ -141,11 +153,6 @@ func (c *Client) Close() error { 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 { messageHeader p []byte