TWebSocketClient

TWebSocketClient

支持WEBSOCKET通讯协议的客户端控件。

连接WEBSOCKET SERVER的代码演示:

WebSocketClient1.HostName := '88.198.69.227';
WebSocketClient1.Port := 8888;

WebSocketClient1.Disconnect;

WebSocketClient1.Connect;

接收数据事件:

procedure TForm43.WebSocketClient1DataReceived(Sender: TObject; Origin: string;
  Data: TJSObject);
var
  it: TTMSFNCListBoxItem;
  sl: TStringList;
  s: String;
  n: string;
  v: string;
begin
  lst.BeginUpdate;
  it := lst.Items.Add;
  s := Data.toString;
  sl := TStringList.Create;
  try
    TTMSFNCUtils.Split('~', s, sl);
    if sl.Count > 2 then
    begin
      n := '<font color="'+sl[0]+'" size="16">'+sl[1];
      v := sl[2];
      it.Text := n + ' says: </font><font size="16">' + v+'</font>';
    end;
  finally
    sl.Free;
  end;
  lst.EndUpdate;
end;

 发送数据演示:

procedure TForm43.SendMessage;
var
  s: string;
begin
  if FConnected and (WebEdit2.Text <> '') then
  begin
    s := TTMSFNCGraphics.ColorToHTML(TMSFNCColorPicker1.SelectedColor) + '~' + WebEdit1.Text + '~' + WebEdit2.Text;
    // limit message length
    s := Copy(s,1,256);
    WebSocketClient1.Send(s);
    WebEdit2.Text := '';
  end;
end;

  

posted @ 2019-02-14 14:50  delphi中间件  阅读(3080)  评论(0编辑  收藏  举报