Delphi Websocket组件获取数据

2016-08-28 19:03:08来源:CSDN作者:MichaelJScofield人点击

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;

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台