Jump to content

Working solution: Problem - "Open WS Connection on TUniLoginForm"


Skyp

Recommended Posts

I ran into a problem that in version 1.95.0.1576 the WS does not work on the Login form.
Of course, this is possible and correct, because the user has not yet logged in, but there are ReceiveSocketsEvents in the form properties, but there is no Delphi event BroadcastMessage(this handler is an illusion, because in fact WS listens only to the frontend and it sends an ajaxRequest to the backend).

I have solved this problem (through a crutch😞

1. property form ReceiveSocketsEvents - in True

2. I add in ClientEvents - for event form.socketmesssage it code 

image.png.f2d61d2a7e49e6d0a897bd1cf4c2408c.png

3. And added a handler on onAjaxEvent...  (with my procedure, here will be your handler)

image.png.7f729038792322962fc0e40ef589fbd9.png

4. Next.. i add in Delphi Event - OnCreate (LoginForm) call:

  UniServerModule.WebSocketStack.CreateWebSocket(unisession, self.WebForm);

this opens WebSockets in the browser - Ok 

But if after that you try to log in (and on the main form you will listen to the WS channel) then you will get an error in the browser console:

"WebSocket is closed before the connection is established"  - https://stackoverflow.com/questions/12487828/what-does-websocket-is-closed-before-the-connection-is-established-mean

for Example:

 image.png.caeb909c180b0d87222d60cfb457ea48.png

Okay not a nice mistake...  - We understand that we need to close the connection on the login form (before initializing the main form) because the connection is global... (We go to the JS part of the framework and look at everything in WS)

5. Writing in Delphi class Helper:

TUniWebSocketStackHelp = class helper for TUniWebSocketStack

  public

    procedure TerminateWS(const aForm: TUniBaseJSForm);

    procedure CloseWS(const aForm: TUniBaseJSForm);

  end;

//...

 

procedure TUniWebSocketStackHelp.CloseWS(const aForm: TUniBaseJSForm);

begin

  with aForm.JSInterface do

  begin

    JSCallGlobal('uniWebSocketObj.closeSocket', []);

 

  end;

end;

 

procedure TUniWebSocketStackHelp.TerminateWS(const aForm: TUniBaseJSForm);

begin

  with aForm.JSInterface do

  begin

    JSCallGlobal('uniWebSocketObj.terminateSocket', []);

  end;

end;

6. Before closing the login form (ModalResult.mrok), we perform:

"UniServerModule.WebSocketStack.TerminateWS(self.WebForm);"

 

- I hope this is useful to someone!! 

  • Thanks 1
Link to comment
Share on other sites

  • Skyp changed the title to Working solution: Problem - "Open WS Connection on TUniLoginForm"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...