Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onClose called immediately when trying to connect (handshake silently fails?) #13

Closed
philiphacks opened this issue May 16, 2011 · 15 comments

Comments

@philiphacks
Copy link

I've got an issue when trying to connect to a socket.io server with the Java-WebSocket client code. My server is socket.io on node.js (very simple example from socket.io website). When I connect, I get an onClose event immediately. I've written some output in the connection and handshaking process and I think the handshake is failing.

Here is the call stack:

in Client constructor (self written class extending WebSocketClient):
Creating client.

All the following calls happen in the WebSocketClient class:

    in run():
        Trying to connect to localhost:3000
    in tryToConnect():
        Opening client channel
        configure blocking = FALSE!
        Connecting to localhost/127.0.0.1:3000
        Opened selector
        Registered client with CONNECT key.

    in run() while loop:
        Selector select
        While loop of this.running
        Finishing connect
            in sendHandshake()
            We are connected... sending handshake for 'READ' operation.
            Requested path: 
            Request: GET / HTTP/1.1
                Upgrade: WebSocket
                Connection: Upgrade
                Host: localhost:3000
                Origin: null
                Sec-WebSocket-Key1: 23j06  7963l6  8
                Sec-WebSocket-Key2: >368  O( 2  ^6   1%, 5 3+2H'q A1

            key3: [B@5f1121f6
            Written 171 bytes to the bytebuffer

    back to run():
        Selector select
        While loop of this.running
        key.isReadable()!
        Handling a read operation

    in conn.handleRead() (in WebSocket class!)
        Rewinding buffer.
        Reading channel!
        // bytesRead == -1 :-(
        No bytes read.

    in my onClose() method in the client:
        onClose :: Lost connection to ws://localhost:3000
    in conn.handleRead()
        Closed connection because of no bytes read.

Handled read.

I have no idea if this call stack is useful and/or this issue is in the right place. But I've been trying several things and have no idea what to do exactly. Is it a bug in the handshake/specification? Or something I'm doing wrong? I merely implemented the onClose, onOpen, onMessage methods and just called the connect() method.

@nightkr
Copy link

nightkr commented May 17, 2011

I'm assuming that you based this on my (somewhat partial) socket.io client implementation.
Remember that you have to override send like I did because socket.io wants to know the length of the message and that socket.io uses heartbeats that you need to return. Just a few pointers.

@philiphacks
Copy link
Author

Thanks for the suggestion. However, I did override the send method. I don't know what the problem is because I just tested it with another node package (Node WebSocket Server: https://github.com/miksago/node-websocket-server ) and that one is working. That means it is a socket.io problem, but I haven't found anything about this issue in the socket.io documentation. :(

@nightkr
Copy link

nightkr commented May 17, 2011

I suppose this should be filed as a bug with Socket.IO then. Or how about testing with the trunk?

@nightkr
Copy link

nightkr commented May 17, 2011

I also suggest logging all messages received and sent to see if it happens because of something that it expects a reply to.

@philiphacks
Copy link
Author

What do you mean? There are no messages received client-side (you mean when/if the onMessage() event is called?). Server side is not a big success either. I don't see any output from socket.io. In addition to this, the socket.io is pretty outdated, because I get errors when trying to execute the examples from their README.md file. I can't configure socket.io to print more debug output..

Following the instructions for cloning the git repository (from https://github.com/learnboost/socket.io-node/tree/06) is not working either. I ran the server from the example folder but get the same output. I guess I should report this to socket.io then...

@nightkr
Copy link

nightkr commented May 17, 2011

Probably. Do keep in mind though that my client was written with Trial-and-Error for Tornadio.

@TooTallNate
Copy link
Owner

Oh wow, I wish GitHub would notify me of updates on Issues after the initial opening of the Issue. In any case, did you get this figured out? I don't think it's a Java-WebSocket issue, since I've had reports of people using this lib with Socket.IO in the past.

@xpepermint
Copy link

I have exactly the same problem!

@TooTallNate
Copy link
Owner

Closing since this isn't an issue with Java-WekSocket itself, but rather your (lack of a) Socket.IO compatibility layer...

@xpepermint
Copy link

True. Use http://static.brandedcode.com/nws-docs/.

@nightkr
Copy link

nightkr commented Jun 15, 2011

@xpepermint, that just seems to be the server API, not the wire protocol.

@xpepermint
Copy link

Well... I experienced the same behaviour (onClose called immediately when trying to connect) when I was using Node.js+Socket.io. I now use websocket server (http://static.brandedcode.com/nws-docs/) with Node.JS (http://nodejs.org).

@nightkr
Copy link

nightkr commented Jun 15, 2011

Oh, right, way for me to not read the title. Sorry.

@xpepermint
Copy link

np :)

@philiphacks
Copy link
Author

Sorry for the late reply, but no, I haven't been able to solve the issue with Socket.IO. Furthermore, I've been using the Node Websocket Server too. It actually suffices, because I'm writing a native Android app that is only able to connect through websockets, so I guess Socket.IO wouldn't give me any advantage over NWS. Anyway, thanks for the help and advice everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants