java Android 到 node.js 通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6499543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android to node.js communication
提问by Duiker101
I saw there are a couple of similar threads but i could not find my answer.
我看到有几个类似的主题,但我找不到我的答案。
I'm making and android app, an i want to use node as server for real time communication.
我正在制作和 android 应用程序,我想使用节点作为服务器进行实时通信。
I really cannot get this to work.
我真的无法让它发挥作用。
Probably I'm making many many things wrong but i like to try to understand.
可能我做错了很多事情,但我喜欢尝试理解。
My server is as simple as
我的服务器很简单
var http = require('http'),
io = require('socket.io'),
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(':)');
});
server.listen(8080);
var socket = io.listen(server);
socket.on('connection', function(client){
client.send("hello");
console.log("connected!");
});
and this works... I tried this with a web app and I can connect.
这有效......我用一个网络应用程序试过这个,我可以连接。
But I can't with java..
但我不能用java..
I tried kryonetbut I get an exception like "connected but timeout on registration"
我尝试了kryonet,但出现“已连接但注册超时”之类的异常
I tried weberknechtI get a "error while creating socket to ws://184.xxxxxx:8080"
我试过weberknecht我得到一个“创建套接字到 ws://184.xxxxxx:8080 时出错”
I tried TooTallNate, no luck, it just call onClose method.
我试过TooTallNate,不走运,它只是调用 onClose 方法。
I tried jWebSocketbut I couldn't get it to work...
我试过jWebSocket但我无法让它工作......
So I'm here, asking for help, does anyone knows how to get this done? any suggestion?
所以我在这里寻求帮助,有人知道如何完成吗?有什么建议吗?
P.S. for TooTallNate I'm using something like this:
PS for TooTallNate 我正在使用这样的东西:
Net net = new Net(new URI("ws://184.xxxxxx:8080"),WebSocketDraft.DRAFT76);
might the problem be here?
问题可能出在这里吗?
UPDATE: I handled this! after a good sleep I had the idea, I was using socket.io, bad idea... now I use Node Websocket Serverwith weberknecht. The server looks like this:
更新:我处理了这个!睡个好觉后我有了这个想法,我正在使用 socket.io,坏主意......现在我使用Node Websocket Server和 weberknecht。服务器看起来像这样:
var ws = require("websocket-server");
var server = ws.createServer();
server.addListener("connection", function(client){
console.log("new connection");
client.send("aaaaaa");
client.addListener("message", function(msg){
});
});
server.listen(8080);
and the client:
和客户:
try {
URI url = new URI("ws://184.106.69.64:8080/");
WebSocket websocket = new WebSocketConnection(url);
websocket.setEventHandler(new WebSocketEventHandler() {
public void onOpen(){
System.out.println("--open");
}
public void onMessage(WebSocketMessage message){
System.out.println("--received message: " + message.getText());
}
public void onClose(){
System.out.println("--close");
}
});
websocket.connect();
websocket.send("hello world");
}
catch (WebSocketException wse) {
wse.printStackTrace();
}
catch (URISyntaxException use) {
use.printStackTrace();
}
采纳答案by Duiker101
I'm the author of node-websocket-server (nws), and I'm pretty sure the reason for node-websocket-server working and socket.io not, is due to the implementation of each. NWS will auto negotiate the right draft to use, and it also has a hopefully 90-100% compliance with the drafts of 76 and 75.
我是 node-websocket-server (nws) 的作者,我很确定 node-websocket-server 工作和 socket.io 不工作的原因是由于每个的实现。NWS 将自动协商要使用的正确草案,并且它也有望 90-100% 符合 76 和 75 的草案。
As for socket.io, I can't comment too much, but the last I looked, it's websocket implementation was rather poorly implemented.
至于socket.io,我不能评论太多,但我最后看,它的websocket实现相当糟糕。
I'm currently working on a project at the moment called node-websocket-protocol, which will be able to be used by socket.io, faye, etc as to provide them with a really reliable and compliant websocket implementation. This will also replace the current implementation in node-websocket-server in version 2.0.0.
我目前正在开发一个名为 node-websocket-protocol 的项目,它可以被 socket.io、faye 等使用,为他们提供真正可靠和兼容的 websocket 实现。这也将替换版本 2.0.0 中 node-websocket-server 中的当前实现。
As a side note, if you'd rather not host your own websocket server, you could look at using Pusher.com, who're actually my employers.
附带说明一下,如果您不想托管自己的 websocket 服务器,您可以考虑使用 Pusher.com,他们实际上是我的雇主。
[Update] As for whether websockets are the most appropriate technology choice for your application, is a matter of what type of data and interaction your application needs. On a mobile device, it may be best to use something like urbanairship or notifio if you're just sending push notifications.
[更新]至于 websockets 是否是您的应用程序最合适的技术选择,这取决于您的应用程序需要什么类型的数据和交互。在移动设备上,如果您只是发送推送通知,最好使用诸如urbanairship 或notifio 之类的东西。
Regards, Micheil Smith
问候, 迈克尔·史密斯
回答by Alfred
Like Raynos said the websocket spec is still a draft(changes). Maybe you could ask Guillermo Rauch(Author socket.io) which draft he is using and find the proper Websocket library. At his github pageyou can find his email address and try to send him an email(short).
就像 Raynos 所说的 websocket 规范仍然是一个草案(变化)。也许您可以询问 Guillermo Rauch(作者 socket.io)他正在使用哪个草案并找到合适的 Websocket 库。在他的 github 页面上,您可以找到他的电子邮件地址并尝试向他发送电子邮件(简短)。
You could also try to use another transport(long-polling) and implement the specyourself I guess.
你也可以尝试使用另一种传输(长轮询)并自己实现规范,我猜。
But in my opinion you should be using Google's c2dm(Big players are already using this and will be very gently to your phone's battery) instead to push updates to phone in realtime. I already have a lot of applications installed that are using c2dm like for example whatsapp(awesome). You could be using notifo's apito simplify your work. Then simply install there android programto receive the desired notifications. There is also a node module(npm)available for you to use (easy :))
但在我看来,您应该使用 Google 的c2dm(大玩家已经在使用它,并且会非常温和地使用您的手机电池),而不是将更新实时推送到手机。我已经安装了很多使用 c2dm 的应用程序,例如whatsapp(很棒)。您可以使用notifo 的 api来简化您的工作。然后只需在那里安装android 程序即可接收所需的通知。还有一个节点模块(npm)可供您使用(简单:))
回答by xpepermint
I recommend
我建议
- Android: https://github.com/stellaeof/java-websocket-client
- Node Websockets:http://static.brandedcode.com/nws-docs/
- 安卓:https: //github.com/stellaeof/java-websocket-client
- 节点 Websockets:http://static.brandedcode.com/nws-docs/