Skip to content

Commit c9eeb12

Browse files
AdministratorAdministrator
Administrator
authored and
Administrator
committed
simple demo
1 parent c76ca9d commit c9eeb12

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

Demo/Project1.dpr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
program Project1;
2+
3+
uses
4+
Vcl.Forms,
5+
Unit1 in 'Unit1.pas' {Form1},
6+
superobject in '..\superobject\superobject.pas';
7+
8+
{$R *.res}
9+
10+
begin
11+
Application.Initialize;
12+
Application.MainFormOnTaskbar := True;
13+
Application.CreateForm(TForm1, Form1);
14+
Application.Run;
15+
end.

Demo/Unit1.dfm

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
object Form1: TForm1
2+
Left = 0
3+
Top = 0
4+
Caption = 'Form1'
5+
ClientHeight = 337
6+
ClientWidth = 635
7+
Color = clBtnFace
8+
Font.Charset = DEFAULT_CHARSET
9+
Font.Color = clWindowText
10+
Font.Height = -11
11+
Font.Name = 'Tahoma'
12+
Font.Style = []
13+
OldCreateOrder = False
14+
PixelsPerInch = 96
15+
TextHeight = 13
16+
object Button1: TButton
17+
Left = 8
18+
Top = 8
19+
Width = 75
20+
Height = 25
21+
Caption = 'Button1'
22+
TabOrder = 0
23+
OnClick = Button1Click
24+
end
25+
object Button2: TButton
26+
Left = 8
27+
Top = 39
28+
Width = 75
29+
Height = 25
30+
Caption = 'Button2'
31+
TabOrder = 1
32+
OnClick = Button2Click
33+
end
34+
object Timer1: TTimer
35+
Enabled = False
36+
Interval = 5000
37+
OnTimer = Timer1Timer
38+
Left = 128
39+
Top = 16
40+
end
41+
end

Demo/Unit1.pas

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)