I am using Android Autobahn client for communication with a web socket server. I need to know how the client detects tcp connection loss. Does it send TCP heart probes to detect this?
PS: error code : de.tavendo.autobahn.WebSocket.ConnectionHandler.CLOSE_CONNECTION_LOST
According to the source code, the CLOSE_CONNECTION_LOST error code is emitted when the endpoint calls close with an abnormal close code:
final int tavendoCloseCode = (close.mCode == 1000) ? ConnectionHandler.CLOSE_NORMAL : ConnectionHandler.CLOSE_CONNECTION_LOST;
I don't know exactly how your router is implemented, but in the source code, you have also a Ping / Pong implementation.
So you can expect that the connection loss is detected by sending some Ping / Pong WebSocket.
Related
I am a newbie to RXKotlin/RXJava.
I am developing the background service in Android.
In my service, I have
Bluetooth socket
TCP Socket
Whenever the data is available on the Bluetooth socket, read and write to the TCP socket. And whenever data is received in the TCP socket, write to the Bluetooth socket.
Can someone help me:
how to achieve this using Observables?
how to exchange the socket id information?
how to exchange the data?
Thanks
Please try using RxSubjects (https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f)
Let me take PublishSubject as an example here.
//a publish subject which publishes int values
public PublishSubject<Integer> source = PublishSubject.create();
source.onNext(1);
source.onNext(2);
So above lines of code goes in Bluetooth socket class.
Now in TCP socket class, using the source, you can observe here.
source
.subscribe(
{
//result
},
{
//error
}
)
Thats it.
Please make sure, the subscription happens before Bluetooth socket starts publishing data.
I'm using autobahn android web socket library to build my web socket client application.
In order to keep the web socket connection alive for a long time, I need to periodically send ping messages from the client.
How can I do that with auotbahn android web socket library?
AutobahnAndroid's WebSocketWriter supports client pong messages, and AutobahnPython claims the following:
explicit processing of Pings/Pongs is unnecessary normally - AutobahnPython will do the right thing under the hood.
However, during execution of a simple chat client-server setup, I observe no such ping/pong heartbeat messages.
From what I have read, it is unclear from both the documentation and the source, what the full requirements are for enabling implicit Autobahn* heartbeats.
My personal solution involves issuing a ping from the server during onConnection and the appropriate onMesssage, and something similar to the following client code onMessage:
if (payload.equals("ping from the server"))) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
myWebSocket.sendTextMessage("pong from the client");
}
}, 1000L);
}
... which schedules a pong response to be sent by the client, 1 second after receiving a ping from the server.
You can use the pull request that I just submitted
https://github.com/tavendo/AutobahnAndroid/pull/67
which adds sendPingMessage(byte[] payload) and onPongMessage(byte[] payl).
I've read the discussion, and now I am working on my own simple Android firewall. Here is the algorithm they used.
The discussion
I was able to forward packets. If I remember correctly I just needed to read the TCP header and open another socket to that destination and send it headerless.
So now I need to write data to my Wi-Fi or 3G network interface if I understand them correctly. How to do that?
I solved it. I only needed to protect the socket which I'd like to use to send data directly to the server without using the TUN device.
Socket socket = socket = SocketChannel.open().socket();
if (!protect(socket)) {
Log.e(Constants.TAG, "Failed to protect the socket");
}
The documentation
I was Posting Data from Android Mobile to Server over HTTP connection. I was able to POST data to server and also get acknowledgement from Server. But sometime if my server communication thread waiting for acknowledgement from server after posting data that time i loss Internet Connectivity and thread unable to receive Ack from server and goes in infinite loop. I tried setReadTimeOut(180000) method.
Can it work for me? Can it gives SocketTimeOutException if no network Available/ No connection between server and mobile
Setting a timeout will most likely work. However, I would not recommend setting it to 180000 (= 180 seconds = 3 minutes!). 30 seconds is usually more than enough. You also might want to add a setConnectTimeout
Note that there are some situations where an HttpUrlConnection doesn't always work properly:
Android versions 2.2 or earlier, better use the Apache HTTP client for these versions (see also this post)
On the emulator (see also this post)
Im trying to determine if data was successfully sent to the server through a TCP socket using the OutputStream object. for testing purposes i disable network communications in the phone and OutputStream.write(); doesn't throw an exception, all the methods in the socket class return as though the socket is active and working. Is there anything i'm doing wrong here?
is there any socket implementation or stream implementation i can use to get an exception or error when the stream / socket doesn't actually send the data in the buffer?
also setting SetSoTimeout() on the socket doesn't seem to do anything.
Thanks,
Totem
If you're using the emulator this is a known bug, data can still be sent after enabling airplane mode.