• level2行情websocket订阅


    行情接入示例

    PYTHON

    1. #!python3
    2. # -*- coding:utf-8 -*-
    3. import time
    4. import websocket
    5. import zlib
    6. # 发送订阅
    7. def on_open(ws):
    8. ws.send("all=lv2_600519,lv1_000001")
    9. # 接收推送
    10. def on_message(ws, message, type, flag):
    11. # 命令返回文本消息
    12. if type == websocket.ABNF.OPCODE_TEXT:
    13. print(time.strftime('%H:%M:%S', time.localtime(time.time())), "Text响应:", message)
    14. # 行情推送压缩二进制消息,在此解压缩
    15. if type == websocket.ABNF.OPCODE_BINARY:
    16. rb = zlib.decompress(message, -zlib.MAX_WBITS)
    17. print(time.strftime('%H:%M:%S', time.localtime(time.time())), "Binary响应:", rb.decode("utf-8"))
    18. def on_error(ws, error):
    19. print(error)
    20. def on_close(ws, code, msg):
    21. print(time.strftime('%H:%M:%S', time.localtime(time.time())), "连接已断开")
    22. wsUrl = "ws://<服务器地址>?token="
    23. ws = websocket.WebSocketApp(wsUrl,
    24. on_open=on_open,
    25. on_data=on_message,
    26. on_error=on_error,
    27. on_close=on_close)
    28. ws.run_forever()

    Copy

    GOLANG

    1. package main
    2. import (
    3. "bytes"
    4. "compress/flate"
    5. "github.com/gorilla/websocket"
    6. "log"
    7. "time"
    8. )
    9. func main() {
    10. //连接地址
    11. wsUrl := "ws://<服务器地址>?token="
    12. conn, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
    13. if err != nil {
    14. log.Fatalln("连接错误:", err)
    15. }
    16. //接收协程
    17. go func() {
    18. receive(conn)
    19. }()
    20. //发送订阅
    21. cmd := "all=lv2_600519,lv1_000001"
    22. err = conn.WriteMessage(websocket.TextMessage, []byte(cmd))
    23. if err != nil {
    24. log.Fatalln("发送指令错误:", err)
    25. }
    26. log.Println("发送指令成功,等待接收")
    27. for {
    28. time.Sleep(time.Second)
    29. }
    30. }
    31. func receive(conn *websocket.Conn) {
    32. for {
    33. //阻塞接收
    34. messageType, rb, err := conn.ReadMessage()
    35. if err != nil {
    36. log.Fatalln("接收错误:", err)
    37. return
    38. }
    39. //文本消息
    40. if messageType == websocket.TextMessage {
    41. log.Println("Text响应:", string(rb))
    42. }
    43. //二进制消息
    44. if messageType == websocket.BinaryMessage {
    45. unZipByte := DeCompress(rb)
    46. log.Println("Binary推送:", string(unZipByte))
    47. }
    48. }
    49. }
    50. //解压方法
    51. func DeCompress(b []byte) []byte {
    52. var buffer bytes.Buffer
    53. buffer.Write([]byte(b))
    54. reader := flate.NewReader(&buffer)
    55. var result bytes.Buffer
    56. result.ReadFrom(reader)
    57. reader.Close()
    58. return result.Bytes()
    59. }

    Copy

    PHP

    1. //需安装swoole扩展
    2. use Swoole\Coroutine\Http\Client;
    3. use function Swoole\Coroutine\run;
    4. run(function () {
    5. //服务器地址
    6. $host = '<服务器地址>';
    7. //服务器端口
    8. $port = <服务器端口>;
    9. //连接
    10. $conn = new Client($host, $port);
    11. $conn->upgrade("/?token=");
    12. //发送订阅
    13. $conn->push("add=lv2_600519,lv1_000001");
    14. //开启接收协程
    15. go("receive", $conn);
    16. });
    17. function receive($client)
    18. {
    19. while (true) {
    20. $data = $client->recv();
    21. $time = date("H:i:s");
    22. //解压
    23. @$zipStr = gzinflate($data->data);
    24. if ($zipStr) {
    25. echo "{$time} Binary推送:{$zipStr}\n";
    26. } else {
    27. echo "{$time} Text响应:{$data->data}\n";
    28. }
    29. }
    30. }

    官方wiki:jvQuant行情交易接口文档 

  • 相关阅读:
    express日志模块Morgan
    vue 中 mixin 和 mixins 区别
    淘宝/天猫,1688,拼多多,京东店铺获取所有商品详情(api接口详情)
    域渗透 | kerberos认证及过程中产生的攻击
    易基因: Nature Biotech:番茄细菌性青枯病的噬菌体联合治疗|国人佳作
    Day52 前端开发 JS 函数 BOM DOM
    CTF/AWD竞赛标准参考书+实战指南
    java计算机毕业设计体育用品购物系统源码+系统+数据库+lw文档
    【深度智能】:迈向高级时代的人工智能全景指南
    基于redis实现用户登陆
  • 原文地址:https://blog.csdn.net/x_9876554321_/article/details/139456297