Support Ping/Pong #2

Open
hongkongkiwi opened this Issue Dec 13, 2012 · 9 comments

2 participants

@hongkongkiwi

Please support Ping/Pong at the low level.

This will really help for the server to know if clients are still connected to the server.

It's very easy to implement, just need to allow the opCode and automatically respond to it.
Any payload passed is automatically sent back to the client as is.

Ping RFC
http://tools.ietf.org/html/rfc6455#section-5.5.2
Pong RFC
http://tools.ietf.org/html/rfc6455#section-5.5.3

As the code is really undocumented and uses lots of bit shifting it's a little bit complicated for me to modify myself. Hoping the author can help out with this?

@mxcl
Owner

Happy to implement this. I didn't bother as for my uses (communication with a browser's Javascript WebSocket) it didn't seem necessary.

@mxcl
Owner

Code would go in - (void)socket:(GCDAsyncSocket *)connection didReadData:(NSData *)data withTag:(long)tag.

I agree documentation is lacking. I didn't add much as I knew I'd refactor it four times or so. That is pretty much done at this point.

@mxcl mxcl added a commit that referenced this issue Dec 16, 2012
@mxcl Make README up-to-date
Refs #2.
ef0f869
@hongkongkiwi

Thanks. I had a small go at doing it myself, but I'm not very knowledgable in bit-shifting so it was slightly over my head.

From what I was reading, Chrome and other browsers support Ping/Pong natively but don't expose it to javascript. So for example, they will automatically respond to a ping without the JS code having to do anything special. Could be useful also for your usage.

The way that I'm planning to use it to to setup a keep alive timer and to automatically disconnect clients if they don't respond in time. This way the server always has an up to date state of each client even if they leave without disconnecting cleanly.

@mxcl
Owner

I have this code almost ready to go.

If I understand your request, you also want to actively ping individual clients? As well as have MBWebSocketServer respond to pings they send with a pong?

@mxcl
Owner

https://github.com/mxcl/MBWebSocketServer/tree/ping

I'm currently trying to test the pings, but it seems to still work with my app so I'm assuming I didn't break it.

@mxcl
Owner

Seems that pings work. Please test and confirm and then I'll merge.

@hongkongkiwi
Actively sending Ping

If I understand your request, you also want to actively ping individual clients? As well as have MBWebSocketServer respond to pings they send with a pong?

That's exactly right. For actively pinging clients, I propose the following methods:

// Sends a ping to a client - data may be nil or not nil and is simply echoed back
- (void)sendPing:(NSData *)data connection:(GCDAsyncSocket *)toConnection;

// Delegate callback - data may be nil or not nil depending on the ping that was sent out
@optional
- (void)webSocketServer:(MBWebSocketServer *)webSocketServer didReceivePong:(NSData *)data connection:(GCDAsyncSocket *)connection;
Responding to Ping with Pong

Seems that pings work. Please test and confirm and then I'll merge.

Server responds to Ping, thanks!! (I tested using SocketRocket (iOS WS Client) and added in a ping method from a fork)

@hongkongkiwi

Just an update to this, in your current fork of the code it seems to work fine except for the following issue.
Binary data is not received properly - This can be fixed by adding to the message parsing

                    case 1:  //  text frame
                    case 2:  // binary frame
                    case 8:  // close frame http://tools.ietf.org/html/rfc6455#section-5.5.1
                    case 9:  //  ping frame http://tools.ietf.org/html/rfc6455#section-5.5.2
@mxcl
Owner

I think my wip branch handles the binary frames.

I found a test suite and started fixing the class to conform. But I didn't get through all of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment