From 7c287564a39508f4f3a7478d3d55f5c7f6674108 Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Mon, 28 Oct 2024 16:30:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BF=83=E8=B7=B3,30?= =?UTF-8?q?=E7=A7=921=E6=AC=A1,=E8=B6=85=E8=BF=8760=E7=A7=92=E6=97=A0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BA=A4=E4=BA=92,=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E4=BC=9A=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 53 +++++++++++++++++++++++++++++++++-------------- protocol/const.go | 12 +++++++---- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/client.go b/client.go index e8f994f..f31cd1e 100644 --- a/client.go +++ b/client.go @@ -23,6 +23,12 @@ func Dial(addr string, op ...client.Option) (cli *Client, err error) { c.SetOption(op...) //自定义选项 c.Event.OnReadFrom = protocol.ReadFrom //分包 c.Event.OnDealMessage = cli.handlerDealMessage //处理分包数据 + //无数据超时时间是60秒 + c.GoTimerWriter(30*time.Second, func(w ios.MoreWriter) error { + bs := protocol.MHeart.Frame().Bytes() + _, err := w.Write(bs) + return err + }) }) if err != nil { return nil, err @@ -45,6 +51,11 @@ type Client struct { msgID uint32 } +// Done 连接关闭 +func (this *Client) Done() <-chan struct{} { + return this.c.Done() +} + // handlerDealMessage 处理服务器响应的数据 func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) { @@ -56,11 +67,17 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) { var resp any switch f.Type { - case protocol.TypeSecurityQuote: + + case protocol.TypeConnect: + + case protocol.TypeStockList: + resp, err = protocol.MStockList.Decode(f.Data) + + case protocol.TypeStockQuote: resp = protocol.MStockQuote.Decode(f.Data) - case protocol.TypeSecurityList: - resp, err = protocol.MStockList.Decode(f.Data) + case protocol.TypeStockMinute: + resp, err = protocol.MStockMinute.Decode(f.Data) } @@ -82,14 +99,6 @@ func (this *Client) SendFrame(f *protocol.Frame) (any, error) { return this.w.Wait(conv.String(this.msgID)) } -// Send 向服务发送数据,并等待响应数据 -func (this *Client) Send(bs []byte) (any, error) { - if _, err := this.c.Write(bs); err != nil { - return nil, err - } - return this.w.Wait(conv.String(this.msgID)) -} - // Write 实现io.Writer,向服务器写入数据 func (this *Client) Write(bs []byte) (int, error) { return this.c.Write(bs) @@ -105,19 +114,18 @@ func (this *Client) connect() error { return err } -// GetSecurityList 获取市场内指定范围内的所有证券代码 -func (this *Client) GetSecurityList(exchange protocol.Exchange, starts ...uint16) (*protocol.StockListResp, error) { +// GetStockList 获取市场内指定范围内的所有证券代码 +func (this *Client) GetStockList(exchange protocol.Exchange, starts ...uint16) (*protocol.StockListResp, error) { f := protocol.MStockList.Frame(exchange, starts...) result, err := this.SendFrame(f) if err != nil { return nil, err } return result.(*protocol.StockListResp), nil - } -// GetSecurityQuotes 获取盘口五档报价 -func (this *Client) GetSecurityQuotes(m map[protocol.Exchange]string) (protocol.StockQuotesResp, error) { +// GetStockQuotes 获取盘口五档报价 +func (this *Client) GetStockQuotes(m map[protocol.Exchange]string) (protocol.StockQuotesResp, error) { f, err := protocol.MStockQuote.Frame(m) if err != nil { return nil, err @@ -128,3 +136,16 @@ func (this *Client) GetSecurityQuotes(m map[protocol.Exchange]string) (protocol. } return result.(protocol.StockQuotesResp), nil } + +// GetStockMinute 获取分时数据 +func (this *Client) GetStockMinute(exchange protocol.Exchange, code string) (*protocol.StockMinuteResp, error) { + f, err := protocol.MStockMinute.Frame(exchange, code) + if err != nil { + return nil, err + } + result, err := this.SendFrame(f) + if err != nil { + return nil, err + } + return result.(*protocol.StockMinuteResp), nil +} diff --git a/protocol/const.go b/protocol/const.go index 0e1876e..0f79638 100644 --- a/protocol/const.go +++ b/protocol/const.go @@ -1,12 +1,15 @@ package protocol const ( - TypeConnect = 0x000d //建立连接 - TypeHandshake = 0xdb0f //握手 - TypeSecurityList = 0x0450 //获取股票代码 - TypeSecurityQuote = 0x053e // 行情信息 + TypeConnect = 0x000D //建立连接 + TypeHeart = 0x0004 //心跳 + TypeStockList = 0x0450 //获取股票代码 + TypeStockQuote = 0x053E //行情信息 + TypeStockMinute = 0x051D //分时数据 ) +/* +从其他地方复制 const ( LOGIN_ONE = 0x000d //第一次登录 LOGIN_TWO = 0x0fdb //第二次登录 @@ -65,3 +68,4 @@ const ( KLINE_TYPE_3MONTH = 10 // 季K 线 KLINE_TYPE_YEARLY = 11 // 年K 线 ) +*/