Android应用程序和网络浏览器之间的socket通信 [英] Socket communication between Android app and web browser

查看:591
本文介绍了Android应用程序和网络浏览器之间的socket通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid设备+应用程序是连续发送的数据每隔几毫秒,我想收到它是我用JavaScript / HTML构建我的Web浏览器应用程序。

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.

在Android /的Java应用程序我下面通过套接字:

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);

所以我有类似 {X:1.0023532,Y:2.454234,Z:6.234583}

在其他Java应用程序中,我通过我的有接收器的应用程序创建一个的ServerSocket 特定的 PORT 上面使用。然后,只要我有我的笔记本电脑正确的本地IP地址,我可以做的ServerSocket =新的ServerSocket(PORT);

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.

现在,我怎么能接受一个Web应用程序(的JavaScript / HTML)这个数据?我听说过的WebSockets的,但不知道如何初始化和使用用于此目的 - 希望这是pretty简单。

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.

感谢。

推荐答案

我不认为它可以直接将数据发送到浏览器,而中间人(服务器)。如果你想创建一个快速和容易服务器的ping数据来回我会有些乐趣的NodeJS。我还没有得到一个机会,用不完的东西,但我确实有一些有趣的玩它。这可能是一些寻找到expecially如果你只是执行ping数据来回客户端之间。

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.

我看了这部影片介绍与瑞安达尔以Node.js加载一会儿以前,他发现做pretty多少你谈论一个基本的例子。只是一个想法,再加上这将是一个有趣和快速实施。

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.

在一个侧面说明我相信亚马逊AWS有节点的JS例如,如果你想将其带到现场服务器。林pretty相信你可以设置一个微型实例没有成本。

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.

这篇关于Android应用程序和网络浏览器之间的socket通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆