Advice about where put a code to websocket

<< Click to Display Table of Contents >>

Navigation:  Forum >

Advice about where put a code to websocket

Forum link

 


 

07-10-2019, 03:08 PM:

 

Hi,

 

I have many iw forms, all inherited from a standard iwform(TIWFormStd).

 

I have a MasterTemplate html to all my IWForms.

 

I have my websocket server component in servercontroller module(I don´t know  if this is the better place)

 

I need the websocket client available in all forms, because my app has a chat to users.

 

In that MasterTemplate, I have the next script code to connect to websocket server:

 

Code:

 

<script>

 

   function wschat()

 

    {

 

        if (!ws || ws.state()!='open'){

 

          ws = new sgcWebSocket('ws://127.0.0.1:5414/auth/url/admin/1234');

 

        }

 

       ws.on('open', function(evt){

 

          console.log(ws.state())

 

        });   

 

        ws.on('message', function(evt){

 

          console.log(evt.message)

 

        })

 

       

 

   }

 

    wschat()

 

</script>

 

 

 

My problem is that when I open a new iwform, I allways release the old iwform. As the old form is released, the websocket connection in the frontend  is  destroyed as well and recreated when the new IWForm is active.

 

I´d like to know some suggestions about where I can put the websocket client code to avoid  it.

 

Regards, Luiz

 


 

07-11-2019, 01:07 PM:

 

Hi Luiz. What websocket component are you using ?, thank you.

 


 

07-11-2019, 05:09 PM:

 

Hi, I am using sgcwebsocket. But I can create a nodejs server using socketio. The server is not a problem. The problem is in the frontend. The js using sgcwebsocket is similar to socketio. When i change the form in front end the webssocket client is closed because the old form is released and recreated with the new form.  My app has a chat and i intent to use webssocket or socketio to It.

 


 

07-11-2019, 06:15 PM:

 

You need to store your websocket connection in a global space such as the window object to persist it.

 


 

07-11-2019, 06:30 PM:

 

(07-11-2019, 06:15 PM)kudzu Wrote: [ -> ]You need to store your websocket connection in a global space such as the window object to persist it.My forms use MasterTemplate, the js code is into Mastertemplate HTML. Is possível the code survive into window object when I load a new form If the old is released? If yes, could you point me some sample code? I use inherithance  in forms.

 


 

07-12-2019, 03:38 PM:

 

My mistake. I'm focused on 17. 17 can persist objects as you wish.

 

14/15 reload the whole page when pages change. Other than using a frame which would be a bit messy, I can't see a way to persist a JS object. You'll have to reconnect it each time I think if you change pages.

 

If you are only making updates to a page, you can use AJAX updates which will preserve your websocket object.