Commit f6babd1d by Michael Brachmann

working on websocket communication and routing issue

parent 5cc2b73b
......@@ -9,8 +9,7 @@ const
WS_MODEL = 'Ws';
type
[ServiceContract]
[Route('ws')]
[ServiceContract, Model(WS_MODEL)]
IWebSocketService = interface(IInvokable)
['{673FE678-D9EF-468D-89CB-CEF26E8758BC}']
[HttpGet, Route('emimobile')]
......
......@@ -70,7 +70,10 @@ begin
Msg := WebSocket.Receive;
case Msg.MessageType of
TWebSocketMessageType.Text:
begin
MsgStr := Msg.AsText;
Logger.Log(1, 'Web Socket Message: ' + MsgStr);
end;
//TODO: HERE we need to create a map to store connections for later
// and implement protocol to communicate
// store websocket obj, handle another layer of compression/encryption,
......
......@@ -14,11 +14,13 @@ type
FAuthUrl: string;
FApiUrl: string;
FAppUrl: string;
FWsUrl: string;
public
constructor Create;
property AuthUrl: string read FAuthUrl write FAuthUrl;
property ApiUrl: string read FApiUrl write FApiUrl;
property AppUrl: string read FAppUrl write FAppUrl;
property WsUrl: string read FWsUrl write FWsUrl;
end;
TConfigLoadedProc = reference to procedure(Config: TAppConfig);
......@@ -49,6 +51,9 @@ procedure LoadConfig(LoadProc: TConfigLoadedProc);
if JS.toString(Obj['AppUrl']) <> '' then
Config.AppUrl := JS.toString(Obj['AppUrl']);
if JS.toString(Obj['WsUrl']) <> '' then
Config.WsUrl := JS.toString(Obj['WsUrl']);
end;
finally
LoadProc(Config);
......@@ -86,6 +91,7 @@ begin
FAuthUrl := '';
FApiUrl := '';
FAppUrl := '';
FWsUrl := '';
end;
end.
......@@ -17,7 +17,9 @@ type
procedure AuthConnectionError(Error: TXDataWebConnectionError);
private
FUnauthorizedAccessProc: TUnauthorizedAccessProc;
FWsUrl: string;
public
property WsUrl: string read FWsUrl;
const clientVersion = '0.1.0';
procedure InitApp(SuccessProc: TSuccessProc;
UnauthorizedAccessProc: TUnauthorizedAccessProc);
......@@ -74,6 +76,9 @@ procedure TDMConnection.InitApp(SuccessProc: TSuccessProc;
if Config.ApiUrl <> '' then
ApiConnection.URL := Config.ApiUrl;
if Config.WsUrl <> '' then
FWsUrl := Config.WsUrl;
AuthConnection.Open(SuccessProc);
end;
......
......@@ -6,8 +6,7 @@ object dmWebsocket: TdmWebsocket
Port = 443
HostName = 'webapps.em-sys.net'
PathName = '/emiMobile/ws/emimobile'
Protocols.Strings = (
'WEB')
Protocols.Strings = ()
OnConnect = EMiMobileWebSocketClientConnect
OnBinaryDataReceived = EMiMobileWebSocketClientBinaryDataReceived
OnDisconnect = EMiMobileWebSocketClientDisconnect
......
......@@ -21,6 +21,7 @@ type
public
EMiMobileWebSocketClient: TWebSocketClient;
procedure DataModuleCreate(Sender: TObject);
procedure Connect(const AWsUrl: string);
end;
......@@ -38,6 +39,56 @@ begin
end;
procedure TdmWebsocket.Connect(const AWsUrl: string);
var
Rest, HostPort, Scheme: string;
ColonSlashSlash, SlashPos, ColonPos: Integer;
begin
if AWsUrl = '' then
Exit;
// Parse ws://host:port/path or wss://host:port/path
ColonSlashSlash := Pos('://', AWsUrl);
if ColonSlashSlash = 0 then
Exit;
Scheme := LowerCase(Copy(AWsUrl, 1, ColonSlashSlash - 1));
Rest := Copy(AWsUrl, ColonSlashSlash + 3, MaxInt);
SlashPos := Pos('/', Rest);
if SlashPos > 0 then
begin
HostPort := Copy(Rest, 1, SlashPos - 1);
EMiMobileWebSocketClient.PathName := Copy(Rest, SlashPos, MaxInt);
end
else
begin
HostPort := Rest;
EMiMobileWebSocketClient.PathName := '/';
end;
ColonPos := Pos(':', HostPort);
if ColonPos > 0 then
begin
EMiMobileWebSocketClient.HostName := Copy(HostPort, 1, ColonPos - 1);
if Scheme = 'wss' then
EMiMobileWebSocketClient.Port := StrToIntDef(Copy(HostPort, ColonPos + 1, MaxInt), 443)
else
EMiMobileWebSocketClient.Port := StrToIntDef(Copy(HostPort, ColonPos + 1, MaxInt), 80);
end
else
begin
EMiMobileWebSocketClient.HostName := HostPort;
if Scheme = 'wss' then
EMiMobileWebSocketClient.Port := 443
else
EMiMobileWebSocketClient.Port := 80;
end;
EMiMobileWebSocketClient.UseSSL := (Scheme = 'wss');
EMiMobileWebSocketClient.Active := True;
end;
procedure TdmWebsocket.EMiMobileWebSocketClientBinaryDataReceived(
Sender: TObject; AData: TBytes);
begin
......
......@@ -141,7 +141,7 @@ begin
RefreshBadgesAsync;
dmWebsocket := TdmWebsocket.Create(Self);
dmWebsocket.EMiMobileWebSocketClient.Active := True;
dmWebsocket.Connect(DMConnection.WsUrl);
end;
......@@ -445,9 +445,9 @@ begin
on E: Exception do
begin
el := Document.getElementById('view.main.badgecomplaints');
if Assigned(el) then TJSHtmlElement(el).innerText := '';
if Assigned(el) then TJSHtmlElement(el).innerText := '';
el := Document.getElementById('view.main.badgeunits');
if Assigned(el) then TJSHtmlElement(el).innerText := '';
if Assigned(el) then TJSHtmlElement(el).innerText := '';
Console.Log('Badge refresh error: ' + E.Message);
end;
end;
......
{
"AuthUrl" : "http://localhost:2009/emimobile/auth/",
"ApiUrl" : "http://localhost:2009/emimobile/api/",
"AppUrl" : "http://localhost:2009/emimobile/app/"
"AppUrl" : "http://localhost:2009/emimobile/app/",
"WsUrl" : "ws://localhost:2009/emiMobile/ws/emimobile"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment