Skip to content

Commit ba15956

Browse files
committed
Make onclose an option.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 6914432 commit ba15956

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

client.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ type Client struct {
4949
err error
5050
}
5151

52-
func NewClient(conn net.Conn) *Client {
52+
type ClientOpts func(c *Client)
53+
54+
func WithOnClose(onClose func()) ClientOpts {
55+
return func(c *Client) {
56+
c.closeFunc = onClose
57+
}
58+
}
59+
60+
func NewClient(conn net.Conn, opts ...ClientOpts) *Client {
5361
c := &Client{
5462
codec: codec{},
5563
conn: conn,
@@ -60,6 +68,10 @@ func NewClient(conn net.Conn) *Client {
6068
closeFunc: func() {},
6169
}
6270

71+
for _, o := range opts {
72+
o(c)
73+
}
74+
6375
go c.run()
6476
return c
6577
}
@@ -141,11 +153,6 @@ func (c *Client) Close() error {
141153
return nil
142154
}
143155

144-
// OnClose allows a close func to be called when the server is closed
145-
func (c *Client) OnClose(closer func()) {
146-
c.closeFunc = closer
147-
}
148-
149156
type message struct {
150157
messageHeader
151158
p []byte

0 commit comments

Comments
 (0)