WAMP bileşeninin en yaygın kullanımı, bir istemcinin sunucudan bir yöntem istemesi ve sunucunun istemciye bir yanıt göndermesidir. İstemci yalnızca yöntemin adını gönderebilir ve/veya sunucunun sonucu hesaplamak için ihtiyaç duyduğu bazı parametreleri geçirebilir. Sunucu istekleri işler ve başarılı olursa istemciye sonuçla birlikte bir yanıt gönderir. Herhangi bir hata varsa, sunucu istemciye bir hata yanıtı gönderir.
Gördüğünüz gibi, yalnızca Bir istek ve Bir yanıt (başarılı veya değil) vardır.
Örnek: sunucunun GetTime adlı bir yöntemi vardır, böylece her seferinde bir istemci bu yöntemi talep ettiğinde, sunucu sunucu zamanını döndürür.
WAMP Sunucusu
procedure OnServerCall(Connection: TsgcWSConnection; const CallId, ProcUri, Arguments: string);
begin
if ProcUri = 'GetTime' then
oServerWAMP.CallResult(CallId, FormatDateTime('yyyymmdd hh:nn:ss', Now))
else
oServer.WAMP.CallError(CallId, 'Unknown method');
end;
oServer := TsgcWebSocketServer.Create(nil);
oServer.Port := 80;
oServerWAMP := TsgcWSPServer_WAMP.Create(nil);
oServerWAMP.OnCall := OnServerCallEvent;
oServerWAMP.Server := oServer;
oServer.Active := True;
WAMP Client
procedure OnCallResultClient(Connection: TsgcWSConnection; CallId, Result: string);
begin
ShowMessage(Result);
end;
procedure OnCallErrorClient(Connection: TsgcWSConnection; const Error: string);
begin
ShowMessage(Error);
end;
oClient := TsgcWebSocketClient.Create(nil);
oClient.Host := '127.0.0.1';
oClient.Port := 80;
oClientWAMP := TsgcWSPClient_WAMP.Create(nil);
oClientWAMP.OnCallResult := OnCallResultClient;
oClientWAMP.OnCallError := OnCallErrorClient;
oClientWAMP.Client := oClient;
oClient.Active := True;
// After client has connected, request GetTime from server
oClientWAMP.Call('GetTime');