Commit f6babd1d by Michael Brachmann

working on websocket communication and routing issue

parent 5cc2b73b
...@@ -9,8 +9,7 @@ const ...@@ -9,8 +9,7 @@ const
WS_MODEL = 'Ws'; WS_MODEL = 'Ws';
type type
[ServiceContract] [ServiceContract, Model(WS_MODEL)]
[Route('ws')]
IWebSocketService = interface(IInvokable) IWebSocketService = interface(IInvokable)
['{673FE678-D9EF-468D-89CB-CEF26E8758BC}'] ['{673FE678-D9EF-468D-89CB-CEF26E8758BC}']
[HttpGet, Route('emimobile')] [HttpGet, Route('emimobile')]
......
...@@ -70,7 +70,10 @@ begin ...@@ -70,7 +70,10 @@ begin
Msg := WebSocket.Receive; Msg := WebSocket.Receive;
case Msg.MessageType of case Msg.MessageType of
TWebSocketMessageType.Text: TWebSocketMessageType.Text:
begin
MsgStr := Msg.AsText;
Logger.Log(1, 'Web Socket Message: ' + MsgStr); Logger.Log(1, 'Web Socket Message: ' + MsgStr);
end;
//TODO: HERE we need to create a map to store connections for later //TODO: HERE we need to create a map to store connections for later
// and implement protocol to communicate // and implement protocol to communicate
// store websocket obj, handle another layer of compression/encryption, // store websocket obj, handle another layer of compression/encryption,
......
...@@ -14,11 +14,13 @@ type ...@@ -14,11 +14,13 @@ type
FAuthUrl: string; FAuthUrl: string;
FApiUrl: string; FApiUrl: string;
FAppUrl: string; FAppUrl: string;
FWsUrl: string;
public public
constructor Create; constructor Create;
property AuthUrl: string read FAuthUrl write FAuthUrl; property AuthUrl: string read FAuthUrl write FAuthUrl;
property ApiUrl: string read FApiUrl write FApiUrl; property ApiUrl: string read FApiUrl write FApiUrl;
property AppUrl: string read FAppUrl write FAppUrl; property AppUrl: string read FAppUrl write FAppUrl;
property WsUrl: string read FWsUrl write FWsUrl;
end; end;
TConfigLoadedProc = reference to procedure(Config: TAppConfig); TConfigLoadedProc = reference to procedure(Config: TAppConfig);
...@@ -49,6 +51,9 @@ procedure LoadConfig(LoadProc: TConfigLoadedProc); ...@@ -49,6 +51,9 @@ procedure LoadConfig(LoadProc: TConfigLoadedProc);
if JS.toString(Obj['AppUrl']) <> '' then if JS.toString(Obj['AppUrl']) <> '' then
Config.AppUrl := JS.toString(Obj['AppUrl']); Config.AppUrl := JS.toString(Obj['AppUrl']);
if JS.toString(Obj['WsUrl']) <> '' then
Config.WsUrl := JS.toString(Obj['WsUrl']);
end; end;
finally finally
LoadProc(Config); LoadProc(Config);
...@@ -86,6 +91,7 @@ begin ...@@ -86,6 +91,7 @@ begin
FAuthUrl := ''; FAuthUrl := '';
FApiUrl := ''; FApiUrl := '';
FAppUrl := ''; FAppUrl := '';
FWsUrl := '';
end; end;
end. end.
...@@ -17,7 +17,9 @@ type ...@@ -17,7 +17,9 @@ type
procedure AuthConnectionError(Error: TXDataWebConnectionError); procedure AuthConnectionError(Error: TXDataWebConnectionError);
private private
FUnauthorizedAccessProc: TUnauthorizedAccessProc; FUnauthorizedAccessProc: TUnauthorizedAccessProc;
FWsUrl: string;
public public
property WsUrl: string read FWsUrl;
const clientVersion = '0.1.0'; const clientVersion = '0.1.0';
procedure InitApp(SuccessProc: TSuccessProc; procedure InitApp(SuccessProc: TSuccessProc;
UnauthorizedAccessProc: TUnauthorizedAccessProc); UnauthorizedAccessProc: TUnauthorizedAccessProc);
...@@ -74,6 +76,9 @@ procedure TDMConnection.InitApp(SuccessProc: TSuccessProc; ...@@ -74,6 +76,9 @@ procedure TDMConnection.InitApp(SuccessProc: TSuccessProc;
if Config.ApiUrl <> '' then if Config.ApiUrl <> '' then
ApiConnection.URL := Config.ApiUrl; ApiConnection.URL := Config.ApiUrl;
if Config.WsUrl <> '' then
FWsUrl := Config.WsUrl;
AuthConnection.Open(SuccessProc); AuthConnection.Open(SuccessProc);
end; end;
......
...@@ -6,8 +6,7 @@ object dmWebsocket: TdmWebsocket ...@@ -6,8 +6,7 @@ object dmWebsocket: TdmWebsocket
Port = 443 Port = 443
HostName = 'webapps.em-sys.net' HostName = 'webapps.em-sys.net'
PathName = '/emiMobile/ws/emimobile' PathName = '/emiMobile/ws/emimobile'
Protocols.Strings = ( Protocols.Strings = ()
'WEB')
OnConnect = EMiMobileWebSocketClientConnect OnConnect = EMiMobileWebSocketClientConnect
OnBinaryDataReceived = EMiMobileWebSocketClientBinaryDataReceived OnBinaryDataReceived = EMiMobileWebSocketClientBinaryDataReceived
OnDisconnect = EMiMobileWebSocketClientDisconnect OnDisconnect = EMiMobileWebSocketClientDisconnect
......
...@@ -21,6 +21,7 @@ type ...@@ -21,6 +21,7 @@ type
public public
EMiMobileWebSocketClient: TWebSocketClient; EMiMobileWebSocketClient: TWebSocketClient;
procedure DataModuleCreate(Sender: TObject); procedure DataModuleCreate(Sender: TObject);
procedure Connect(const AWsUrl: string);
end; end;
...@@ -38,6 +39,56 @@ begin ...@@ -38,6 +39,56 @@ begin
end; 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( procedure TdmWebsocket.EMiMobileWebSocketClientBinaryDataReceived(
Sender: TObject; AData: TBytes); Sender: TObject; AData: TBytes);
begin begin
......
...@@ -141,7 +141,7 @@ begin ...@@ -141,7 +141,7 @@ begin
RefreshBadgesAsync; RefreshBadgesAsync;
dmWebsocket := TdmWebsocket.Create(Self); dmWebsocket := TdmWebsocket.Create(Self);
dmWebsocket.EMiMobileWebSocketClient.Active := True; dmWebsocket.Connect(DMConnection.WsUrl);
end; end;
...@@ -445,9 +445,9 @@ begin ...@@ -445,9 +445,9 @@ begin
on E: Exception do on E: Exception do
begin begin
el := Document.getElementById('view.main.badgecomplaints'); 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'); 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); Console.Log('Badge refresh error: ' + E.Message);
end; end;
end; end;
......
{ {
"AuthUrl" : "http://localhost:2009/emimobile/auth/", "AuthUrl" : "http://localhost:2009/emimobile/auth/",
"ApiUrl" : "http://localhost:2009/emimobile/api/", "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