jetty 9, websockets and closing websocket connection on server side - java

I have an application that uses a websocket server based on jetty 8. Everything is working great, and when the user refreshes the page, closes the page or move to another page the connection is closed and, on the server side the onClose event is triggered.
I just moved to jetty 9 and in firefox everything works the same ( on refresh the connection is closed) but not on chrome or opera. I even close the browsers and the onClose event didn't triggered.
I know jetty 9 only supports the latest websockets protocol version, and I wanna know if the browser is sending some message to the server when the user refreshed the page for example, and how to handle this on jetty or do I have to handle this in javascript like in this example : Closing WebSocket correctly (HTML5, Javascript) using window.onbeforeunload ?
I would preffer if I can handle all the work on the server side. Thanks
UPDATE
If I close the connection from the client using ws.close() everything works great.

There were a few close and disconnect detection bugs in Jetty 9.0.0
406449 - Session's disconnect not detected
404991 - WebSocketCloseTest fails spuriously
Both of those were recently fixed and delivered in Jetty 9.0.3.v20130506 please give that version a try.

Related

Can't connect to TeamCity server itself

I've seen a lot of posts on the build agent not being able to connect to the server, but in my case I can't connect to the server at all.
I'm using TeamCity 10, on CentOS 7 with the Oracle JDK installed. I've started the server using the runAll script and have not changed anything in the configuration. I opened port 8111 with FirewallD to access the setup screen.
The problem is, I can't access the setup screen. I open the page in Chrome and it just hangs "connecting". I tried Firefox too, and also CURL. It appears that the TCP connection is accepted, and the HTTP request is sent, but no response comes at all.
The logs do not show that anything is wrong. The build agent log fails to register to the server, however, saying it is in "maintenance mode". This is a fresh install.

Push notification using atmosphere

I am using Atmosphere RC 2.4.5(java 1.8, jsp, tomcat 8, and javascript Atmosphere client) for pushing messages from server to the clients. Everything works as expected, and didn't have any trouble with the implementation of it, but now I have doubt about the next thing:
I implemented a feature where admin user can send some notification to all users currently using the app. But if in that exact time while the message was pushing through web sockets, some user clicked link and started navigation to another page. His web socket would be closed and he would never get the message.
Does anyone know how this can be achieved using the atmosphere, so no messages would be lost.
Thanks all.
You can find your solution based on this page from documentation of atmosphere-js :
Sharing connection between Browser's windows and tabs
By default, the atmosphere.js library will open a new connection (based on the available transports: websocket, long-polling, streaming, sse, jsonp or ajax) every time a new window or tab is opened by the Browser. In some case it may be more efficient to share a connection between windows/tabs. To enable the mechanism, all you need to do is to set the shared property of an AtmosphereRequest object:
Here's example :
var request = new $.atmopshere.AtmosphereRequest();
request.shared = true;
request.transport = 'websocket'
$.atmosphere.subscribe(request);
One of these lines can be the solution of your problem :
request.shared = true;

Disconnect from EventSource not working

I have a Play 2 (2.3.7) app that uses EventSource connections. Everything works fine except when I try to close the connection from the client. To do that I am using the ES.close method, but even after I disconnect Play only detects that the connection was closed (and processes the onDisconnect callback) when I send a ping to the client. Closing the connection from server side works as expected.
I tried this in Chrome/Firefox (using native implementation) and using Yaffle ES polyfill and the result is the same. Both client and server are running in a linux machine.
EDIT: After closing the connection in the browser console and inspection the network tab in dev tools it seems the connection is indeed closed by the browser. So this may be a problem of Play?
So what may be the problem here?

WebSocket connection interrupted when Apache HTTPD in front of Tomcat

I try to use Tomcat implementation of WebSockets on local machine.
When I use only tomcat everything works fine, but when I start use bundle Apache + mod_jk + Tomcat browser show me 'The connection to ws://example.com/test/echoMessage was interrupted while the page was loading' after couple of seconds of work. WebSocket connection is closed after this.
Can anyone tell me how to fix it?
I have: Apache HTTPD 2.2.18, mod_jk 1.2.37, Tomcat 7.0.28
Here is server side code that I use: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/ and client side: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/websocket/
Browsers: Firefox 12, Chrome 19
I would research whether Apache mod_proxy is able to transparently forward traffic at the TCP level at all. Since that is likely needed for WebSocket to pass through. IOW: is mod_proxy able to transparently reverse proxy WebSocket traffic?
But why would you want to bring Apache in the line anyway? It doesn't add any value, only more parts which can break. That is for WebSocket of course ..

websocket server for android, java

I am building a simple websocket server for Android. (let us, say 3.2 - I tried some earlier Android versions with the same result). Before trying the code on Android, I tested it under Java SE on Windows, Linux. It worked OK with Chrome and Firefox .
However on Android it does not work. Please see the comments in code.
In short:
I successfully pass handshaking and browser confirms that connection is OK.
I happily receive all the messages from the browser via Websocket
I cannot send anything: It just does not get to the javascript callback.
After some retries (sometimes immediately) the browser closes websocket.
Here is the code:
//BufferedOutput myout=new BufferedOutput(mysocket.getOutputStream()));
//myout was successfully used for handshaking before
String msg=”I want to send this”;
try {
myout.write(129);//129=0x81); text frame 0x81=129
int payloadlen=msg.length();
myout.write(payloadlen);
byte[] bts=msg.getBytes(Charset.forName("UTF-8"));
myout.write(bts,0,payloadlen);
myout.flush();//after this command client Browser closes websocket
//firefox also gives error
}
catch(IOException ee)
{
System.out.println("Error sending websocket message"+ee);
try{myclose();}catch(IOException eio){}
}
}
Thank you
Where are you running this code? Inside an Activity or in a Service.
Notice that if you need to create your one customized Web Server and need to run it on Android you should implement it as a Service running on it own thread.
If you are running it on an Activity depending of the android version, the main thread should not (or can not, depending of the android version) perform I/O operation on the main thread.
I hope it helps.
I solved it. The reason was that in case of Android, I was sending something BEFORE via the SAME connection. And this "something", though was wrong, somehow did not produce any error in Chrome/Firefox. It took me two days and byte-to-byte comparison Wireshark results for both cases: my Sun Java Websocket Server and my Android Websocket Server.

Categories

Resources