'Socket communication between Android app and web browser
My Android device+app is continuously sending data every few ms, and I'd like to receive it on my web browser application that I'm building with JavaScript/HTML.
In the Android/Java app I do the following over socket:
//Initialize, where PORT = local ip of my laptop with web server I guess.
//and I choose an available port on my network, say 8080.
echoSocket = new Socket(HOST, PORT);
out = new PrintWriter(echoSocket.getOutputStream(), true);
//Sending data every few ms:
JSONObject j = new JSONObject();
j.put("x", params[0]);
j.put("y", params[1]);
j.put("z", params[2]);
String jString = j.toString();
out.println(jString);
So I have something like {"x": 1.0023532, "y": 2.454234, "z": 6.234583}
.
In other Java applications, I've done this communication by having my receiver application create a ServerSocket
on the particular PORT
used above. Then as long as I have the right local IP address for my laptop, I can do serverSocket = new ServerSocket(PORT);
etc.
Now, how can I accept this data in a web application (JavaScript/HTML)? I've heard of websockets but have no idea how to initialize and use for this purpose - hopefully it's pretty straightforward.
Solution 1:[1]
I dont think its possible to send data directly to the browser without a middle man (server). If you want to create a fast and easy server to ping data back and forth I would have some fun with NodeJs. I havent gotten a chance to ever use the stuff but I did have some fun playing with it. It could be something to look into expecially if your just pinging data back and forth between clients.
I watched this video "Introduction to Node.js with Ryan Dahl" a while ago and he showed a basic example that does pretty much what your talking about. Just a thought, plus it would be a fun and fast implementation.
On a side note I do believe Amazon AWS has a instance for Node JS if you want to bring it to a live server. Im pretty sure you can setup a micro instance for no cost.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ug_ |