Android 到 node.js 通信 [英] Android to node.js communication
问题描述
我看到有几个类似的主题,但我找不到我的答案.
I saw there are a couple of similar threads but i could not find my answer.
我正在制作和 android 应用程序,我想使用节点作为服务器进行实时通信.
I'm making and android app, an i want to use node as server for real time communication.
我真的无法让它发挥作用.
I really cannot get this to work.
可能我做错了很多事情,但我想试着去理解.
Probably I'm making many many things wrong but i like to try to understand.
我的服务器很简单
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.
但是我不能用 java..
But I can't with java..
我尝试了 kryonet,但出现已连接但注册超时"之类的异常
I tried kryonet but I get an exception like "connected but timeout on registration"
我尝试了 weberknecht 我收到一个在创建到 ws://的套接字时出错"184.xxxxxx:8080"
I tried weberknecht I get a "error while creating socket to ws://184.xxxxxx:8080"
我尝试了 TooTallNate,没有运气,它只是调用了 onClose 方法.
I tried TooTallNate, no luck, it just call onClose method.
我尝试了 jWebSocket 但我无法让它工作......
I tried jWebSocket but I couldn't get it to work...
所以我在这里寻求帮助,有人知道如何完成吗?有什么建议吗?
So I'm here, asking for help, does anyone knows how to get this done? any suggestion?
附言对于 TooTallNate 我正在使用这样的东西:
P.S. for TooTallNate I'm using something like this:
Net net = new Net(new URI("ws://184.xxxxxx:8080"),WebSocketDraft.DRAFT76);
问题可能出在这里吗?
更新:我处理了这个!睡个好觉后,我有了这个主意,我正在使用 socket.io,坏主意……现在我使用 Node Websocket 服务器,带有 weberknecht.服务器看起来像这样:
UPDATE: I handled this! after a good sleep I had the idea, I was using socket.io, bad idea... now I use Node Websocket Server with weberknecht. The server looks like this:
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);
和客户:
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();
}
推荐答案
我是 node-websocket-server (nws) 的作者,我很确定 node-websocket-server 工作和套接字的原因.io不是,是由于每个的执行.NWS 将自动协商使用正确的草案,并且它也有希望 90-100% 符合 76 和 75 的草案.
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.
对于socket.io,我不能过多评论,但我最后看,它的websocket实现相当糟糕.
As for socket.io, I can't comment too much, but the last I looked, it's websocket implementation was rather poorly implemented.
我目前正在开发一个名为 node-websocket-protocol 的项目,它可以被 socket.io、faye 等使用,为他们提供真正可靠且合规的 websocket 实现.这也将替换 2.0.0 版本中 node-websocket-server 中的当前实现.
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.
附带说明,如果您不想托管自己的 websocket 服务器,可以考虑使用 Pusher.com,他们实际上是我的雇主.
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.
[更新]至于 websockets 是否是最适合您的应用程序的技术选择,这取决于您的应用程序需要什么类型的数据和交互.在移动设备上,如果您只是发送推送通知,最好使用诸如urbanairship 或notifio 之类的东西.
[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.
问候,迈克尔·史密斯
这篇关于Android 到 node.js 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!