Browse Source

Add 'sendData:', 'sendString:' and deprecate 'send:'. (#368)

Nikita Lutsenko 9 năm trước cách đây
mục cha
commit
e8efe0bfb2

+ 33 - 3
SocketRocket/SRWebSocket.h

@@ -109,15 +109,45 @@ extern NSString *const SRHTTPResponseErrorKey;
 - (void)close;
 - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
 
-// Send a UTF8 String or Data.
-- (void)send:(id)data;
+///--------------------------------------
+#pragma mark Send
+///--------------------------------------
 
-// Send Data (can be nil) in a ping message.
+/**
+ Send a UTF-8 string or binary data to the server.
+
+ @param message UTF-8 String or Data to send.
+
+ @deprecated Please use `sendString:` or `sendData` instead.
+ */
+- (void)send:(id)message __attribute__((deprecated("Please use `sendString:` or `sendData` instead.")));
+
+/**
+ Send a UTF-8 String to the server.
+
+ @param string String to send.
+ */
+- (void)sendString:(NSString *)string;
+
+/**
+ Send binary data to the server.
+
+ @param data Data to send.
+ */
+- (void)sendData:(NSData *)data;
+
+/**
+ Send Ping message to the server with optional data.
+
+ @param data Instance of `NSData` or `nil`.
+ */
 - (void)sendPing:(NSData *)data;
 
 @end
 
+///--------------------------------------
 #pragma mark - SRWebSocketDelegate
+///--------------------------------------
 
 @protocol SRWebSocketDelegate <NSObject>
 

+ 24 - 8
SocketRocket/SRWebSocket.m

@@ -650,19 +650,36 @@ static __strong NSData *CRLFCRLF;
 }
 
 - (void)send:(id)data;
+{
+    if (!data) {
+        [self sendData:nil]; // Send Data, but it doesn't matter since we are going to send the same text frame with 0 length.
+    } else if ([data isKindOfClass:[NSString class]]) {
+        [self sendString:data];
+    } else if ([data isKindOfClass:[NSData class]]) {
+        [self sendData:data];
+    } else {
+        NSAssert(NO, @"Unrecognized message. Not able to send anything other than a String or NSData.");
+    }
+}
+
+- (void)sendString:(NSString *)string
+{
+    NSAssert(self.readyState != SR_CONNECTING, @"Invalid State: Cannot call send: until connection is open");
+    string = [string copy];
+    dispatch_async(_workQueue, ^{
+        [self _sendFrameWithOpcode:SROpCodeTextFrame data:[string dataUsingEncoding:NSUTF8StringEncoding]];
+    });
+}
+
+- (void)sendData:(NSData *)data
 {
     NSAssert(self.readyState != SR_CONNECTING, @"Invalid State: Cannot call send: until connection is open");
-    // TODO: maybe not copy this for performance
     data = [data copy];
     dispatch_async(_workQueue, ^{
-        if ([data isKindOfClass:[NSString class]]) {
-            [self _sendFrameWithOpcode:SROpCodeTextFrame data:[(NSString *)data dataUsingEncoding:NSUTF8StringEncoding]];
-        } else if ([data isKindOfClass:[NSData class]]) {
+        if (data) {
             [self _sendFrameWithOpcode:SROpCodeBinaryFrame data:data];
-        } else if (data == nil) {
-            [self _sendFrameWithOpcode:SROpCodeTextFrame data:data];
         } else {
-            assert(NO);
+            [self _sendFrameWithOpcode:SROpCodeTextFrame data:nil];
         }
     });
 }
@@ -670,7 +687,6 @@ static __strong NSData *CRLFCRLF;
 - (void)sendPing:(NSData *)data;
 {
     NSAssert(self.readyState == SR_OPEN, @"Invalid State: Cannot call send: until connection is open");
-    // TODO: maybe not copy this for performance
     data = [data copy] ?: [NSData data]; // It's okay for a ping to be empty
     dispatch_async(_workQueue, ^{
         [self _sendFrameWithOpcode:SROpCodePing data:data];

+ 4 - 0
Tests/Operations/SRAutobahnOperation.m

@@ -58,7 +58,11 @@ SRAutobahnOperation *SRAutobahnTestOperation(NSURL *serverURL, NSInteger caseNum
                                                caseNumber:@(caseNumber)
                                                     agent:agent
                                            messageHandler:^(SRWebSocket * _Nonnull socket, id  _Nullable message) {
+                                               //TODO: (nlutsenko) Use proper callbacks, instead of a unifying one.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
                                                [socket send:message];
+#pragma clang diagnostic pop
                                            }];
 }