Delphi Websocket組件獲取數據

Websocket 是個好東西。目前寫的量化交易軟件,行情的更新目前主要靠它,推送最新的行情信息到軟件。
找到兩個。

Delphi Websocket組件

僅做一些簡單的記錄.從資料來講,可能esegece要更爲成熟一點,是一款免費但是不開源的組件。資料文檔也比較健全,因此打算採用這個。組件我放到了常用的Delphi目錄(sgcWebSockets.zip),本目錄下有bin的deme。
- esegece websockets
- DelphiWebsockets

我用的是Esegece的,官方例子超多。給個我連接火幣的websocket的例子。Huobi用的是websocket socketIO版的。
連接

  //連接 火幣 websocket
  websockethuobi.Host    := 'hq.huobi.com';
  websockethuobi.Port    := 80;
  websockethuobi.Active  := True;

發送命令

sCmdPushHuobiMarketDetail := '{"symbolList":{"marketDetail":[{"symbolId":"btccny","pushType":"pushLong"}]},"version":1,"msgType":"reqMsgSubscribe","requestIndex":1404103038520}';
websockethuobi.SendEvent('request',sCmdPushHuobiMarketDetail);

讀取返回數據 MessageEvent事件

procedure TfrmBitScalper.websockethuobiMessageEvent(
  Connection: TsgcWSConnection; const Text, MsgId, MsgEndPoint, EventName,
  EventArgs, JSON: string);
begin
//  FrmLogs('EventName->'+EventName);
//  FrmLogs('MsgId->'+MsgId);
//  FrmLogs(Text);
  ReadHuobiMarketDetail(Copy(Text,5,Length(Text)));
end;

ReadHuobiMarketDetail過程 更新到窗體和全局變量
JSON的解析用的是SuperObject

{ 讀取火幣網reMarkDetail 盤口數據 }
procedure TfrmBitScalper.ReadHuobiMarketDetail(sJson: string);
var
  jo: ISuperObject;
  joarr,jobid,joask: TSuperArray;
  listitem:TListItem;
begin
  jo := SO(sJson);
  joarr := jo['args'].AsArray;
  if joarr[0]['msgType'].AsString='marketDetail' then
  begin
    jobid                               := joarr[0]['payload.bids.price'].AsArray;
    joask                               := joarr[0]['payload.asks.price'].AsArray;
    if lvMarkets.Items.Count<>0 then
    begin
      lvMarkets.Items.Item[0].Caption := 'huobi.com';
      lvMarkets.Items.Item[0].SubItems[0] := joarr[0]['payload.priceNew'].AsString;
      lvMarkets.Items.Item[0].SubItems[1] := joarr[0]['payload.priceOpen'].AsString;
      lvMarkets.Items.Item[0].SubItems[2] := joarr[0]['payload.priceHigh'].AsString;
      lvMarkets.Items.Item[0].SubItems[3] := joarr[0]['payload.priceLow'].AsString;
      lvMarkets.Items.Item[0].SubItems[4] := Floatmask(TruncTo(joarr[0]['payload.totalAmount'].AsDouble,2));
      lvMarkets.Items.Item[0].SubItems[5] := jobid[0].AsString;
      lvMarkets.Items.Item[0].SubItems[6] := joask[0].AsString;
      HUOBI_MARKET_PRICE := joarr[0]['payload.priceNew'].AsDouble;//獲取最新價格
      SELL_PRICE1        := joask[0].AsDouble;
      BUY_PRICE1         := jobid[0].AsDouble
    end
    else
    begin
      listitem := lvMarkets.Items.Add;
      listitem.Caption := 'huobi.com';
      listitem.SubItems.Add(joarr[0]['payload.priceNew'].AsString);
      listitem.SubItems.Add(joarr[0]['payload.priceOpen'].AsString);
      listitem.SubItems.Add(joarr[0]['payload.priceHigh'].AsString);
      listitem.SubItems.Add(joarr[0]['payload.priceLow'].AsString);
      listitem.SubItems.Add(Floatmask(TruncTo(joarr[0]['payload.totalAmount'].AsDouble,2)));
      listitem.SubItems.Add(jobid[0].AsString);
      listitem.SubItems.Add(joask[0].AsString);
      HUOBI_MARKET_PRICE := joarr[0]['payload.priceNew'].AsDouble;
      SELL_PRICE1        := joask[0].AsDouble;
      BUY_PRICE1         := jobid[0].AsDouble
    end;
  end;

end;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章