diff --git a/README.md b/README.md index 20b9785..16b34b2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -**This fork supports some socket.io v1.0 features, (no binary support yet). Many thanks to francoisTemasys who did most of the heavy lifting; this is a fork of a fork. v0.9.x compatibility is generally maintained. +**This fork supports some socket.io v1.x features, (no binary support yet). Many thanks to francoisTemasys who did most of the heavy lifting; this is a fork of a fork. v0.9.x compatibility is generally maintained. --- @@ -15,8 +15,7 @@ ## Requirements -As of version 0.4, this library requires at least OS X 10.7 or iOS 5.0. -Because of this, we were able to remove the external JSON frameworks in v0.5 and only rely on iOS' own `NSJSONSerialization`. +This library requires at least OS X 10.7 or iOS 5.0. ## Usage diff --git a/SocketIO.m b/SocketIO.m index 6cfc636..0b2c455 100755 --- a/SocketIO.m +++ b/SocketIO.m @@ -135,7 +135,7 @@ - (void) connectToHost:(NSString *)host // 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,kTransportPolling, time, query]; //@"%@://%@%@/%@/1/?t=%.0f%@"; @@ -185,7 +185,7 @@ - (void) disconnect - (void) disconnectForced { NSString *protocol = [self useSecure] ? @"https" : @"http"; - NSString *port = _port ? [NSString stringWithFormat:@":%d", _port] : @""; + NSString *port = _port ? [NSString stringWithFormat:@":%ld", (long)_port] : @""; NSString *urlString = [NSString stringWithFormat:kForceDisconnectURL, protocol, _host, port, kResourceName, _sid]; NSURL *url = [NSURL URLWithString:urlString]; DEBUGLOG(@"Force disconnect at: %@", urlString); @@ -291,6 +291,7 @@ - (void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(Socke if (function) { packet.ack = @"data"; } + NSLog(@"%@", packet.data); [self send:packet]; } @@ -338,7 +339,7 @@ - (void) send:(SocketIOPacket *)packet return; } DEBUGLOG(@"Prepare to send()"); - + packet.endpoint = _endpoint; NSString *req = [packet toString]; if (![_transport isReady]) { @@ -686,10 +687,10 @@ - (void) onData:(NSString *)data //?? break; case 2: - { + //Ping->Pong [self sendMessage:[NSString stringWithFormat:@"3:%@", data]]; - } break; + break; case 3: //Pong if([data isEqualToString:@"probe"]) @@ -708,7 +709,7 @@ - (void) onData:(NSString *)data //GET ENDPOINT NSUInteger rendpoint = [packet.data rangeOfString:@"["].location; if(rendpoint == NSNotFound) - packet.endpoint = @""; + packet.endpoint = packet.data; else packet.endpoint = [packet.data substringToIndex:rendpoint]; @@ -741,7 +742,8 @@ - (void) onData:(NSString *)data packet.args = [parsedData subarrayWithRange:NSMakeRange(1, parsedData.count-1)]; if([packet.name isEqualToString:@"message"]) { - packet.data = packet.args[0]; + // data is specified as NSString, so rewrap it... + packet.data = [SocketIOJSONSerialization JSONStringFromObject:packet.args[0] error:nil]; if ([_delegate respondsToSelector:@selector(socketIO:didReceiveMessage:)]) { [_delegate socketIO:self didReceiveMessage:packet]; } @@ -759,8 +761,8 @@ - (void) onData:(NSString *)data { if (ack > 0) { - int ackId = ack; - DEBUGLOG(@"ack id found: %d", ackId); + NSUInteger ackId = ack; + DEBUGLOG(@"ack id found: %d", (unsigned int)ackId); NSString *argsStr = [packet.data substringFromIndex:1 ]; argsStr = [argsStr substringToIndex:argsStr.length-1]; @@ -774,7 +776,7 @@ - (void) onData:(NSString *)data } // get selector for ackId - NSString *key = [NSString stringWithFormat:@"%d", ackId]; + NSString *key = [NSString stringWithFormat:@"%d", (unsigned int)ackId]; SocketIOCallback callbackFunction = [_acks objectForKey:key]; if (callbackFunction != nil) { callbackFunction(argsData); @@ -864,7 +866,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() %i", (int)statusCode); if (statusCode >= 400) { // stop connecting; no more delegate messages @@ -1009,7 +1011,8 @@ - (void) connectionDidFinishLoading:(NSURLConnection *)connection DEBUGLOG(@"VERSION 10x"); _version = V10x; //...0{"sid":"","upgrades":[,...],"pingInterval":,"pingTimeout":} - responseString = [responseString substringFromIndex:[responseString rangeOfString:@"{"].location]; + if([responseString rangeOfString:@"{"].location != NSNotFound) + responseString = [responseString substringFromIndex:[responseString rangeOfString:@"{"].location]; DEBUGLOG(@"Response %@", responseString); connectionFailed = true; NSData *utf8Data = [responseString dataUsingEncoding:NSUTF8StringEncoding]; diff --git a/SocketIOPacket.h b/SocketIOPacket.h index 8fcfafe..b13be07 100644 --- a/SocketIOPacket.h +++ b/SocketIOPacket.h @@ -45,15 +45,15 @@ @property (nonatomic, copy) NSArray *args; - (id) initWithType:(NSString *)packetType; -- (id) initWithTypeIndex:(int)index; +- (id) initWithTypeIndex:(NSUInteger)index; - (id) dataAsJSON; - (NSString *) toString; - (NSNumber *) typeAsNumber; -- (NSString *) typeForIndex:(int)index; +- (NSString *) typeForIndex:(NSUInteger)index; + (SocketIOPacket *) createPacketWithType:(NSString *)type version:(SocketIOVersion) version; -+ (SocketIOPacket *) createPacketWithTypeIndex:(int) type ++ (SocketIOPacket *) createPacketWithTypeIndex:(NSUInteger) type version:(SocketIOVersion) version; @end diff --git a/SocketIOPacket.m b/SocketIOPacket.m index cdac17c..5e5be67 100644 --- a/SocketIOPacket.m +++ b/SocketIOPacket.m @@ -55,7 +55,7 @@ - (id) initWithType:(NSString *)packetType return self; } -- (id) initWithTypeIndex:(int)index +- (id) initWithTypeIndex:(NSUInteger)index { self = [self init]; if (self) { @@ -78,37 +78,49 @@ - (id) dataAsJSON - (NSString *) toString { - NSMutableArray *encoded = [NSMutableArray arrayWithObject:[self typeAsNumber]]; + NSNumber *typeAsNumber = [self typeAsNumber]; + NSMutableArray *encoded = [NSMutableArray arrayWithObject:typeAsNumber]; + NSNumber *typeNumber = [self typeAsNumber]; + if (!(self.endpoint == nil || [@"/" isEqualToString:self.endpoint]) && [typeNumber intValue] != 6 && [typeNumber intValue] != 2) + { + [encoded addObject:[self.endpoint stringByAppendingString:@","]]; + } + NSString *pIdL = self.pId != nil ? self.pId : @""; if( !([self isKindOfClass:[SocketIOPacketV10x class]]) ){ if ([self.ack isEqualToString:@"data"]) { - pIdL = [pIdL stringByAppendingString:@"+"]; + //pIdL = [pIdL stringByAppendingString:@"+"]; + } + } else { + // Engine.IO 1.0 expects payload with the ping packet + if ([typeAsNumber intValue] == 2) { + [encoded addObject:@"probe"]; } } // Do not write pid for acknowledgements - if ([type intValue] != 6) { + if ([typeNumber intValue] != 6) { [encoded addObject:pIdL]; } // Add the end point for the namespace to be used, as long as it is not // an ACK, heartbeat, or disconnect packet - if ([type intValue] != 6 && [type intValue] != 2 && [type intValue] != 0) { + /*if ([type intValue] != 6 && [type intValue] != 2 && [type intValue] != 0) { [encoded addObject:endpoint]; } else { [encoded addObject:@""]; - } + }*/ if (data != nil) { NSString *ackpId = @""; // This is an acknowledgement packet, so, prepend the ack pid to the data - if ([type intValue] == 6) + if ([typeNumber intValue] == 6) { if( !([self isKindOfClass:[SocketIOPacketV10x class]]) ) ackpId = [NSString stringWithFormat:@":%@%@", pIdL, @"+"]; @@ -126,7 +138,7 @@ - (NSNumber *) typeAsNumber return num; } -- (NSString *) typeForIndex:(int)index +- (NSString *) typeForIndex:(NSUInteger)index { return [_types objectAtIndex:index]; } @@ -144,7 +156,7 @@ + (SocketIOPacket *) createPacketWithType:(NSString *)type } } -+ (SocketIOPacket *) createPacketWithTypeIndex:(int) type ++ (SocketIOPacket *) createPacketWithTypeIndex:(NSUInteger) type version:(SocketIOVersion) version { switch (version) { diff --git a/SocketIOTransportWebsocket.m b/SocketIOTransportWebsocket.m index c88c2b9..afd0f11 100644 --- a/SocketIOTransportWebsocket.m +++ b/SocketIOTransportWebsocket.m @@ -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://%@/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/%@"; @implementation SocketIOTransportWebsocket @@ -62,17 +62,17 @@ - (void) openUsing:(SocketIOVersion)version { NSString *urlStr; NSString *format; - NSString *addOnVersion = @""; + NSString *addOnVersion = delegate.sid;; if(version == V10x) - addOnVersion = @"?EIO=2&transport=websocket&sid="; + addOnVersion = [NSString stringWithFormat:@"?EIO=2&transport=websocket&sid=%@", delegate.sid]; if (delegate.port) { format = delegate.useSecure ? kSecureSocketPortURL : kInsecureSocketPortURL; - urlStr = [NSString stringWithFormat:format, delegate.host, delegate.port, addOnVersion, delegate.sid]; + urlStr = [NSString stringWithFormat:format, delegate.host, delegate.port, addOnVersion, delegate.sid, addOnVersion]; } else { format = delegate.useSecure ? kSecureSocketURL : kInsecureSocketURL; - urlStr = [NSString stringWithFormat:format, delegate.host,addOnVersion, delegate.sid]; + urlStr = [NSString stringWithFormat:format, delegate.host,addOnVersion, delegate.sid, addOnVersion]; } NSURL *url = [NSURL URLWithString:urlStr]; diff --git a/SocketTesterARC.xcodeproj/project.pbxproj b/SocketTesterARC.xcodeproj/project.pbxproj index e8e4616..9bb00cd 100644 --- a/SocketTesterARC.xcodeproj/project.pbxproj +++ b/SocketTesterARC.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 00DC61D01A9FC62100136DE1 /* socket.io-objc.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 00DC61CF1A9FC62100136DE1 /* socket.io-objc.podspec */; }; 4A444FA11589E28800B44ABB /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A444FA01589E28800B44ABB /* libicucore.dylib */; }; 4A444FA31589E29500B44ABB /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A444FA21589E29500B44ABB /* Security.framework */; }; 4A4453811589EE9100B44ABB /* base64.c in Sources */ = {isa = PBXBuildFile; fileRef = 4A44537A1589EE9100B44ABB /* base64.c */; }; @@ -31,6 +32,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 00DC61CF1A9FC62100136DE1 /* socket.io-objc.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "socket.io-objc.podspec"; sourceTree = SOURCE_ROOT; }; 4A444FA01589E28800B44ABB /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; }; 4A444FA21589E29500B44ABB /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; 4A44537A1589EE9100B44ABB /* base64.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = base64.c; path = "submodules/socket-rocket/SocketRocket/base64.c"; sourceTree = SOURCE_ROOT; }; @@ -146,6 +148,7 @@ 4ADCCBC615790DEC0022990C /* SocketIO.h */, 4ADCCBC715790DEC0022990C /* SocketIO.m */, 4AD96DF21680853E00D9E42D /* SocketIOPacket.h */, + 00DC61CF1A9FC62100136DE1 /* socket.io-objc.podspec */, 4AD96DF31680853E00D9E42D /* SocketIOPacket.m */, C9E391A015E2A1B00004693A /* SocketIOJSONSerialization.h */, C9E391A115E2A1B00004693A /* SocketIOJSONSerialization.m */, @@ -221,6 +224,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 00DC61D01A9FC62100136DE1 /* socket.io-objc.podspec in Resources */, 4ADCCBAC15790D760022990C /* InfoPlist.strings in Resources */, 4ADCCBB815790D760022990C /* ViewController.xib in Resources */, ); diff --git a/socket.io-objc.podspec b/socket.io-objc.podspec new file mode 100644 index 0000000..abcf9e3 --- /dev/null +++ b/socket.io-objc.podspec @@ -0,0 +1,15 @@ +Pod::Spec.new do |spec| +spec.name = 'socket.io-objc' +spec.version = '0.6.4' +spec.license = 'MIT' +spec.homepage = 'https://github.com/francoisp/socket.IO-objc/' +spec.authors = { 'Philipp Kyeck' => 'philipp@beta-interactive.de' } +spec.summary = 'Socket.io 1.x client for Objective-C projects., with backward 0.9 compatibility' +spec.description = "Interface to communicate between Objective C and Socket.IO with the help of websockets. Originally based on fpotter's socketio-cocoa and uses square's SocketRocket.\n" +spec.source = { :git => 'https://github.com/francoisp/socket.IO-objc.git' } +spec.source_files = '*.{h,m}' +spec.requires_arc = true +spec.dependency 'SocketRocket' +spec.platforms = { "ios" => "6.0", +"osx" => "10.8" } +end \ No newline at end of file diff --git a/submodules/.gitignore b/submodules/.gitignore deleted file mode 100644 index 08b418a..0000000 --- a/submodules/.gitignore +++ /dev/null @@ -1,30 +0,0 @@ -# xcode noise -build/* -build -*.pbxuser -*.mode1v3 -*.mode2v3 -*.perspectivev3 -*.tmproj -*/*.pbxuser -*/*.mode1v3 -*/*.mode2v3 -*/*.perspectivev3 -*/*.tmproj -Build -Products -xcuserdata -xcuserdata/* -project.xcworkspace -project.xcworkspace/* -*/project.xcworkspace -*/project.xcworkspace/* -*/xcuserdata -DerivedData - -# osx noise -.DS_Store -profile - -# vim noise -*.swp diff --git a/submodules/socket-rocket b/submodules/socket-rocket deleted file mode 160000 index 8275ef3..0000000 --- a/submodules/socket-rocket +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8275ef3b15cebd99b1815840c55f4e07f96bf862