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 WebSocket Support #27

Open
wants to merge 16 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: 3 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ xcodeproj 'StompKit.xcodeproj'

platform :ios, '5.0'


pod 'SocketRocket', '0.3.1-beta2'
pod 'CocoaAsyncSocket', '7.3.2'

target 'StompKitTests', :exclusive => true do
pod 'Kiwi', '2.2'
end
end
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ STOMPClient *client = [[STOMPClient alloc] initWithHost:@"localhost"
[client connectWithLogin:@"mylogin"
passcode:@"mypassword"
completionHandler:^(STOMPFrame *_, NSError *error) {
if (err) {
if (error) {
NSLog(@"%@", error);
return;
}
Expand All @@ -72,7 +72,7 @@ STOMPClient *client = [[STOMPClient alloc] initWithHost:@"localhost"
[client connectWithLogin:@"mylogin"
passcode:@"mypassword"
completionHandler:^(STOMPFrame *_, NSError *error) {
if (err) {
if (error) {
NSLog(@"%@", error);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion StompKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Pod::Spec.new do |s|
s.source = { :git => 'https://github.com/mobile-web-messaging/StompKit.git', :tag => "#{s.version}" }
s.platform = :ios, 5.0
s.source_files = 'StompKit/*.{h,m}'
s.public_header_files = 'StompKit/StompKit.h'
s.public_header_files = 'StompKit/StompKit.h', 'StompKit/SKSocket/SKSocket.h'
s.requires_arc = true
s.dependency 'CocoaAsyncSocket', '7.3.4'
s.dependency 'SocketRocket', '0.3.1-beta2'
end
254 changes: 239 additions & 15 deletions StompKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion StompKit.xcodeproj/xcshareddata/xcschemes/StompKit.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0500"
LastUpgradeVersion = "0620"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -49,6 +49,15 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "932BA57718056C4B00A03257"
BuildableName = "libStompKit.a"
BlueprintName = "StompKit"
ReferencedContainer = "container:StompKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
Expand Down
18 changes: 18 additions & 0 deletions StompKit/SKSocket/SKRawSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// SKRawSocket.h
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#ifndef StompKit_SKRawSocket_h
#define StompKit_SKRawSocket_h

#import "SKSocket.h"

@interface SKRawSocket : NSObject <SKSocket> {
}
@end

#endif
85 changes: 85 additions & 0 deletions StompKit/SKSocket/SKRawSocket.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// SKRawSocket.m
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SKRawSocket.h"
#import "GCDAsyncSocket.h"

@interface SKRawSocket() <GCDAsyncSocketDelegate>
@property (nonatomic, weak) id <SKSocketDelegate> delegate;
@property (nonatomic, retain) GCDAsyncSocket *socket;
@end

@implementation SKRawSocket

// synthesize properties
@synthesize delegate;
@synthesize socket;

- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq {
if((self = [super init])) {
if (aDelegate != nil) {
self.delegate = aDelegate;

// initialize our socket
self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dq];
}
}

return self;
}

- (BOOL)connectToHost:(NSString*)host onPort:(uint16_t)port error:(NSError **)errPtr {
return [socket connectToHost:host onPort:port error:errPtr];
}

- (BOOL)isDisconnected {
return [socket isDisconnected];
}

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout {
[socket writeData:data withTimeout:timeout tag:123];
}

- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout {
[socket readDataToData:data withTimeout:timeout tag:123];

}

- (void)disconnectAfterReadingAndWriting {
[socket disconnectAfterReadingAndWriting];
}

#pragma mark -
#pragma mark GCDAsyncSocketDelegate

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
if (self.delegate != nil) {
[delegate socket:(SKSocket*)self didReadDataWithData:data];
}
}

- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
if (self.delegate != nil) {
[delegate socket:(SKSocket*)self didReadPartialDataOfLength:partialLength];
}
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
if (self.delegate != nil) {
[delegate socket:(SKSocket*)self didConnectToHost:host port:port];
}
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
if (self.delegate != nil) {
[delegate socketDidDisconnect:(SKSocket*)self withError:err];
}
}

@end
40 changes: 40 additions & 0 deletions StompKit/SKSocket/SKSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// SKSocket.h
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#ifndef StompKit_SKSocket_h
#define StompKit_SKSocket_h

#import "SKSocketUtility.h"

// forward declare
@class SKSocket;

// SKSocket delegate interface
@protocol SKSocketDelegate <NSObject>
@optional
- (void)socket:(SKSocket *)sock didReadDataWithData:(NSData *)data;
- (void)socket:(SKSocket *)sock didReadDataWithString:(NSString *)data;
- (void)socket:(SKSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength;
- (void)socket:(SKSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
- (void)socketDidDisconnect:(SKSocket *)sock withError:(NSError *)err;
@end



// SKSocket abstract interface
@protocol SKSocket <NSObject>
- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq;
- (BOOL)connectToHost:(NSString*)host onPort:(uint16_t)port error:(NSError **)errPtr;
- (BOOL)isDisconnected;
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout;
- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout;
- (void)disconnectAfterReadingAndWriting;

@end

#endif
14 changes: 14 additions & 0 deletions StompKit/SKSocket/SKSocketUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SKSocketUtility.h
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SKSocketUtility : NSObject
+ (NSData*)zeroData;
+ (NSData*)lineFeedData;
@end
21 changes: 21 additions & 0 deletions StompKit/SKSocket/SKSocketUtility.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SKSocketUtility.m
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#import "SKSocketUtility.h"

@implementation SKSocketUtility

+ (NSData*)zeroData {
return [NSData dataWithBytes:"" length:1];
}

+ (NSData*)lineFeedData {
return [NSData dataWithBytes:"\x0A" length:1];
}

@end
16 changes: 16 additions & 0 deletions StompKit/SKSocket/SKWebSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// SKWebSocket.h
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SKSocket.h"

extern NSString *const SKWebSocketErrorDomain;

@interface SKWebSocket : NSObject <SKSocket> {
}
@end
94 changes: 94 additions & 0 deletions StompKit/SKSocket/SKWebSocket.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// SKWebSocket.m
// StompKit
//
// Created by Travis Bowers on 4/2/15.
// Copyright (c) 2015 Jeff Mesnil. All rights reserved.
//

#import "SKWebSocket.h"
#import "GCDAsyncSocket.h"
#import "SRWebSocket.h"

NSString *const SKWebSocketErrorDomain = @"SKWebSocketErrorDomain";

@interface SKWebSocket() <SRWebSocketDelegate>
@property (nonatomic, weak) id <SKSocketDelegate> delegate;
@property (nonatomic, retain) SRWebSocket *socket;
@property (nonatomic, assign) BOOL connected;
@end

@implementation SKWebSocket

// synthesize properties
@synthesize delegate;
@synthesize socket;

- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq {
if((self = [super init])) {
if (aDelegate != nil) {
self.delegate = aDelegate;
self.connected = NO;
}
}

return self;
}

- (BOOL)connectToHost:(NSString*)host onPort:(uint16_t)port error:(NSError **)errPtr {
self.socket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:host]];
self.socket.delegate = self;
[self.socket open];
return YES;
}

- (BOOL)isDisconnected {
return !self.connected;
}

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout {
// convert data to string and send
[socket send:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}

- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout {
// not supported
//[socket readDataToData:data withTimeout:timeout tag:tag];
}

- (void)disconnectAfterReadingAndWriting {
[socket close];
}

#pragma mark -
#pragma mark SRWebSocketDelegate
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
[delegate socket:(SKSocket*)self didReadDataWithString:message];
}

- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
self.connected = YES;
[delegate socket:(SKSocket*)self didConnectToHost:self.socket.url.absoluteString port:80];
}

- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
self.connected = NO;
[delegate socketDidDisconnect:(SKSocket*)self withError:error];
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
self.connected = NO;

NSError *error = nil;

if (wasClean == NO) {
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: @"Socket did close",
NSLocalizedFailureReasonErrorKey: reason
};
error = [NSError errorWithDomain:SKWebSocketErrorDomain code:-57 userInfo:userInfo];
}

[delegate socketDidDisconnect:(SKSocket*)self withError:error];
}

@end
5 changes: 3 additions & 2 deletions StompKit/StompKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ typedef void (^STOMPMessageHandler)(STOMPMessage *message);
@property (nonatomic, copy) void (^errorHandler)(NSError *error);
@property (nonatomic, assign) BOOL connected;

- (id)initWithHost:(NSString *)theHost
port:(NSUInteger)thePort;
- (id)initWithHost:(NSString *)theHost;
- (id)initWithHost:(NSString *)theHost andPort:(NSUInteger)thePort;

- (void)connectWithCompletionHandler:(void (^)(STOMPFrame *connectedFrame, NSError *error))completionHandler;
- (void)connectWithLogin:(NSString *)login
passcode:(NSString *)passcode
completionHandler:(void (^)(STOMPFrame *connectedFrame, NSError *error))completionHandler;
Expand Down
Loading