Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check that the connection is open? #2

Open
majidbigdeli opened this issue Jun 22, 2019 · 25 comments
Open

How to check that the connection is open? #2

majidbigdeli opened this issue Jun 22, 2019 · 25 comments
Labels
enhancement New feature or request

Comments

@majidbigdeli
Copy link

majidbigdeli commented Jun 22, 2019

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

@kataras
Copy link
Owner

kataras commented Jun 23, 2019

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

@majidbigdeli
Copy link
Author

majidbigdeli commented Jun 24, 2019

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

@majidbigdeli
Copy link
Author

majidbigdeli commented Jun 24, 2019

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 majidbigdeli changed the title how to check that the isconnect() is true for receiving event How to check that the connection is open? Jun 24, 2019
@kataras
Copy link
Owner

kataras commented Jun 25, 2019

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 kataras added the enhancement New feature or request label Jun 25, 2019
@majidbigdeli
Copy link
Author

majidbigdeli commented Jun 25, 2019

@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

@kataras
Copy link
Owner

kataras commented Jun 25, 2019

OK let me implement it now then :)

@kataras
Copy link
Owner

kataras commented Jun 25, 2019

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

@majidbigdeli
Copy link
Author

majidbigdeli commented Jun 25, 2019

@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 majidbigdeli reopened this Jun 25, 2019
@majidbigdeli
Copy link
Author

majidbigdeli commented Jun 25, 2019

@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.

@kataras
Copy link
Owner

kataras commented Jun 25, 2019

It's my pleasure @majidbigdeli !!!

It's done with neffos.js: d3bb155 and neffos: kataras/neffos@5314c23.

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

}, {
// if > 0 then on network failures it tries to reconnect every 5 seconds, defaults to 0 (disabled).
reconnect: 5000,
// custom headers:
headers: {
// 'X-Username': 'kataras',
}
});

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.

_OnNamespaceConnected: function (nsConn, msg) {
if (nsConn.conn.wasReconnected()) {
addMessage("re-connected after " + nsConn.conn.reconnectTries.toString() + " trie(s)");
}

srv.OnConnect = func(c *neffos.Conn) error {
if c.WasReconnected() {
log.Printf("[%s] connection is a result of a client-side re-connection, with tries: %d", c.ID(), c.ReconnectTries)
}

@kataras
Copy link
Owner

kataras commented Jun 25, 2019

@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:

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

Hope I helped you

@majidbigdeli
Copy link
Author

@kataras You are wonderful.

kataras added a commit to kataras/neffos that referenced this issue Jun 26, 2019
kataras added a commit that referenced this issue Jun 26, 2019
…d wasReconnected(): boolean method to check if a connection is a result of a reconnection - same as the go server side #2
@kataras
Copy link
Owner

kataras commented Jun 26, 2019

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
Copy link
Owner

kataras commented Jun 27, 2019

Updated: reconnection is available on nodejs side as well.

@majidbigdeli
Copy link
Author

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

@kataras
Copy link
Owner

kataras commented Jun 30, 2019

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

neffos.js/src/neffos.ts

Lines 871 to 874 in ca73e99

let endpointHTTP = endpoint.replace("ws://", "http://");
if (endpoint.startsWith("wss://")) {
endpointHTTP = endpoint.replace("ws://", "https://");
}

_fetch(endpointHTTP, fetchOptions).then(() => {

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).

@majidbigdeli
Copy link
Author

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

@kataras
Copy link
Owner

kataras commented Jul 1, 2019

Hey no worries @majidbigdeli !!!

Does the new neffos.js version fixed the issue?

@majidbigdeli
Copy link
Author

majidbigdeli commented Jul 1, 2019

@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

@kataras
Copy link
Owner

kataras commented Jul 1, 2019

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).

neffos.js/src/neffos.ts

Lines 868 to 869 in 374700d

// Chrome itself is emitting net::ERR_CONNECTION_REFUSED and the final Bad Request messages to the console on network failures on fetch,
// there is no way to block them programmatically, we could do a console.clear but this will clear any custom logging the end-dev may has too.

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

@majidbigdeli
Copy link
Author

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( 7384e2d ) for version 0.0.18
Thank You my friend.

@kataras
Copy link
Owner

kataras commented Jul 1, 2019

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)
  // [...]
}

@majidbigdeli
Copy link
Author

@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.

@kataras
Copy link
Owner

kataras commented Jul 2, 2019

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

@majidbigdeli
Copy link
Author

majidbigdeli commented Jul 4, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants