Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

For custom resource and http header #210

Open
wants to merge 4 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
Binary file added Default-568h@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions SocketIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ typedef enum {

@property (nonatomic, readonly) NSString *host;
@property (nonatomic, readonly) NSInteger port;
@property (nonatomic) NSString *resource;
@property (nonatomic, readonly) NSString *sid;
@property (nonatomic, readonly) NSTimeInterval heartbeatTimeout;
@property (nonatomic) BOOL useSecure;
@property (nonatomic) NSArray *cookies;
@property (nonatomic) NSDictionary *headers;
@property (nonatomic, readonly) BOOL isConnected, isConnecting;
@property (nonatomic, weak) id<SocketIODelegate> delegate;
@property (nonatomic) BOOL returnAllDataFromAck;
Expand All @@ -117,6 +119,4 @@ typedef enum {
- (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function;
- (void) sendAcknowledgement:(NSString*)pId withArgs:(NSArray *)data;

- (void) setResourceName:(NSString *)name;

@end
35 changes: 18 additions & 17 deletions SocketIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#define DEBUGLOG(...)
#endif

static NSString* kResourceName = @"socket.io";
static NSString* kHandshakeURL = @"%@://%@%@/%@/1/?t=%.0f%@";
static NSString* kForceDisconnectURL = @"%@://%@%@/%@/1/xhr-polling/%@?disconnect";

Expand Down Expand Up @@ -90,6 +89,7 @@ - (id) initWithDelegate:(id<SocketIODelegate>)delegate
_ackCount = 0;
_acks = [[NSMutableDictionary alloc] init];
_returnAllDataFromAck = NO;
self.resource = @"socket.io";
}
return self;
}
Expand Down Expand Up @@ -128,15 +128,17 @@ - (void) connectToHost:(NSString *)host

// create a query parameters string
NSMutableString *query = [[NSMutableString alloc] initWithString:@""];
[params enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
[query appendFormat:@"&%@=%@", key, value];
[params enumerateKeysAndObjectsUsingBlock: ^(id key, id val, BOOL *stop) {
NSString *k = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *v = [val stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[query appendFormat:@"&%@=%@", k, v];
}];

// do handshake via HTTP request
NSString *protocol = _useSecure ? @"https" : @"http";
NSString *port = _port ? [NSString stringWithFormat:@":%d", _port] : @"";
NSString *port = _port ? [NSString stringWithFormat:@":%ld", (long)_port] : @"";
NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
NSString *handshakeUrl = [NSString stringWithFormat:kHandshakeURL, protocol, _host, port, kResourceName, time, query];
NSString *handshakeUrl = [NSString stringWithFormat:kHandshakeURL, protocol, _host, port, self.resource, time, query];

DEBUGLOG(@"Connecting to socket with URL: %@", handshakeUrl);
query = nil;
Expand All @@ -151,9 +153,14 @@ - (void) connectToHost:(NSString *)host
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:_cookies];
[request setAllHTTPHeaderFields:headers];
}

[request setHTTPShouldHandleCookies:YES];


if (self.headers) {
[self.headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[request setValue:obj forHTTPHeaderField:key];
}];
}

_handshake = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[_handshake scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[_handshake start];
Expand Down Expand Up @@ -182,8 +189,8 @@ - (void) disconnect
- (void) disconnectForced
{
NSString *protocol = [self useSecure] ? @"https" : @"http";
NSString *port = _port ? [NSString stringWithFormat:@":%d", _port] : @"";
NSString *urlString = [NSString stringWithFormat:kForceDisconnectURL, protocol, _host, port, kResourceName, _sid];
NSString *port = _port ? [NSString stringWithFormat:@":%ld", (long)_port] : @"";
NSString *urlString = [NSString stringWithFormat:kForceDisconnectURL, protocol, _host, port, self.resource, _sid];
NSURL *url = [NSURL URLWithString:urlString];
DEBUGLOG(@"Force disconnect at: %@", urlString);

Expand Down Expand Up @@ -259,13 +266,7 @@ - (void) sendAcknowledgement:(NSString *)pId withArgs:(NSArray *)data
[self send:packet];
}

- (void) setResourceName:(NSString *)name
{
kResourceName = [name copy];
}

# pragma mark -
# pragma mark private methods
# pragma mark - private methods

- (void) sendDisconnect
{
Expand Down Expand Up @@ -653,7 +654,7 @@ - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespo
// check for server status code (http://gigliwood.com/weblog/Cocoa/Q__When_is_an_conne.html)
if ([response respondsToSelector:@selector(statusCode)]) {
NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode];
DEBUGLOG(@"didReceiveResponse() %i", statusCode);
DEBUGLOG(@"didReceiveResponse() %li", (long)statusCode);

if (statusCode >= 400) {
// stop connecting; no more delegate messages
Expand Down
1 change: 1 addition & 0 deletions SocketIOTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

@property (nonatomic, readonly) NSString *host;
@property (nonatomic, readonly) NSInteger port;
@property (nonatomic, readonly) NSString *resource;
@property (nonatomic, readonly) NSString *sid;
@property (nonatomic, readonly) NSTimeInterval heartbeatTimeout;
@property (nonatomic) BOOL useSecure;
Expand Down
12 changes: 6 additions & 6 deletions SocketIOTransportWebsocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#define DEBUGLOG(...)
#endif

static NSString* kInsecureSocketURL = @"ws://%@/socket.io/1/websocket/%@";
static NSString* kSecureSocketURL = @"wss://%@/socket.io/1/websocket/%@";
static NSString* kInsecureSocketPortURL = @"ws://%@:%d/socket.io/1/websocket/%@";
static NSString* kSecureSocketPortURL = @"wss://%@:%d/socket.io/1/websocket/%@";
static NSString* kInsecureSocketURL = @"ws://%@/%@/1/websocket/%@";
static NSString* kSecureSocketURL = @"wss://%@/%@/1/websocket/%@";
static NSString* kInsecureSocketPortURL = @"ws://%@:%d/%@/1/websocket/%@";
static NSString* kSecureSocketPortURL = @"wss://%@:%d/%@/1/websocket/%@";

@implementation SocketIOTransportWebsocket

Expand All @@ -58,11 +58,11 @@ - (void) open
NSString *format;
if (delegate.port) {
format = delegate.useSecure ? kSecureSocketPortURL : kInsecureSocketPortURL;
urlStr = [NSString stringWithFormat:format, delegate.host, delegate.port, delegate.sid];
urlStr = [NSString stringWithFormat:format, delegate.host, delegate.port, delegate.resource, delegate.sid];
}
else {
format = delegate.useSecure ? kSecureSocketURL : kInsecureSocketURL;
urlStr = [NSString stringWithFormat:format, delegate.host, delegate.sid];
urlStr = [NSString stringWithFormat:format, delegate.host, delegate.resource, delegate.sid];
}
NSURL *url = [NSURL URLWithString:urlStr];

Expand Down
12 changes: 6 additions & 6 deletions SocketIOTransportXHR.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#define DEBUGLOG(...)
#endif

static NSString* kInsecureXHRURL = @"http://%@/socket.io/1/xhr-polling/%@";
static NSString* kSecureXHRURL = @"https://%@/socket.io/1/xhr-polling/%@";
static NSString* kInsecureXHRPortURL = @"http://%@:%d/socket.io/1/xhr-polling/%@";
static NSString* kSecureXHRPortURL = @"https://%@:%d/socket.io/1/xhr-polling/%@";
static NSString* kInsecureXHRURL = @"http://%@/%@/1/xhr-polling/%@";
static NSString* kSecureXHRURL = @"https://%@/%@/1/xhr-polling/%@";
static NSString* kInsecureXHRPortURL = @"http://%@:%d/%@/1/xhr-polling/%@";
static NSString* kSecureXHRPortURL = @"https://%@:%d/%@/1/xhr-polling/%@";

@interface SocketIOTransportXHR (Private)
- (void) checkAndStartPoll;
Expand Down Expand Up @@ -61,11 +61,11 @@ - (void) open
NSString *format;
if (delegate.port) {
format = delegate.useSecure ? kSecureXHRPortURL : kInsecureXHRPortURL;
_url = [NSString stringWithFormat:format, delegate.host, delegate.port, delegate.sid];
_url = [NSString stringWithFormat:format, delegate.host, delegate.port, delegate.resource, delegate.sid];
}
else {
format = delegate.useSecure ? kSecureXHRURL : kInsecureXHRURL;
_url = [NSString stringWithFormat:format, delegate.host, delegate.sid];
_url = [NSString stringWithFormat:format, delegate.host, delegate.resource, delegate.sid];
}
DEBUGLOG(@"Opening XHR @ %@", _url);
[self poll:nil];
Expand Down
26 changes: 21 additions & 5 deletions SocketTesterARC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
4ADCCBF315790FDF0022990C /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4ADCCBF215790FDF0022990C /* SystemConfiguration.framework */; };
4ADCCD4D157915F00022990C /* SocketIO.m in Sources */ = {isa = PBXBuildFile; fileRef = 4ADCCBC715790DEC0022990C /* SocketIO.m */; };
C9E391A215E2A1B00004693A /* SocketIOJSONSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E391A115E2A1B00004693A /* SocketIOJSONSerialization.m */; };
F4FF849E1A07830300D8A773 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F4FF849D1A07830300D8A773 /* Default-568h@2x.png */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -67,6 +68,7 @@
4ADCCBF215790FDF0022990C /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
C9E391A015E2A1B00004693A /* SocketIOJSONSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SocketIOJSONSerialization.h; sourceTree = SOURCE_ROOT; };
C9E391A115E2A1B00004693A /* SocketIOJSONSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocketIOJSONSerialization.m; sourceTree = SOURCE_ROOT; };
F4FF849D1A07830300D8A773 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -105,6 +107,7 @@
4ADCCB9215790D760022990C = {
isa = PBXGroup;
children = (
F4FF849D1A07830300D8A773 /* Default-568h@2x.png */,
4ADCCBA715790D760022990C /* SocketTesterARC */,
4ADCCBA015790D760022990C /* Frameworks */,
4ADCCB9E15790D760022990C /* Products */,
Expand Down Expand Up @@ -196,7 +199,7 @@
4ADCCB9415790D760022990C /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0460;
LastUpgradeCheck = 0610;
ORGANIZATIONNAME = beta_interactive;
};
buildConfigurationList = 4ADCCB9715790D760022990C /* Build configuration list for PBXProject "SocketTesterARC" */;
Expand All @@ -223,6 +226,7 @@
files = (
4ADCCBAC15790D760022990C /* InfoPlist.strings in Resources */,
4ADCCBB815790D760022990C /* ViewController.xib in Resources */,
F4FF849E1A07830300D8A773 /* Default-568h@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -273,14 +277,17 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand All @@ -290,10 +297,13 @@
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
};
Expand All @@ -303,20 +313,26 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos;
Expand Down
15 changes: 15 additions & 0 deletions socket.IO.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Pod::Spec.new do |s|
s.name = "socket.IO"
s.version = "0.5.2"
s.summary = "socket.io v0.7.2+ for iOS devices"
s.description = <<-DESC
Interface to communicate between Objective C and Socket.IO with the help of websockets. It's based on fpotter's socketio-cocoa and uses other libraries/classes like SocketRocket, json-framework (optional) and jsonkit (optional).
DESC
s.homepage = "https://github.com/pkyeck/socket.IO-objc"
s.license = 'MIT'
s.author = { "Philipp Kyeck" => "philipp@beta-interactive.de" }
s.source = { :git => "https://github.com/EchoLiao/socket.IO-objc.git", :tag => s.version.to_s }
s.source_files = '*.{h,m}'
s.requires_arc = true
s.dependency 'SocketRocket', '~> 0.2'
end