Websockets and Socket.io for Delphi
Switch branches/tags
Nothing to show
Clone or download
Latest commit d205bbe Jan 5, 2018
Permalink
Failed to load latest commit information.
DUnit * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
Demo simple demo Oct 1, 2015
superobject superobject: get current value/object (so objects are not created eve… Feb 14, 2014
.gitattributes error handling and unit test Nov 18, 2013
.gitignore error handling and unit test Nov 18, 2013
IdHTTPWebsocketClient.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdIOHandlerWebsocket.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdServerBaseHandling.pas first checkin Nov 11, 2013
IdServerIOHandlerWebsocket.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdServerSocketIOHandling.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdServerWebsocketContext.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdServerWebsocketHandling.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdSocketIOHandling.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
IdWebsocketServer.pas * Add defines for supporting SSL and HTTPBridge and removing SUPEROBJ… Oct 28, 2016
README.md Update README.md Jan 5, 2018
ROdemoWS.zip Websocket support for RemObjectsSDK Nov 11, 2013
RemObjectsSDK_WS.js Websocket support for RemObjectsSDK Nov 11, 2013
uROHTTPWebsocketServer.pas local sync Jan 2, 2014
uROIdServerWebsocketHandling.pas Websocket support for RemObjectsSDK Nov 11, 2013
uROIndyHTTPWebsocketChannel.pas shutdown fixes, error event handling Jun 25, 2014
uROSimpleEventRepository.pas Websocket support for RemObjectsSDK Nov 11, 2013
wsdefines.pas Missing file Nov 11, 2016

README.md

Not active anymore

Unfortunately I don't have time to support this project anymore. Also the websocket protocol has changed in the meantime, so it won't work with browser and other modern implementations.

Please take a look at the free (but closed) 3rd party component:

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;