No description
Find a file
André Mussche f3268769c3 Merge pull request #4 from Yvi71/master
openssl support. tested. worked.
2016-11-10 12:55:31 +01:00
Demo simple demo 2015-10-01 08:33:16 +02:00
DUnit firefox support 2014-02-10 11:48:31 +01:00
superobject superobject: get current value/object (so objects are not created every time and overwritten -> mem leak) 2014-02-14 15:48:26 +01:00
.gitattributes error handling and unit test 2013-11-18 14:27:13 +01:00
.gitignore error handling and unit test 2013-11-18 14:27:13 +01:00
IdHTTPWebsocketClient.pas added support for openssl - and therewith dependency to openssl units. 2015-11-13 01:59:22 +01:00
IdIOHandlerWebsocket.pas Fixed some issues with the compiler switch and other things i messed up last night. 2015-11-13 11:09:24 +01:00
IdServerBaseHandling.pas first checkin 2013-11-11 21:14:42 +01:00
IdServerIOHandlerWebsocket.pas Fixed some issues with the compiler switch and other things i messed up last night. 2015-11-13 11:09:24 +01:00
IdServerSocketIOHandling.pas write timeout + error handling (try except for callbacks) 2014-07-03 11:39:18 +02:00
IdServerWebsocketContext.pas error handling and unit test 2013-11-18 14:27:13 +01:00
IdServerWebsocketHandling.pas added support for openssl - and therewith dependency to openssl units. 2015-11-13 01:59:22 +01:00
IdSocketIOHandling.pas fixed locks and memleak 2014-08-01 12:19:25 +02:00
IdWebsocketServer.pas added support for openssl - and therewith dependency to openssl units. 2015-11-13 01:59:22 +01:00
README.md Create README.md 2015-09-28 09:16:36 +02:00
RemObjectsSDK_WS.js Websocket support for RemObjectsSDK 2013-11-11 21:15:05 +01:00
ROdemoWS.zip Websocket support for RemObjectsSDK 2013-11-11 21:15:05 +01:00
uROHTTPWebsocketServer.pas local sync 2014-01-02 14:59:48 +01:00
uROIdServerWebsocketHandling.pas Websocket support for RemObjectsSDK 2013-11-11 21:15:05 +01:00
uROIndyHTTPWebsocketChannel.pas shutdown fixes, error event handling 2014-06-25 15:30:18 +02:00
uROSimpleEventRepository.pas Websocket support for RemObjectsSDK 2013-11-11 21:15:05 +01:00

DelphiWebsockets

Websockets and Socket.io for Delphi

See below for an event driven async example of an socket.io server and client:

uses
  IdWebsocketServer, IdHTTPWebsocketClient, superobject, IdSocketIOHandling;

var
  server: TIdWebsocketServer;
  client: TIdHTTPWebsocketClient;

const
  C_CLIENT_EVENT = 'CLIENT_TO_SERVER_EVENT_TEST';
  C_SERVER_EVENT = 'SERVER_TO_CLIENT_EVENT_TEST';

procedure ShowMessageInMainthread(const aMsg: string) ;
begin
  TThread.Synchronize(nil,
    procedure
    begin
      ShowMessage(aMsg);
    end);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  server := TIdWebsocketServer.Create(Self);
  server.DefaultPort := 12345;
  server.SocketIO.OnEvent(C_CLIENT_EVENT,
    procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback)
    begin
      //show request (threadsafe)
      ShowMessageInMainthread('REQUEST: ' + aArgument[0].AsJSon);
      //send callback (only if specified!)
      if aCallback <> nil then
        aCallback.SendResponse( SO(['succes', True]).AsJSon );
    end);
  server.Active      := True;

  client := TIdHTTPWebsocketClient.Create(Self);
  client.Port := 12345;
  client.Host := 'localhost';
  client.SocketIOCompatible := True;
  client.SocketIO.OnEvent(C_SERVER_EVENT,
    procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback)
    begin
      ShowMessageInMainthread('Data PUSHED from server: ' + aArgument[0].AsJSon);
      //server wants a response?
      if aCallback <> nil then
        aCallback.SendResponse('thank for the push!');
    end);
  client.Connect;
  client.SocketIO.Emit(C_CLIENT_EVENT, SO([ 'request', 'some data']),
    //provide callback
    procedure(const ASocket: ISocketIOContext; const aJSON: ISuperObject; const aCallback: ISocketIOCallback)
    begin
      //show response (threadsafe)
      ShowMessageInMainthread('RESPONSE: ' + aJSON.AsJSon);
    end);

  //start timer so server pushes (!) data to all clients
  Timer1.Interval := 5 * 1000; //5s
  Timer1.Enabled  := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := false;
  server.SocketIO.EmitEventToAll(C_SERVER_EVENT, SO(['data', 'pushed from server']),
    procedure(const ASocket: ISocketIOContext; const aJSON: ISuperObject; const aCallback: ISocketIOCallback)
    begin
        //show response (threadsafe)
        TThread.Synchronize(nil,
          procedure
          begin
            ShowMessage('RESPONSE from a client: ' + aJSON.AsJSon);
          end);
    end);
end;