要的Andr​​oid Node.js的通信 [英] Android to node.js communication

查看:126
本文介绍了要的Andr​​oid Node.js的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到有几个类似的线程,但我找不到我的答案。

我正在和Android应用程序中,我想使用节点服务器进行实时通信。

我真的不能得到这个工作。

也许我做很多很多事情错了,但我想试着去理解。

我的服务器很简单,只要

VAR HTTP =要求(HTTP), IO =需要('socket.io), 服务器= http.createServer(功能(REQ,RES){      res.writeHead(200,{内容类型:text / html的'});      res.end(':)'); }); server.listen(8080); VAR插座= io.listen(服务器); socket.on('连接',功能(客户端){     client.send(你好);     的console.log(接!); });

和这个作品......我想这与一个web应用程序,我可以连接。

但我不能用java ..

我想 kryonet 但我得到一个例外,如连接超时,但在注册

我想 weberknecht 我得到一个错误在创建套接字WS: //184.xxxxxx:8080

我想 TooTallNate ,没有运气,它只是调用的OnClose方法。

我想 jWebSocket 但我无法得到它的工作...

所以,我在这里,寻求帮助,有没有人知道如何完成这件事?任何建议?

P.S。 对于TooTallNate我使用的是这样的:

净净=新的网络化(新的URI(WS://184.xxxxxx:8080),WebSocketDraft.DRAFT76 );

也许这个问题会在这里?

更新: 我处理了这个!良好的睡眠我有这个想法,我使用socket.io,糟糕的主意......我现在用的节点的WebSocket服务器 weberknecht 。服务器看起来是这样的:

变种WS =要求(WebSocket的服务器); VAR服务器= ws.createServer(); server.addListener(连接,功能(客户端){     的console.log(新的连接);     client.send(AAAAAA);     client.addListener(信息,函数(MSG){     }); }); server.listen(8080);

和客户端:

尝试{     URI URL =新的URI(WS://184.106.69.64:8080 /);     的WebSocket的WebSocket =新WebSocketConnection(URL);     websocket.setEventHandler(新WebSocketEventHandler(){         公共无效的OnOpen(){             的System.out.println( - 开);         }         公共无效的onMessage(WebSocketMessage消息){             的System.out.println( - 接收到的消息:+ message.getText());         }         公共无效的OnClose(){             的System.out.println( - 关闭);         }     });     websocket.connect();     websocket.send(世界你好); } 赶上(WebSocketException WSE){     wse.printStackTrace(); } 赶上(的URISyntaxException使用){     use.printStackTrace(); }

解决方案

我节点的WebSocket服务器(NWS)的作者,我是pretty的确认工作的原因为节点的WebSocket服务器和socket.io不,是由于每个的执行。 NWS将自动谈判的权利草案使用,它也有一个希望90-100%,符合76和75的草稿。

至于socket.io,我不能评论太多,但最后我一看,这是WebSocket的执行实施相当差。

我目前工作的一个项目,当时称作节点的WebSocket协议的那一刻,这将是能够使用socket.io,王菲等为他们提供一个非常可靠的,合规的WebSocket实现。这也将替换节点的WebSocket服务器的当前实施2.0.0版本。

作为一个侧面说明,如果你不想承载自己的WebSocket的服务器,你可以看看使用Pusher.com,谁是真正我的雇主。

[更新]至于的WebSockets是否是最合适的技术选择,为您的应用,是数据和交互应用需要什么类型的问题。在移动设备上,它可能是最好使用类似urbanairship或notifio如果你只是发送推送通知。

问候, Micheil史密斯

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.

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..

I tried kryonet but I get an exception like "connected but timeout on registration"

I tried weberknecht I get a "error while creating socket to ws://184.xxxxxx:8080"

I tried TooTallNate, no luck, it just call onClose method.

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?

P.S. for TooTallNate I'm using something like this:

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

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

解决方案

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.

As for socket.io, I can't comment too much, but the last I looked, it's websocket implementation was rather poorly implemented.

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.

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.

[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.

Regards, Micheil Smith

这篇关于要的Andr​​oid Node.js的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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