安卓到节点.js通信
我看到有几个类似的线程,但我找不到我的答案。
我正在制作和Android应用程序,我想使用节点作为服务器进行实时通信。
我真的无法让它工作。
也许我做了很多很多事情,但我喜欢尝试理解。
我的服务器就像
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!");
});
这有效...我尝试使用Web应用程序进行此操作,并且可以连接。
但是我不能用java..
我尝试了 kryonet,但我收到一个异常,如“已连接但在注册时超时”
我尝试了weberknecht,我得到一个“创建套接字时 ws://184.xxxxxx:8080 错误”
我尝试了TooTallNate,没有运气,它只是调用onClose方法。
我尝试了jWebSocket,但我无法让它工作...
所以我在这里寻求帮助,有人知道如何完成这项工作吗?任何建议?
P.S. 对于TooTallNate,我正在使用这样的东西:
Net net = new Net(new URI("ws://184.xxxxxx:8080"),WebSocketDraft.DRAFT76);
问题可能出在这里吗?
更新:我处理了这个!睡个好觉后,我有了这个想法,我用 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);
和客户端:
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();
}