|
| 1 | +unit Unit1; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, |
| 7 | + Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, |
| 8 | + IdServerWebsocketContext; |
| 9 | + |
| 10 | +type |
| 11 | + TForm1 = class(TForm) |
| 12 | + Button1: TButton; |
| 13 | + Timer1: TTimer; |
| 14 | + Button2: TButton; |
| 15 | + procedure Button1Click(Sender: TObject); |
| 16 | + procedure Timer1Timer(Sender: TObject); |
| 17 | + procedure Button2Click(Sender: TObject); |
| 18 | + private |
| 19 | + procedure ServerMessageTextReceived(const AContext: TIdServerWSContext; const aText: string); |
| 20 | + procedure ClientBinDataReceived(const aData: TStream); |
| 21 | + public |
| 22 | + end; |
| 23 | + |
| 24 | +var |
| 25 | + Form1: TForm1; |
| 26 | + |
| 27 | +implementation |
| 28 | + |
| 29 | +{$R *.dfm} |
| 30 | + |
| 31 | +uses |
| 32 | + IdWebsocketServer, IdHTTPWebsocketClient, superobject, IdSocketIOHandling, |
| 33 | + IdIOHandlerWebsocket; |
| 34 | + |
| 35 | +var |
| 36 | + server: TIdWebsocketServer; |
| 37 | + client: TIdHTTPWebsocketClient; |
| 38 | + |
| 39 | +const |
| 40 | + C_CLIENT_EVENT = 'CLIENT_TO_SERVER_EVENT_TEST'; |
| 41 | + C_SERVER_EVENT = 'SERVER_TO_CLIENT_EVENT_TEST'; |
| 42 | + |
| 43 | +procedure ShowMessageInMainthread(const aMsg: string) ; |
| 44 | +begin |
| 45 | + TThread.Synchronize(nil, |
| 46 | + procedure |
| 47 | + begin |
| 48 | + ShowMessage(aMsg); |
| 49 | + end); |
| 50 | +end; |
| 51 | + |
| 52 | +procedure TForm1.Button1Click(Sender: TObject); |
| 53 | +begin |
| 54 | + server := TIdWebsocketServer.Create(Self); |
| 55 | + server.DefaultPort := 12345; |
| 56 | + server.SocketIO.OnEvent(C_CLIENT_EVENT, |
| 57 | + procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback) |
| 58 | + begin |
| 59 | + //show request (threadsafe) |
| 60 | + ShowMessageInMainthread('REQUEST: ' + aArgument[0].AsJSon); |
| 61 | + //send callback (only if specified!) |
| 62 | + if aCallback <> nil then |
| 63 | + aCallback.SendResponse( SO(['succes', True]).AsJSon ); |
| 64 | + end); |
| 65 | + server.Active := True; |
| 66 | + |
| 67 | + client := TIdHTTPWebsocketClient.Create(Self); |
| 68 | + client.Port := 12345; |
| 69 | + client.Host := 'localhost'; |
| 70 | + client.SocketIOCompatible := True; |
| 71 | + client.SocketIO.OnEvent(C_SERVER_EVENT, |
| 72 | + procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback) |
| 73 | + begin |
| 74 | + ShowMessageInMainthread('Data PUSHED from server: ' + aArgument[0].AsJSon); |
| 75 | + //server wants a response? |
| 76 | + if aCallback <> nil then |
| 77 | + aCallback.SendResponse('thank for the push!'); |
| 78 | + end); |
| 79 | + client.Connect; |
| 80 | + client.SocketIO.Emit(C_CLIENT_EVENT, SO([ 'request', 'some data']), |
| 81 | + //provide callback |
| 82 | + procedure(const ASocket: ISocketIOContext; const aJSON: ISuperObject; const aCallback: ISocketIOCallback) |
| 83 | + begin |
| 84 | + //show response (threadsafe) |
| 85 | + ShowMessageInMainthread('RESPONSE: ' + aJSON.AsJSon); |
| 86 | + end); |
| 87 | + |
| 88 | + //start timer so server pushes (!) data to all clients |
| 89 | + Timer1.Interval := 5 * 1000; //5s |
| 90 | +// Timer1.Enabled := True; |
| 91 | +end; |
| 92 | + |
| 93 | +procedure TForm1.Button2Click(Sender: TObject); |
| 94 | +begin |
| 95 | + server := TIdWebsocketServer.Create(Self); |
| 96 | + server.DefaultPort := 12346; |
| 97 | + server.Active := True; |
| 98 | + |
| 99 | + client := TIdHTTPWebsocketClient.Create(Self); |
| 100 | + client.Port := 12346; |
| 101 | + client.Host := 'localhost'; |
| 102 | + client.Connect; |
| 103 | + client.UpgradeToWebsocket; |
| 104 | + |
| 105 | + client.OnBinData := ClientBinDataReceived; |
| 106 | + server.OnMessageText := ServerMessageTextReceived; |
| 107 | + client.IOHandler.Write('test'); |
| 108 | +end; |
| 109 | + |
| 110 | +procedure TForm1.ClientBinDataReceived(const aData: TStream); |
| 111 | +begin |
| 112 | + // |
| 113 | +end; |
| 114 | + |
| 115 | +procedure TForm1.ServerMessageTextReceived(const AContext: TIdServerWSContext; const aText: string); |
| 116 | +var |
| 117 | + strm: TStringStream; |
| 118 | +begin |
| 119 | + ShowMessageInMainthread('WS REQUEST: ' + aText); |
| 120 | + strm := TStringStream.Create('SERVER: ' + aText); |
| 121 | + AContext.IOHandler.Write(strm, wdtBinary); |
| 122 | +end; |
| 123 | + |
| 124 | +procedure TForm1.Timer1Timer(Sender: TObject); |
| 125 | +begin |
| 126 | + Timer1.Enabled := false; |
| 127 | + server.SocketIO.EmitEventToAll(C_SERVER_EVENT, SO(['data', 'pushed from server']), |
| 128 | + procedure(const ASocket: ISocketIOContext; const aJSON: ISuperObject; const aCallback: ISocketIOCallback) |
| 129 | + begin |
| 130 | + //show response (threadsafe) |
| 131 | + TThread.Synchronize(nil, |
| 132 | + procedure |
| 133 | + begin |
| 134 | + ShowMessage('RESPONSE from a client: ' + aJSON.AsJSon); |
| 135 | + end); |
| 136 | + end); |
| 137 | +end; |
| 138 | + |
| 139 | +end. |
0 commit comments