Skip to content
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
3 changes: 3 additions & 0 deletions Core/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
**/
- (void)didOpen;
- (void)didReceiveMessage:(NSString *)msg;
- (void)didReceiveData:(NSData *)data;
- (void)didClose;

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

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

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

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

@end
40 changes: 38 additions & 2 deletions Core/WebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,22 @@ - (void)didReceiveMessage:(NSString *)msg
}
}

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

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

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

- (void)didClose
{
HTTPLogTrace();
Expand Down Expand Up @@ -733,8 +749,24 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
}
else if (tag == TAG_PAYLOAD_LENGTH64)
{
// FIXME: 64bit data size in memory?
[self didClose];
UInt8 *pFrame = (UInt8 *)[data bytes];
NSUInteger length =
((NSUInteger)pFrame[7]) |
((NSUInteger)pFrame[6] << 8) |
((NSUInteger)pFrame[5] << 16) |
((NSUInteger)pFrame[4] << 24);

#ifdef __arm64__
length |= ((NSUInteger)pFrame[3] << 32) |
((NSUInteger)pFrame[2] << 40) |
((NSUInteger)pFrame[1] << 48) |
((NSUInteger)pFrame[0] << 56);
#endif

if (nextFrameMasked) {
[asyncSocket readDataToLength:4 withTimeout:TIMEOUT_NONE tag:TAG_MSG_MASKING_KEY];
}
[asyncSocket readDataToLength:length withTimeout:TIMEOUT_NONE tag:TAG_MSG_WITH_LENGTH];
}
else if (tag == TAG_MSG_WITH_LENGTH)
{
Expand All @@ -754,6 +786,10 @@ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)t
NSString *msg = [[NSString alloc] initWithBytes:[data bytes] length:msgLength encoding:NSUTF8StringEncoding];
[self didReceiveMessage:msg];
}
if (nextOpCode == WS_OP_BINARY_FRAME)
{
[self didReceiveData:data];
}
else
{
[self didClose];
Expand Down