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.
Related
There is an application on Angular, the backend is an application on Java-Spring.
Both of them are running on a server in a shared network, on a Windows OC machine in VirtualBox (Linux).
The essence of the problem is that when you try to open a web application in a browser, it runs completely on one computer out of five with Windows OC and on one of one on Linux OC.
The browser is everywhere Chrome, only in Linux Mozilla
The application itself is launched, but it does not receive data from the backend at startup.
At the same time I get an error
Failed to load resource:
http://10.151.78.6:5003/es-serv/api/v1/get-data/sh1 net::ERR_CONNECTION_RESET
Here is the controller method that receives requests, there is no call to it in the logs
#GetMapping("/get-data/" + RestApiConstants.VARIABLE_NAME)
public ResponseEntity<ListResponse<DataDto>> getData(
#PathVariable(RestApiConstants.PARAM_NAME_WORD) String name) {
log.info("getData -> start");
return converterDtoService.converterDataDto(name);
}
Moreover, if you just try to open the link in the browser bar
http://10.151.78.6:5003/es-serv/api/v1/get-data/sh1
Then I get the data every time, I have never noticed any failures.
Very similar to the problem with CORS, but then the browser gives a specific error to all requests. Yes, and cors is disabled in the Java application. And even then it is not clear why it still works on some browsers.
It doesn't look like a timeout problem either, because I get an error instantly, and when the server doesn't respond, some time passes and it's noticeable.
And another such moment, I added a forced data reading button to the application. And after 20-30 attempts to read the data, the answer may still come to those computers that did not receive them.
If it was a problem with the network, then it is unclear why on the same computer from the same browser, the GET request typed in the browser line gets answers all the time, without a single pass.
Tell me where to look to understand the reason?
The problem was solved by updating the browsers to the latest version. And before that, the browser version was not very old. I don't understand how this could affect the transmission of the GET request over the network?
In working with wso2emm, I've noticed a particular behavior that we'd like to change on our end. This is that when you issue a deploy to a set of devices, the server will send one json message informing the client of a new update to their software, and if anything goes wrong in the process of downloading the update, the json message is lost.
I've saved this on the client end so that we can catch errors and retry and resume downloads. My question is, since I'm unfamiliar with a lot of web development, where would one look to modify the logic for sending out messages to mobile clients?
If your are using LOCAL notification for android, server add the message of update/install software in an API.
When android pull from server, device read that message and marked it as read into database status. Then server remove that message from API queue.
If your are using LOCAL notification for android, you can take any of these approach to find out what really happening.
Approach 1:(little bit manually)
Change android side code for sending version of device apps into server and change server side code to display app version in deivce tab UI.
Then cross check that your app have latest version or backdated version. If it have backdated version, then send update to that device/s.
Approach 2:
Do not mark as read this update message when 1st read this by device from API message queue.
Update read status after successfully update/install new version.
I have a tomcat 7.0.53 server running for the purpose of communicating between java and the website through websockets. I wrote the website script as well as the websocket server java code and they communicate and work correctly. However the information I want passed to the website is from another java program running in the background on a Linux machine. The problem lies however with that when I try to pass messages into a sendmessage type of function on the websocket server java code, it thinks that no sessions are open to send the websocket message to even if I do have the website opening waiting for a message from the separate java process to be passed through the websocket server. I found out this problem arises since I have to make a new instance of the websocket server when passes messages from my separate java process into the websocket server sendmessage function and thus there are no websocket sessions in that instance. I am stumped as to a way around this problem other then to making a socket between my websocket server and the other java process running which will be messy and want to avoid. Does anyone else know anyway else this can be done with communicating a separate java process with a websocket server for tomcat, or is this just impossible to do without making a socket?
I'm not entirely sure I understand your question. Could you share the code you are using to provide more context as to what you are attempting server side?
I believe you should be able to create a websocket client endpoint from the second java program. You can find examples of java websocket clients in the web such as the following:
http://www.hascode.com/2014/11/creating-different-websocket-chat-clients-in-java/
hope this helps
I am trying to achieve a TCP connection between a JavaScript client and a java server. (implementation must be this way I cannot swap to node for the server for example ).
Web sockets implmentation in java looked very complicated. I had a look at Jetty and JWebSocket and was quickly scared off. I have no idea what is going on in the source for them. – So I didn’t have much luck implementing a server using them.
So then I looked for websocket alternatives.
I noticed SocketBridge, It seems very straight forwards and offers exactly what I need for my project so I downloaded that.
I created a simple java server that just prints what I receives and sends a string as bytes back. I used the prebuilt JavaSocketBridge and modified the index.html to point to my server. My server recived the message but nothing showed up on the client.
function run(){
socket_connect('localhost', 31113);
socket_send("Hello from JavaSocketBridge applet");
}
I then decided to build the JavaSocketBridge to see if I could debug the read methods. However my build of JavaSocketBridge refuses to connect with the error.
Java Socket Bridge ERROR: Could not connect to localhost on port 31113
Access denied (“java.net.SocketPermission” “127.0.0.1:31113” “connect,resolve” )
(This was in chrome but it happens in firefox too)
So my questions:
Why does my build get a socket permission error?
Why does the client not receive anything even though the example does from google.com:80?
Notes.
My server appears to be working fine. (I have used a simple java client to test it.
I have used java 1.6 and 1.7 to build the JavaSocketBridge)
I have included C:\Program Files\Java\jre7\lib\plugin.jar
My System is Win7 64 and java SDK / JRE is up to date
Edit. Ive gone back to jetty and got a client/server working, however I will monitor this question.
I am developing an application which uses Bluetooth to connect to a device and send/receive data. I am doing all of my testing with a Nexus One phone.
I have never been able to establish a SPP (serial port) connection from my phone to any device. However, I have been able to connect from a device (my laptop) to my phone using a Mac equivalent of PuTTY (The only exception to this is the "Bluetooth File Transfer" app from the Marketplace seems to work, but I don't think that uses RFCOM/SPP...).
I keep seeing this message in my LogCat logs:
ERROR/BluetoothService.cpp(78): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
as well as these:
java.io.IOException: Operation Canceled
java.io.IOException: Software caused connection abort
I have tried using the UUID of "00001101-0000-1000-8000-00805F9B34FB" and I have also tried using the:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
method instead of device.createRfcommSocketToServiceRecord(UUID); as well--with no luck.
I am using the BluetoothChat example and variations of that code to do all of my testing...
Solutions or suggestions would be great...or even a better/less complex example of some testing code I can run on the phone, or a python script or something I can run on my computer to help debug?
Thanks! I hope this isn't a bug with the Android OS, but if it is I hope to find a workaround.
EDIT: I should also note that most devices show up as "paired, but not connected" in the Bluetooth settings.
EDIT 2: The solution seems to be simply disabling any Bluetooth listening. See my answer post for more information.
The solution, as it turns out, was to disable the server functionality of the Bluetooth service. By only using createRfcommSocketToServiceRecord and never calling listenUsingRfcommWithServiceRecord (in the BluetoothChat example this means never starting the "AcceptThread") the problem was fixed.
Even though these two calls are supposed to be totally separated and have no affect on each other (according to the Android docs), simply commenting out listenUsingRfcommWithServiceRecord fixed my supposedly unrelated issue.
I can take the Bluetooth Chat program unedited and it will not be able to establish an outgoing connection to ANY bluetooth device I have tested (laptops, desktops, headsets, etc.), but if I remove that one thing it works flawlessly as a client.
Anyway, I hope this will help someone else if they come across the same issue. This must be a bug with the Android OS, or possibly the firmware on the Nexus One.
I would ignore the stopDiscovery error - its good that you're cancelling discovery before making your connection. Per the SDK docs:
Because discovery is a heavyweight
precedure for the Bluetooth adapter,
this method should always be called
before attempting to connect to a
remote device with connect().
Discovery is not managed by the
Activity, but is run as a system
service, so an application should
always call cancel discovery even if
it did not directly request a
discovery, just to be sure.
So with that said, were you able to get the Bluetooth Chat example to work before you made any modifications to the code?
The UUID you want for SPP/RFCOMM is:
static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);
or defined another way (both accomplish the same thing).
static final UUID UUID_RFCOMM_GENERIC = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");