-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerUnit.pas
98 lines (81 loc) · 2.39 KB
/
ServerUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
unit ServerUnit;
{$I mormot.defines.inc}
interface
uses
Classes,
SysUtils,
mormot.core.base,
mormot.core.interfaces,
mormot.rest.http.server,
mormot.rest.memserver,
mormot.soa.core,
mormot.core.os,
SharedInterface;
type
{ TChatService }
TChatService = class(TInterfacedObject,IChatService)
protected
fConnected: array of IChatCallback;
public
procedure Join(const pseudo: string; const callback: IChatCallback);
procedure BlaBla(const pseudo,msg: string);
procedure CallbackReleased(const callback: IInvokable; const interfaceName: RawUTF8);
end;
{ TServer }
TServer = class
public
procedure Run;
end;
implementation
{ TServer }
procedure TServer.Run;
var
HttpServer: TRestHttpServer;
Server: TRestServerFullMemory;
AChatService: IChatService;
begin
Server := TRestServerFullMemory.CreateWithOwnModel([]);
try
Server.ServiceDefine(TChatService,[IChatService],sicShared).
SetOptions([],[optExecLockedPerInterface]). // thread-safe fConnected[]
ByPassAuthentication := true;
HttpServer := TRestHttpServer.Create('8888',[Server],'+',useBidirSocket);
try
HttpServer.WebSocketsEnable(Server,PROJECTEST_TRANSMISSION_KEY).
Settings.SetFullLog; // full verbose logs for this demo
TextColor(ccLightGreen);
writeln('WebSockets Chat Server running on localhost:8888'#13#10);
TextColor(ccWhite);
writeln('Please compile and run Client.exe'#13#10);
TextColor(ccLightGray);
writeln('Press [Enter] to quit'#13#10);
TextColor(ccCyan);
readln;
finally
HttpServer.Free;
end;
finally
Server.Free;
end;
end;
{ TChatService }
procedure TChatService.Join(const pseudo: string; const callback: IChatCallback);
begin
InterfaceArrayAdd(fConnected,callback);
end;
procedure TChatService.BlaBla(const pseudo, msg: string);
var i: integer;
begin
for i := high(fConnected) downto 0 do // downwards for InterfaceArrayDelete()
try
fConnected[i].NotifyBlaBla(pseudo, msg);
except
InterfaceArrayDelete(fConnected,i); // unsubscribe the callback on failure
end;
end;
procedure TChatService.CallbackReleased(const callback: IInvokable; const interfaceName: RawUTF8);
begin
if interfaceName='IChatCallback' then
InterfaceArrayDelete(fConnected,callback);
end;
end.