增加心跳,30秒1次,超过60秒无数据交互,服务器会断开连接

This commit is contained in:
injoyai
2024-10-28 16:30:52 +08:00
parent c5cfc2bc98
commit 7c287564a3
2 changed files with 45 additions and 20 deletions

View File

@@ -23,6 +23,12 @@ func Dial(addr string, op ...client.Option) (cli *Client, err error) {
c.SetOption(op...) //自定义选项 c.SetOption(op...) //自定义选项
c.Event.OnReadFrom = protocol.ReadFrom //分包 c.Event.OnReadFrom = protocol.ReadFrom //分包
c.Event.OnDealMessage = cli.handlerDealMessage //处理分包数据 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 { if err != nil {
return nil, err return nil, err
@@ -45,6 +51,11 @@ type Client struct {
msgID uint32 msgID uint32
} }
// Done 连接关闭
func (this *Client) Done() <-chan struct{} {
return this.c.Done()
}
// handlerDealMessage 处理服务器响应的数据 // handlerDealMessage 处理服务器响应的数据
func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) { 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 var resp any
switch f.Type { 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) resp = protocol.MStockQuote.Decode(f.Data)
case protocol.TypeSecurityList: case protocol.TypeStockMinute:
resp, err = protocol.MStockList.Decode(f.Data) 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)) 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,向服务器写入数据 // Write 实现io.Writer,向服务器写入数据
func (this *Client) Write(bs []byte) (int, error) { func (this *Client) Write(bs []byte) (int, error) {
return this.c.Write(bs) return this.c.Write(bs)
@@ -105,19 +114,18 @@ func (this *Client) connect() error {
return err return err
} }
// GetSecurityList 获取市场内指定范围内的所有证券代码 // GetStockList 获取市场内指定范围内的所有证券代码
func (this *Client) GetSecurityList(exchange protocol.Exchange, starts ...uint16) (*protocol.StockListResp, error) { func (this *Client) GetStockList(exchange protocol.Exchange, starts ...uint16) (*protocol.StockListResp, error) {
f := protocol.MStockList.Frame(exchange, starts...) f := protocol.MStockList.Frame(exchange, starts...)
result, err := this.SendFrame(f) result, err := this.SendFrame(f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return result.(*protocol.StockListResp), nil return result.(*protocol.StockListResp), nil
} }
// GetSecurityQuotes 获取盘口五档报价 // GetStockQuotes 获取盘口五档报价
func (this *Client) GetSecurityQuotes(m map[protocol.Exchange]string) (protocol.StockQuotesResp, error) { func (this *Client) GetStockQuotes(m map[protocol.Exchange]string) (protocol.StockQuotesResp, error) {
f, err := protocol.MStockQuote.Frame(m) f, err := protocol.MStockQuote.Frame(m)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -128,3 +136,16 @@ func (this *Client) GetSecurityQuotes(m map[protocol.Exchange]string) (protocol.
} }
return result.(protocol.StockQuotesResp), nil 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
}

View File

@@ -1,12 +1,15 @@
package protocol package protocol
const ( const (
TypeConnect = 0x000d //建立连接 TypeConnect = 0x000D //建立连接
TypeHandshake = 0xdb0f //握手 TypeHeart = 0x0004 //心跳
TypeSecurityList = 0x0450 //获取股票代码 TypeStockList = 0x0450 //获取股票代码
TypeSecurityQuote = 0x053e // 行情信息 TypeStockQuote = 0x053E //行情信息
TypeStockMinute = 0x051D //分时数据
) )
/*
从其他地方复制
const ( const (
LOGIN_ONE = 0x000d //第一次登录 LOGIN_ONE = 0x000d //第一次登录
LOGIN_TWO = 0x0fdb //第二次登录 LOGIN_TWO = 0x0fdb //第二次登录
@@ -65,3 +68,4 @@ const (
KLINE_TYPE_3MONTH = 10 // 季K 线 KLINE_TYPE_3MONTH = 10 // 季K 线
KLINE_TYPE_YEARLY = 11 // 年K 线 KLINE_TYPE_YEARLY = 11 // 年K 线
) )
*/