This commit is contained in:
钱纯净
2024-10-14 22:56:54 +08:00
parent f26c8027d0
commit 68a04b7ed7
4 changed files with 66 additions and 7 deletions

View File

@@ -1,8 +1,22 @@
package protocol
type Exchange uint8
func (this Exchange) Uint8() uint8 { return uint8(this) }
const (
Connect = 0x000d //建立连接
Handshake = 0xdb0f //握手
ExchangeSH Exchange = iota //上海交易所
ExchangeSZ //深圳交易所
)
const (
Control = 0x01
)
const (
Connect = 0x000d //建立连接
Handshake = 0xdb0f //握手
SecurityQuote = 0x053e // 行情信息
)
const (

View File

@@ -40,3 +40,31 @@ func DecodeSecurityList(bs []byte) (*SecurityListResp, error) {
return nil, nil
}
func NewConnect() *Frame {
return &Frame{
Control: Control,
Type: Connect,
Data: []byte{0x01},
}
}
func NewSecurityQuotes(m map[Exchange]string) (*Frame, error) {
f := &Frame{
Control: Control,
Type: SecurityQuote,
Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}
payload := Bytes(uint16(len(m)))
for k, v := range m {
if len(v) != 6 {
return nil, errors.New("股票代码长度错误")
}
payload = append(payload, k.Uint8())
payload = append(payload, v...)
}
f.Data = append(f.Data, payload...)
return f, nil
}

21
protocol/model_test.go Normal file
View File

@@ -0,0 +1,21 @@
package protocol
import (
"testing"
)
/*
0c00000000011a001a003e05050000000000000002000030303030303101363030303038
0c02000000011a001a003e05050000000000000002000030303030303101363030303038
*/
func TestNewSecurityQuotes(t *testing.T) {
f, err := NewSecurityQuotes(map[Exchange]string{
ExchangeSH: "000001",
ExchangeSZ: "600008",
})
if err != nil {
t.Error(err)
return
}
t.Log(f.Bytes().HEX())
}