Bug? [ JServer ~ b4j_ws.js ] when disconnect and reconnect all events are duplicated

Waldemar Lima

Well-Known Member
Licensed User
Hey guys ! I believe I found a bug, when I start a connection using b4j_ws.js the client is disconnected from the server and tries a new reconnection, all events are duplicated (always send 2 events equal to the server)...
And only goes back to normal, if I reload the client (html page).

2021-05-28 22:38:33.651:INFO::main: Logging initialized @151ms to org.eclipse.jetty.util.log.StdErrLog
2021-05-28 22:38:33.828:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2018-05-03T15:56:21.710Z; git: daa59876e6f384329b122929e70a80934569428c; jvm 1.8.0_291-b10
2021-05-28 22:38:33.888:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2021-05-28 22:38:33.888:INFO:oejs.session:main: No SessionScavenger set, using defaults
2021-05-28 22:38:33.890:INFO:oejs.session:main: node0 Scavenging every 600000ms
2021-05-28 22:38:33.919:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@5442a311{/,file:///D:/Waldemar%20Lima/Documents/b4j/Libraryes/GuessMyNumberWithReconnection/WebAppHelloWorld/Objects/www/,AVAILABLE}
2021-05-28 22:38:33.925:INFO:oejs.AbstractNCSARequestLog:main: Opened D:\Waldemar Lima\Documents\b4j\Libraryes\GuessMyNumberWithReconnection\WebAppHelloWorld\Objects\logs\b4j-2021_05_29.request.log
2021-05-28 22:38:34.208:INFO:oejs.AbstractConnector:main: Started ServerConnector@1e397ed7{HTTP/1.1,[http/1.1]}{}
2021-05-28 22:38:34.209:INFO:oejs.Server:main: Started @713ms
Connected  ' first connection
Creating new state. Number = 83
(MyMap) {which=229, metaKey=false, target=txtnumber}
(MyMap) {which=229, metaKey=false, target=txtnumber}
Waiting for value (4 ms)
onWebSocketError: null '  turning off the wifi  
Connected '  turning the wifi on again  
Reusing previous state. Number = 83
'  I just clicked the button once: "Guess!" and log 2 events in same time ...
Waiting for value (6 ms) 
Waiting for value (28 ms)

screenshot explaining :

is it a bug or is it something I'm not seeing?

Waldemar Lima

Well-Known Member
Licensed User
You need to show your code (modified). I don’t experience this issue in the sample project.

the code was not modified, although at first I thought it was a problem in the js code that I edited ... but to be absolutely sure, I downloaded the official version, and I did the process above and the problem persists ...

Waldemar Lima

Well-Known Member
Licensed User
Would this affect your project?
If the server returns the value of previous state correctly (after reconnected) then I think should be no problem. Right?

couldn’t those duplicate messages be a problem later?

I recorded a video showing that each time the Client reconnects to the server, it increases +1 message to be sent ... is it that with each reconnection it is generating a new ws connection?

Video Here

Waldemar Lima

Well-Known Member
Licensed User
If you kill the process (and recompiling the project), I think it is recreating a new connection (re-initialize) a new object to create a new connection.
The reconnecting doesn’t terminate the existing process, it just attempting to continually use the existing connection.

well, I did other tests, and the fact that the user "loses" the connection due to lack of internet or turning off the wifi, causes the same problem as "killing" the server, I recorded a video below :

Video Here

I don’t agree it is a bug. It may be how this is the script working. I am not expert in this. I can only guess this is how under the hood the JavaScript has its own events or hook like something mounted and unmounted. (I learned this concept recently in a vue course)

the problem is that I like to think about all the consequences that a function that I don't really understand could cause in case of production... you know? xD


Licensed User
Longtime User
Quote Erel note
The server doesn't distinguish between an automatic reconnection to a regular connection. In both cases a new WebSocket handler instance is created.
You can use the HttpSession to restore the previous state.
You mean if too many disconnection (and reconnection) happened from the clients would cause the server to handle “too many connections” ?