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

added support for binary sockets #153

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Core/HTTPConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ - (void)startConnection
forKey:(NSString *)kCFStreamSSLCertificates];

// Configure this connection to use the highest possible SSL level
[settings setObject:(NSString *)kCFStreamSocketSecurityLevelNegotiatedSSL
forKey:(NSString *)kCFStreamSSLLevel];
// [settings setObject:(NSString *)kCFStreamSocketSecurityLevelNegotiatedSSL
// forKey:(NSString *)kCFStreamSSLLevel];

[asyncSocket startTLS:settings];
}
Expand Down
6 changes: 5 additions & 1 deletion Core/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
* Sends a message over the WebSocket.
* This method is thread-safe.
**/
- (void)sendData:(NSData *)msg;
- (void)sendBinaryData:(NSData *)msg;


/**
* Subclass API
Expand All @@ -73,6 +74,7 @@
**/
- (void)didOpen;
- (void)didReceiveMessage:(NSString *)msg;
- (void)didReceiveData:(NSData *)data;
- (void)didClose;

@end
Expand Down Expand Up @@ -100,6 +102,8 @@

- (void)webSocket:(WebSocket *)ws didReceiveMessage:(NSString *)msg;

- (void)webSocket:(WebSocket *)ws didReceiveData:(NSData *)data;

- (void)webSocketDidClose:(WebSocket *)ws;

@end
102 changes: 76 additions & 26 deletions Core/WebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -536,41 +536,74 @@ - (void)didOpen
- (void)sendMessage:(NSString *)msg
{
NSData *msgData = [msg dataUsingEncoding:NSUTF8StringEncoding];
[self sendData:msgData];
[self sendData:msgData asString:YES];
}

- (void)sendData:(NSData *)msgData
- (void)sendBinaryData:(NSData *)msg {
[self sendData:msg asString:NO];
}

- (void)sendData:(NSData *)msgData asString:(BOOL)asString
{
HTTPLogTrace();

NSMutableData *data = nil;

if (isRFC6455)
{
NSUInteger length = msgData.length;
if (length <= 125)
{
data = [NSMutableData dataWithCapacity:(length + 2)];
[data appendBytes: "\x81" length:1];
UInt8 len = (UInt8)length;
[data appendBytes: &len length:1];
[data appendData:msgData];
}
else if (length <= 0xFFFF)
{
data = [NSMutableData dataWithCapacity:(length + 4)];
[data appendBytes: "\x81\x7E" length:2];
UInt16 len = (UInt16)length;
[data appendBytes: (UInt8[]){len >> 8, len & 0xFF} length:2];
[data appendData:msgData];
}
else
{
data = [NSMutableData dataWithCapacity:(length + 10)];
[data appendBytes: "\x81\x7F" length:2];
[data appendBytes: (UInt8[]){0, 0, 0, 0, (UInt8)(length >> 24), (UInt8)(length >> 16), (UInt8)(length >> 8), length & 0xFF} length:8];
[data appendData:msgData];
}

NSUInteger length = msgData.length;

if (asString) {
if (length <= 125)
{
data = [NSMutableData dataWithCapacity:(length + 2)];
[data appendBytes: "\x81" length:1];
UInt8 len = (UInt8)length;
[data appendBytes: &len length:1];
[data appendData:msgData];
}
else if (length <= 0xFFFF)
{
data = [NSMutableData dataWithCapacity:(length + 4)];
[data appendBytes: "\x81\x7E" length:2];
UInt16 len = (UInt16)length;
[data appendBytes: (UInt8[]){len >> 8, len & 0xFF} length:2];
[data appendData:msgData];
}
else
{
data = [NSMutableData dataWithCapacity:(length + 10)];
[data appendBytes: "\x81\x7F" length:2];
[data appendBytes: (UInt8[]){0, 0, 0, 0, (UInt8)(length >> 24), (UInt8)(length >> 16), (UInt8)(length >> 8), length & 0xFF} length:8];
[data appendData:msgData];
}
}
else {
Byte prefix = 0x80 | WS_OP_BINARY_FRAME;

if (length <= 125) {
data = [NSMutableData dataWithCapacity:(length + 2)];
[data appendBytes:&prefix length:1];
UInt8 len = (UInt8)length;
[data appendBytes:&len length:1];
[data appendData:msgData];
} else if (length <= 0xFFFF) {
Byte extendedPrefix[2] = {prefix, 0x7E};
data = [NSMutableData dataWithCapacity:(length + 4)];
[data appendBytes:extendedPrefix length:2];
UInt16 len = (UInt16)length;
[data appendBytes:(UInt8[]) { len >> 8, len & 0xFF } length:2];
[data appendData:msgData];
} else {
Byte extendedPrefix[2] = {prefix, 0x7F};
data = [NSMutableData dataWithCapacity:(length + 10)];
[data appendBytes:extendedPrefix length:2];
[data appendBytes:(UInt8[]) { 0, 0, 0, 0, (UInt8)(length >> 24), (UInt8)(length >> 16), (UInt8)(length >> 8), length & 0xFF } length:8];
[data appendData:msgData];
}
}

}
else
{
Expand Down Expand Up @@ -602,6 +635,20 @@ - (void)didReceiveMessage:(NSString *)msg
}
}

- (void)didReceiveData:(NSData *)data {
// HTTPLogTrace();

// Override me to process incoming messages.
// This method is invoked on the websocketQueue.
//
// For completeness, you should invoke [super didReceiveMessage:msg] in your method.

// Notify delegate
if ([delegate respondsToSelector:@selector(webSocket:didReceiveData:)]) {
[delegate webSocket:self didReceiveData:data];
}
}

- (void)didClose
{
HTTPLogTrace();
Expand Down Expand Up @@ -754,6 +801,9 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
NSString *msg = [[NSString alloc] initWithBytes:[data bytes] length:msgLength encoding:NSUTF8StringEncoding];
[self didReceiveMessage:msg];
}
else if (nextOpCode == WS_OP_BINARY_FRAME) {
[self didReceiveData:data];
}
else
{
[self didClose];
Expand Down