afrad / angular2-websocket

Websocket wrapper for angular2 based on angular-websocket
Apache License 2.0
210 stars 84 forks source link

Websocket is opening always twice connection #76

Closed mburger81 closed 7 years ago

mburger81 commented 7 years ago

I'm using your lib in version 0.9.1 and have an issue which I didn't seen before.

On opening a simple websocket connection like this

        this.ws = new $WebSocket(this.httpService.getFullUrl('/wsapi/v1/sensorsws'));
        this.ws.connect();

        this.ws.onOpen(function() { that.webSocketOpen(); });
        this.ws.onMessage(function(evt) { that.webSocketMessage(evt); });
        this.ws.onClose(function(evt) { that.webSocketClose(evt); });

the client opens always to connection on server and on the client the onOpen listener is called also twice times! I think this behavior wasn't the version I used before.

mburger81 commented 7 years ago

@adel-frad @afrad Can someone check this?

bougnat12 commented 7 years ago

I confirm than with 0.8.1 release we have only connection on server but two with 0.9.1. As I don't see any change about this in 0.9.2, I suppose it's not corrected but I don't have make the test.

mburger81 commented 7 years ago

@bougnat12 I tested it today and I confirm, the error persist on 0.9.2.

mburger81 commented 7 years ago

Can anyone help or provide a solution??

rawnsley commented 7 years ago

@mburger81 What is the readyState of the connection for both calls? I'm seeing CONNECTING (0) then OPEN (1) under some circumstances in Chrome.

mburger81 commented 7 years ago

@rawnsley first of all thx for the response! This is my console output I created

DashboardPage#webSocketConnect
dashboard.ts:86 Socket Opend, send Hello message
dashboard.ts:88 this.ws.getReadyState() 0
dashboard.ts:86 Socket Opend, send Hello message
dashboard.ts:88 this.ws.getReadyState() 1

So you can see, I call only once the connect() command but the onOpen() event is called twice, first time the getReadyState() is 0 and second time it is 1. I think this wasn't before so. The problem it seems I have open two connections!

Are you notice the same behavior?

Edit: As you can see there are two open WS connections which are pending!! image

rawnsley commented 7 years ago

@mburger81 At least I'm not the only one. According to the Mozilla documentation this isn't correct, but I can't find a definitive answer in the actual specification. Technically the socket is open I suppose even though it is not connected.

It seems to happen for me if the browser tries and connect to a server that isn't available immediately. Presumably it enters some sort of polling loop that keeps trying until it times out, but if it does manage to connect, you get this additional CONNECTING call to onopen. This may be why it hasn't been seen more widely: people are mostly connecting to reliable Internet-based WebSocket servers that don't come-and-go. Personally I'm connecting to micro-servers on devices with sporadic network connections; from the IP-based URL in your screenshot I would guess you are doing something similar.

I'm not sure that the "Pending" in the Time column of the Chrome dev tools is accurate; it doesn't seem to update after a connection event has a timeout. The Status column does seem accurate, and goes either to Finished or 101 Switching Protocols.

I don't see the double connections you are seeing, although I'm not 100% certain because I am also debugging a problem where my code calls connect() multiple times (Javascript timeouts going mad). I would double-check you aren't doing the same and then maybe check how many connections the other device is seeing to confirm the problem.

mburger81 commented 7 years ago

Hi @rawnsley, there are others like us!!! But the creator does not respond, and you are the only one which are so nice to answer and perhaps you can help us!!!

I'm really sure 99% :smile: that the I'm opening only one a connection to the server and we are also really sure that we have two opened connection, because we get two "response" from server with the same data.

So for sure there are two valid connections.

In my opinion this wasn't before version 0.9. But if I'm understand what you write here, you will say this should not be a problem of the lib but a problem of chrome?

Looking to the timetable to firefox, it is different. image But I think, this is also not the right one, they seems to be closed, but this is not so!

I'm sure there are two socket opened.

Do you have in meantime found a solution for that??

rawnsley commented 7 years ago

@mburger81 I think there may be two problems: the double call to onopen (which I'm pretty sure is a Chrome bug / undocumented behaviour) and the double socket connection.

I don't get the double socket problem, but I'm not actually using this library (I'm just passing through). When I open a connection using WebSocket.connect in Javascript (no libraries) I get a single connection. Is it possible for you to make a connection directly without using the library? This might help you work out where the fault lies.

If the library is having a problem you might be able to trace it through the source code.

mburger81 commented 7 years ago

So you are not using this angualar lib? But use plain javascript/browser implementation where you have the same error. The problem is, I have also the two open events on firefox, so I'm not sure if it is only a problem of chrome!

There is no value angular2 websocket lib. Probably wr have to migrate to Observable/Rx Websocket integration

mburger81 commented 7 years ago

Now I'm using simple RxJs implementation like this https://medium.com/@lwojciechowski/websockets-with-angular2-and-rxjs-8b6c5be02fac which is working great. And @rawnsley there is now problem with two opened connection, also not on Chrome. Perhaps this implementation has not all the tested functions as the lib of this repository, but at least there is a community and it does work.

The implementation is working well, also If I had to do still some testing and functions like reconnecting and others.

I leave the issue open, because there is still someone which can need help and there is still the bug present.

mburger81 commented 7 years ago

@afrad @adel-frad do you think you can help us for this? Or at least comment this? Please

afrad commented 7 years ago

@mburger81 I need to debug it. I have limited time for now. I'll try to check in the coming days. However this issue may be browser dependent. Is this also happening in other browsers except Chrome?

mburger81 commented 7 years ago

@afrad Sorry for my late response, we tested it on Chrome and also on Firefox, there is the same behavior. I'm not an expert for WebSockets, but I think this closing behavior wasn't before and I think the WebSocket should not timed out after a few seconds.

Where you in the meantime able to reproduce the situation?

mburger81 commented 7 years ago

I found probably the problem!

On commit 908aa55f87dc312c0d97546a9f042890b8e45f78 you added this.connect(true); to the constructor. But we always have the manual connect on our code

this.ws = new $WebSocket(/wsapi/v1/blabla'));
this.ws.connect();

If we comment manual connect()there are only one valid connection!!

Can someone confirm this?

afrad commented 7 years ago

@Lyoko-Jeremie could please confirm?

mburger81 commented 7 years ago

In this case also bug #85 is obsolete and resolved. It turned out we was not able to close the second opened connection. If we have only one connection, the closing behavior is standard and as expected!! We can close the connection manually and on closing the browser/tab the connection also is closed!

mburger81 commented 7 years ago

I think we can close this error, I had never this error again!