Disconnect from EventSource not working - java

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?

Related

Client/Server java - connection refused

I'm trying to write basic client server. The code works fine when I'm using "localhost" and random port. The problems start when I run the server and the client from different hosts, and then I get "Connection Refused".
I searched for this problem and none of the solutions helped (Disable the firewall, run the server before the client, connection to the internet etc)
Again, the code works fine, my firend ran it and all works fine. The problem is probably with my computer. I have a router.
Thank you

Bluecove Close connection issue

I use Bluecove 2.1.1 and trying to restart bluetooth service, but after closing connection and reopening I can't connect to service, I got "Connection refused" exception. I found this issue on Bluecove site? I also try to change service name(also UUID) after closing connection, but it's not help. Does anyone know workaround for it? I'm simply need to restart bluetooth service.
I am not sure if I understood your question in the way it meant to be.
I assume that you are opening a Connector and let a SessionNotifier handle OBEX connections with accecptAndOpen(ServerRequestHandler).
When a clients connects to your bluetooth service the specific methods in your ServerRequestHandler are called (onGet, onPut, ..). When the client disconnects onDisconnect is triggered. Now you need to acceptAndOpen your service again to handle future connections.

jetty 9, websockets and closing websocket connection on server side

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.

Switching from WiFi connection to 3g causes connection to hang?

I have an application which connects to a web service. I can connect to the web service any number of times without any problem using WIFI or 3G provided that I stay loyal to my connection type during the life cycle of my application. That is if I don't switch from WIFI to 3G. If I switches from WIFI to 3G, I can't get a response anymore. My connection just keeps on waiting for response.
I tried 4 scenarios below. I'm only having problem with the 3rd scenario. What could be the problem?
1st Scenario: Connection is always on WIFI (Ok)
Application connects to a web service using WIFI.
Response was received successfully.
Application connects again to a web service using WIFI.
Response was receive successfully.
2nd Scenario: Connection is always on 3G (Ok)
Application connects to a web service using WIFI.
Response was received successfully.
Application connects again to a web service using WIFI.
Response was receive successfully.
3rd Scenario: Connection switches from WIFI to 3G (No response)
Application connects to a web service using WIFI.
Response was received successfully.
Connection was switched to 3G. WIFI is disabled. 3G is enabled.
Application connects again to a web service using 3G.
No was response or error was received. Application keeps on waiting for response. Last log was displayed before getResponseCode was called.
4th Scenario: Connection switches from 3G to WIFI (Ok)
Application connects to a web service using 3G.
Response was received successfully.
Connection was switched to WIFI. 3G is disabled. WIFI is enabled.
Application connects again to a web service using WIFI.
Response was receive successfully.
My guess is by default, HttpURLConnection thinks WIFI as a better connection type compare to 3G. So when the connection is switched from WIFI to 3G, HttpURLConnection refuses to acknowledge 3G and still tries to connect using WIFI. On the other hand, HttpURLConnection allows switching from 3G to WIFI since WIFI is a better connection type. Am I correct with this? If so, how do I allow switching from WIFI to 3G?
Below is a snippet of my code: (I call it every time I connect to a web service.)
//open new connection
httpsURLConnection = (HttpURLConnection) ((new URL(url)).openConnection());
httpsURLConnection.setDoInput(isDoInput);
httpsURLConnection.setDoOutput(isDoOutput);
try
{
//supply parameters
OutputStreamWriter wr = new OutputStreamWriter(httpsURLConnection.getOutputStream());
wr.write(data);
wr.flush();
if(httpURLConnection != null)
{
if (httpsURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) //connection hangs here
{
//some code
}
else
{
//some code
}
}
}catch(Exception e)
{
}
I'm not sure why but adding System.setProperty("http.keepAlive", "false") on the code solves the problem.
According to the Android Developers Blog (androids-http-clients), HttpURLConnection has a bug prior to Froyo and it can be solved by disabling connection pooling just like above. However, I'm using Gingerbread so I'm not sure why HttpURLConnection is still misbehaving on my application.
To others: If you can provide more explanation, please don't hesitate to edit my post.
when you make a connection on wifi and it switches back to 3G, do you see any connection drop exceptions?
From what i know, if you open a connection on wifi the same connection can't be used for 3G. you need to teardown existing connection and reestablish new connection on 3G.
I am guessing your problem could be because you haven't set any connection timeouts for your requests and as a result when connection drops it takes a long time for HttpUrlConnection to realize ur connection is lost.
I have put together utility libraries on http connections with some default timeouts, see if this can help you anyway
https://github.com/nareshsvs/android-libraries

Cannot connect to my HTTP server running on my Android phone

I'm trying to run a small, custom HTTP server embedded in my Android application (to aid in debugging the app). However, I cannot reliably to connect to the HTTP server from my desktop web browser when the server is running on my Android 2.3.4 phone.
I know the HTTP server code works (at least the basics), because one time the connection went through fine. And the code works reliably when run on my desktop (my app is a libgdx-based, pure Java app that can be built for Windows or for Android).
I have the Android permissions correct, because I was getting the PermissionDenied fault (when setting up the socket listener) thrown before I added the android:name="android.permission.INTERNET" permission. The server-side code runs in its own thread and boils down to:
ServerSocket ss = new ServerSocket(serverPort);
Socket clientSocket = ss.accept();
I'm using port 8080. (I tried port 80 but hit the must-be-root-below-port-1024 "feature" of Linux).
The LogCat output looks fine. My app just prints its "listening on port 8080" message and no exceptions seem to be thrown.
Network connectivity seems fine (browsing the internet from the phone is working, and I can ping the phone from desktop). I'm using WiFi, not 3G.
I think I found it. I was missing a call to:
ss.setReuseAddress(true);
after creating the server socket. This means each restart of the app (pause/resume, too) was (silently?) failing to create server socket because the socket was marked as 'in use' for a bit. This explains why it was so unreliable (only the very first run, or running after a long enough wait would work ...)
I found it by staring at the code linked to from this blog entry.

Categories

Resources