Browse Source

Add optional delegate method for received ping. (#463)

Nikita Lutsenko 9 years ago
parent
commit
04b28a7c56

+ 1 - 0
SocketRocket/Internal/Delegate/SRDelegateController.h

@@ -20,6 +20,7 @@ struct SRDelegateAvailableMethods {
     BOOL didOpen : 1;
     BOOL didFailWithError : 1;
     BOOL didCloseWithCode : 1;
+    BOOL didReceivePing : 1;
     BOOL didReceivePong : 1;
     BOOL shouldConvertTextFrameToString : 1;
 };

+ 1 - 0
SocketRocket/Internal/Delegate/SRDelegateController.m

@@ -56,6 +56,7 @@ NS_ASSUME_NONNULL_BEGIN
             .didOpen = [delegate respondsToSelector:@selector(webSocketDidOpen:)],
             .didFailWithError = [delegate respondsToSelector:@selector(webSocket:didFailWithError:)],
             .didCloseWithCode = [delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)],
+            .didReceivePing = [delegate respondsToSelector:@selector(webSocket:didReceivePingWithData:)],
             .didReceivePong = [delegate respondsToSelector:@selector(webSocket:didReceivePong:)],
             .shouldConvertTextFrameToString = [delegate respondsToSelector:@selector(webSocketShouldConvertTextFrameToString:)]
         };

+ 8 - 0
SocketRocket/SRWebSocket.h

@@ -382,6 +382,14 @@ extern NSString *const SRHTTPResponseErrorKey;
  */
 - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(nullable NSString *)reason wasClean:(BOOL)wasClean;
 
+/**
+ Called on receive of a ping message from the server.
+
+ @param webSocket An instance of `SRWebSocket` that received a ping frame.
+ @param data      Payload that was received or `nil` if there was no payload.
+ */
+- (void)webSocket:(SRWebSocket *)webSocket didReceivePingWithData:(nullable NSData *)data;
+
 /**
  Called when a pong data was received in response to ping.
 

+ 7 - 4
SocketRocket/SRWebSocket.m

@@ -658,12 +658,15 @@ NSString *const SRHTTPResponseErrorKey = @"HTTPResponseStatusCode";
     return YES;
 }
 
-- (void)handlePing:(NSData *)pingData;
+- (void)_handlePingWithData:(nullable NSData *)data
 {
     // Need to pingpong this off _callbackQueue first to make sure messages happen in order
-    [self.delegateController performDelegateQueueBlock:^{
+    [self.delegateController performDelegateBlock:^(id<SRWebSocketDelegate> _Nullable delegate, SRDelegateAvailableMethods availableMethods) {
+        if (availableMethods.didReceivePing) {
+            [delegate webSocket:self didReceivePingWithData:data];
+        }
         dispatch_async(_workQueue, ^{
-            [self _sendFrameWithOpcode:SROpCodePong data:pingData];
+            [self _sendFrameWithOpcode:SROpCodePong data:data];
         });
     }];
 }
@@ -824,7 +827,7 @@ static inline BOOL closeCodeIsValid(int closeCode) {
             [self handleCloseWithData:frameData];
             break;
         case SROpCodePing:
-            [self handlePing:frameData];
+            [self _handlePingWithData:frameData];
             break;
         case SROpCodePong:
             [self handlePong:frameData];