Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...