Delphi sgcWebSockets 的 WebSockets简单使用

最近在项目中使用到了Websocket,开始想自己写,懒了, 觉得还是不要造车轮了,于是找到了sgcWebSockets。Demo简单,连接也简单。 用的是 delphi2007 + sgcWebSockets v2.3

  WSClient_Eye.Host := sIP;
  WSClient_Eye.Port := sPort;
  WSClient_Eye.Active := True;

但是,对方的WS地址是   ws://127.0.0.1:8808/main/sm991228,于是简单改造一下,如下:

function WSConn(sUrl: string):Boolean;
var
  fsUrl : AnsiString;
  sHeader, sIP, sWSResourceName : AnsiString;
  sPort : Word;
  iPosition1, iPosition2 : Integer;
  function Analysis(sIn : string) : Boolean;
  begin
    Result := False;
    try
      fsUrl := trim(sIn);
      if fsUrl = '' then Exit;
      sHeader := LeftStr(fsUrl,5);
      if sHeader <> 'ws://' then Exit;
      Delete(fsUrl, 1, 5);
      iPosition1 := pos(':',fsUrl);
      iPosition2 := pos('/',fsUrl);
      sIP := copy(fsUrl, 1, (iPosition1-1));
      sPort := StrToIntDef(copy(fsUrl, (iPosition1+1), (iPosition2 - iPosition1 - 1)), 0);
      sWSResourceName := copy(fsUrl, (iPosition2+1), (Length(fsUrl) - iPosition2));
      Result := True;
    finally
    end;
  end;
begin
  Result := false;
  try
    if not Analysis(sUrl) then Exit;
    WSClient_Eye.Host := sIP;
    WSClient_Eye.Port := sPort;
    WSClient_Eye.Options.Parameters := sWSResourceName;   //主要就是这一句
    WSClient_Eye.Active := True;
    Result := True;
  finally
  end;
end;

其实也很简单, 多看一下源码。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
负载平衡,MQTT协议支持,修正大量Bug,最新稳定版 Delphi7/XE6/XE7/XE8 ******************************************************* sgcWebSockets ******************************************************* [*] : Bug [+] : New [-] : Deleted [/] : Breaking changes Versions -------- 4.1.0: 2017 April [+] : Added support for Rad Studio 10.2 Tokyo. [+] : Added support for Linux compiler. [+] : MQTT client, added subscribe method. [+] : MQTT client, added unsubscribe method. [+] : MQTT client, added publish method. [+] : MQTT client, added Authentication property, allows to set user and password to authenticate against MQTT Server. [+] : MQTT client, added HeartBeat property, keeps alive connection. [+] : MQTT client, added LastWillTestament property, when client disconnects, sends a message to other connected clients. [+] : New Property "NotifyDeletes" in Dataset Server Protocol, if enabled (by default) broadcast deleted record to all clients. [+] : New Method "BroadcastRecord" in Dataset Server Protocol, sends dataset record values to all clients. [+] : New Method "MetaData" in Dataset Server Protocol, sends metadata info to a single client. [+] : New Method "Synchronize" in Dataset Server Protocol, sends dataset record values to a single client. [*] : Fixed Bug ReadTimeout and ConnectionTimeout in client component. [*] : Added Guid property to Client File Protocol. [*] : Fixed Bug Invalid Character when trying to access to built-in javascript libraries. [*] : Fixed Bug File Protocol when BufferSize was set to zero, file was not saved properly. [*] : Fixed Memory Leaks on NextGen compiler. [*] : Fixed WebRTC Chrome console errors.
------------------------------------------------------------------------------------------------------------------------------------------------- Installing sgcWebSockets in Rad Studio 7 - XE8 - 10.2 Tokyo ------------------------------------------------------------------------------------------------------------------------------------------------- 1) Unzip the files included into a directory {$DIR} 2) Add the directory where the files are unzipped {$DIR} to the Rad Studio library path under Tools, Environment options, Directories a) All Rad Studio Versions: Add the directory {$DIR}\source to the library path b) For specific Rad Studio version Rad Studio 7 : Add the directory {$DIR}\libD7 to the library path Rad Studio 2007 : Add the directory {$DIR}\libD2007 to the library path Rad Studio 2009 : Add the directory {$DIR}\libD2009 to the library path Rad Studio 2010 : Add the directory {$DIR}\libD2010 to the library path Rad Studio XE : Add the directory {$DIR}\libDXE to the library path Rad Studio XE2 : Add the directory {$DIR}\libDXE2\$(Platform) to the library path Rad Studio XE3 : Add the directory {$DIR}\libDXE3\$(Platform) to the library path Rad Studio XE4 : Add the directory {$DIR}\libDXE4\$(Platform) to the library path Rad Studio XE5 : Add the directory {$DIR}\libDXE5\$(Platform) to the library path Rad Studio XE6 : Add the directory {$DIR}\libDXE6\$(Platform) to the library path Rad Studio XE7 : Add the directory {$DIR}\libDXE7\$(Platform) to the library path Rad Studio XE8 : Add the directory {$DIR}\libDXE8\$(Platform) to the library path Rad Studio 10 : Add the directory {$DIR}\libD10\$(Platform) to the library path Rad Studio 10.1 : Add the directory {$DIR}\libD10_1\$(Platform) to the library path Rad Studio 10.2 : Add the directory {$DIR}\libD10_2\$(Platform) to the library path 3) Choose : File, Open and browse
下面是一个使用 Python 的 Flask 和 Flask-SocketIO 库实现的简单 WebSocket 示例: ```python from flask import Flask, render_template from flask_socketio import SocketIO, emit app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app) @app.route('/') def index(): return render_template('index.html') @socketio.on('connect') def test_connect(): emit('my response', {'data': 'Connected'}) @socketio.on('disconnect') def test_disconnect(): print('Client disconnected') if __name__ == '__main__': socketio.run(app) ``` 在上面的代码中,我们创建了一个 Flask 应用程序,并使用 Flask-SocketIO 扩展来启用 WebSocket 功能。我们定义了两个事件处理程序:`test_connect` 用于处理客户端连接事件,`test_disconnect` 用于处理客户端断开连接事件。我们还定义了一个简单的路由处理程序,用于返回一个 HTML 模板。 在客户端,我们可以使用 JavaScript 代码来连接到 WebSocket 服务器,并发送和接收消息: ```javascript var socket = io.connect('http://' + document.domain + ':' + location.port); socket.on('connect', function() { socket.emit('my event', {data: 'I\'m connected!'}); }); socket.on('my response', function(msg) { console.log(msg.data); }); ``` 在上面的代码中,我们使用 SocketIO 客户端库来连接到 WebSocket 服务器,并发送和接收消息。当客户端连接成功时,`connect` 事件将被触发,并发送一个 `my event` 消息。服务器将收到该消息,并发送一个 `my response` 消息作为响应。客户端将收到该响应,并将消息内容输出到浏览器的控制台中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值