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

我正在制作android应用,我想使用node作为服务器进行实时通信。

我真的无法使它正常工作。

可能我把很多事情弄错了,但我想尝试理解。

我的服务器很简单

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

});

并且可以正常工作…我通过网络应用程序尝试了此操作,然后就可以连接了。

但是我不能用java ..

我尝试了kryonet,但出现了类似“已连接但注册超时”的异常

我尝试使用weberknecht时遇到“在创建ws://184.xxxxxx:8080的套接字时出错”

我尝试了TooTallNate,没有运气,它只是调用onClose方法。

我尝试了jWebSocket,但无法正常工作…

所以我在这里寻求帮助,有人知道如何完成这项工作吗?有什么建议吗?

PS for 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();

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐