neffos.js icon indicating copy to clipboard operation
neffos.js copied to clipboard

How to check that the connection is open?

Open majidbigdeli opened this issue 5 years ago • 25 comments

What's your idea about this issue that the event will not be caught e.g 5 hours after connecting, When we press the F5 key (refresh) the problem will be fixed. Is it possible for network problems ? If the problem is this, how to fix it? E.g. sending a request to the server again for connecting

majidbigdeli avatar Jun 22 '19 14:06 majidbigdeli

Hmmm I don't quite understand, can you please describe it a bit more?

kataras avatar Jun 23 '19 18:06 kataras

Yes, the explanation is complicated, and my explanation was bad. How to check that the connection is open?

majidbigdeli avatar Jun 24 '19 07:06 majidbigdeli

Of course, I have not seen any problems so far. I just want to re connect if the connection was interrupted. Without refresh page.

majidbigdeli avatar Jun 24 '19 07:06 majidbigdeli

aaa you talk about automatic reconnection, yes I had that in mind but let's not hurry, this is an active project and the next update will contain automatic reconnection, we don't have to implement them from version zero, good feature request though.

kataras avatar Jun 25 '19 06:06 kataras

@kataras yes I Talk about automatic reconnection. please see my code .

async function runServer() {
                try {
                    var wsURL = 'wss://wss.ami.dev.iwin.local:3811/echo';
                    const conn = await neffos.dial(wsURL, {
                        default: {
                            _OnNamespaceConnected: function (nsConn, msg) {
                                console.log("connect to server");
                                setInterval(function () { checkConnectStatus(nsConn)}, 10000);
                            },
                            _OnNamespaceDisconnect: function (nsConn, msg) {                            
                                console.log(err);
                            },
                            showForm: function (nsConn, msg) {

                            }
                        }
                    }, { headers: { 'Extension': 2009 } });
                    conn.connect("default");
                } catch (err) {
                    console.log(err);
                }
            }
function checkConnectStatus(nsConn) {
                console.log('checkConnectStatus...')
                var resp = nsConn.emit("heartBit", "");
                if (resp === false) {
                    console.log(resp);
                    var postdata = {};
                    postdata["category"] = 'PopUp_Error';
                    postdata["customData"] = JSON.stringify(resp) ;
                    postdata["desc"] = 'server is disconect during work!';
                    postdata["message"] = 'server is disconect during work!';
                    logError(postdata);
                    console.log('server is disconect!')
                    console.log('reconnected...')
                    runServer();
                }
            }


I have a lot of '''server error disconect during work!'''

my log in database

image

majidbigdeli avatar Jun 25 '19 06:06 majidbigdeli

OK let me implement it now then :)

kataras avatar Jun 25 '19 06:06 kataras

However you can check if it's closed by nsConn.conn.isClosed() *

kataras avatar Jun 25 '19 06:06 kataras

@kataras thank you . You're so good .

@kataras

I really owe you Because the projects of iris and neffos have helped me a lot. And when I have a problem, you fix it.

When I used the signalr in .NET or when I used socketio, I had a lot of memory usage problems in browser.

But in neffos, I can open several tabs without a memory usage problem. Really thank you.

majidbigdeli avatar Jun 25 '19 06:06 majidbigdeli

@kataras I have another question. How to send event to a tab from several tabs opened by the user? For example, check if the user is connected to the server in another tab Do not connect again to the server in the new tab. it is important . Because we want to open a new window when receiving the event. In this case, several new windows will open.

I'm trying to find a solution to avoid multiple connections over multiple open tabs or multiple browser.

majidbigdeli avatar Jun 25 '19 09:06 majidbigdeli

It's my pleasure @majidbigdeli !!!

It's done with neffos.js: https://github.com/kataras/neffos.js/commit/d3bb1559a4e8fe503bf2b96a6a94e8bc9d82a23c and neffos: https://github.com/kataras/neffos/commit/5314c232169169ed8f83404ce478fb1df4aa8316.

Simple by pass a options.reconnect: 5000 (milliseconds to try reconnect, 0 means disabled, defaults to 0) to the dial function.

https://github.com/kataras/neffos.js/blob/d3bb1559a4e8fe503bf2b96a6a94e8bc9d82a23c/_examples/browserify/app.js#L52-L59

UPDATE

I also added some features that you didn't ask but I think you will love it, such as count of total retries on suceed reconnection on the server-side.

https://github.com/kataras/neffos.js/blob/91eb944c2157d29f136cc3715f5c2265431eef1a/_examples/browserify/app.js#L36-L39

https://github.com/kataras/neffos.js/blob/91eb944c2157d29f136cc3715f5c2265431eef1a/_examples/server.go#L91-L94

kataras avatar Jun 25 '19 10:06 kataras

@majidbigdeli You need to communicate across web browser windows, this requires some knowedge about the window api and how postMessage works, read the links below to learn how:

  • https://developer.mozilla.org/en-US/docs/Web/API/Window
  • https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
  • And a good video tutorial to watch about these things is created by @linkedin and @lynda, https://www.lynda.com/JavaScript-tutorials/Web-Security-Same-Origin-Policies/794147-2.html look at its fifth section: Communicating Securely across Browser Windows.

For nodejs and go side there is a wrap-users-conn example at the neffos repository.

Hope I helped you

kataras avatar Jun 25 '19 10:06 kataras

@kataras You are wonderful.

majidbigdeli avatar Jun 25 '19 12:06 majidbigdeli

Thanks @majidbigdeli !!! I updated the API a bit giving some more information on the connection itself, go: Conn.ReconnectTries int and Conn.WasReconnected() bool (the server can decide if allow this reconnection or not or after how much tries is not allowed for example), js: nsConn.conn.reconnectTries number and nsConn.conn.wasReconnected() boolean (on javascript we don't have a limit of maximum tries, this can be implemented easly if some1 ask, it doesn't mean a lot though) :)

see my comment above

kataras avatar Jun 26 '19 18:06 kataras

Updated: reconnection is available on nodejs side as well.

kataras avatar Jun 27 '19 10:06 kataras

hello @kataras
When I disconnect and reconnect the network, I get error. neffos.min.js?v980406:1 Fetch API cannot load wss://wss.ami.dev.iwin.local:3811/echo. URL scheme must be "http" or "https" for CORS request.

image

majidbigdeli avatar Jun 30 '19 20:06 majidbigdeli

@majidbigdeli, no need to duplicate the issues, this belongs to this repository:P

https://github.com/kataras/neffos.js/blob/ca73e99f911f254af7b6f5226d6dc21693a7b30e/src/neffos.ts#L871-L874

https://github.com/kataras/neffos.js/blob/ca73e99f911f254af7b6f5226d6dc21693a7b30e/src/neffos.ts#L884

But yes... javascript replace works with regexp, funny that it worked locally, anyway...

I'll replace it with this one instead: let endpointHTTP = endpoint.replace(/(ws)(s)?\:\/\//, "http$2://"); please update and tell me if that did the job.

Sorry for the delay but I was working with the wiki and a pdf book of the neffos (on the neffos/README).

kataras avatar Jun 30 '19 23:06 kataras

@kataras I am Sorry. Sorry . I did not know it is a client or server problem.

majidbigdeli avatar Jul 01 '19 09:07 majidbigdeli

Hey no worries @majidbigdeli !!!

Does the new neffos.js version fixed the issue?

kataras avatar Jul 01 '19 15:07 kataras

@kataras

I get another issue

I get
neffos.min.js?v980406:1 HEAD https://wss.ami.dev.iwin.local:3811/echo net::ERR_NETWORK_CHANGED in disconnect network

and when I re connect network i get Access to fetch at 'https://wss.ami.dev.iwin.local:3811/echo' from origin 'https://dev.iwin.ir' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

image

I get two server ; 1 panel (https://dev.iwin.ir) and 2 neffos server (wss.ami.dev.iwin.local:3811) that this call in panel

majidbigdeli avatar Jul 01 '19 15:07 majidbigdeli

OK that's normal, first of all:

The ERR_NETWORK_CHANGED as I describe in my code comments, is coming from the google chrome browser and there is no way to disable it ( we could clear the whole console's browser but that way any custom log messages would be dissapear and we don't want that).

https://github.com/kataras/neffos.js/blob/374700d4f05de8ed3f142a3a8ae898cc1b0bc885/src/neffos.ts#L868-L869

The second one is easy to fix, I wanted to do it but I didn't think was necessary, as it seems it is. Update to 0.0.18 and tell me the results, thanks a lot @majidbigdeli

kataras avatar Jul 01 '19 16:07 kataras

thanks a lot @kataras I fix issue with add cors in server side

	serveMux := http.NewServeMux()
	serveMux.Handle("/echo", server)
	th := http.HandlerFunc(timeHandler)
	broadcast := http.HandlerFunc(broadcastHandler)
	serveMux.Handle("/time", th)
	serveMux.Handle("/broadcast",broadcast)

	handler := cors.Default().Handler(serveMux)
        log.Fatal(http.ListenAndServeTLS(addr, certPath, keyPath, handler))

.

But I now see Your last Commit( 7384e2d716af68f55fc4425e39d9368c946fe1a9 ) for version 0.0.18 Thank You my friend.

majidbigdeli avatar Jul 01 '19 16:07 majidbigdeli

Nice @majidbigdeli , however, you don't need an external middleware for that, just set a header:

w.Header.Set("Access-Control-Allow-Origin", "*")

can I ask why you don't use Iris in this one? You can use it with Iris v11.1.1 too:

app.Get("/echo", iris.FromStd(server))

and on v11.2.0 you can optionally do the following to have iris.Context access too:

import (
  // [...]
  "github.com/kataras/iris/websocket"

  "github.com/kataras/neffos"
)
app.Get("/echo", websocket.Handler(server))
neffos.OnNamespaceConnected: func(nsConn *neffos.NSConn, msg neffos.Message) error {
  // with `websocket.GetContext` you can retrieve the Iris' `Context`.
  ctx := websocket.GetContext(nsConn.Conn)
  // [...]
}

kataras avatar Jul 01 '19 17:07 kataras

@kataras yes . I just use iris . Thank you . I use the neffos in the company for the voip project with 60,000 daily calls. I open the crm form with the neffos on the incoming call. Neffos is great. Everyone should know and use neffos.

majidbigdeli avatar Jul 02 '19 16:07 majidbigdeli

You are amazing @majidbigdeli thank you for your truly support once again!

Earlier today I've pushed a new Iris feature that some of my clients wanted as well, it's not the correct repository and issue to describe it, but in short it offers request authentication and verification that your company may want to use at: https://github.com/kataras/iris/blob/660849d01ce59cf926ffbb6df83d1385cdb239db/_examples/authentication/request/main.go

kataras avatar Jul 02 '19 17:07 kataras

You are amazing @majidbigdeli thank you for your truly support once again!

Earlier today I've pushed a new Iris feature that some of my clients wanted as well, it's not the correct repository and issue to describe it, but in short it offers request authentication and verification that your company may want to use at: https://github.com/kataras/iris/blob/660849d01ce59cf926ffbb6df83d1385cdb239db/_examples/authentication/request/main.go

@kataras I read this. Thank you.

majidbigdeli avatar Jul 04 '19 05:07 majidbigdeli