Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: MegaBits/SIOSocket
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.2.2
Choose a base ref
...
head repository: MegaBits/SIOSocket
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 13 commits
  • 4 files changed
  • 4 contributors

Commits on Jan 11, 2015

  1. Ability to specify list of engine.io transports through the SIOSocket…

    … client. Over-loaded to preserve API backward-compatibility and used default that is contained within the socket.io-client.
    brandonramirez committed Jan 11, 2015
    Copy the full SHA
    89efd80 View commit details

Commits on Feb 4, 2015

  1. Thread issue fixes

    Added usage of internal webview thread.
    Minaev Alexey committed Feb 4, 2015
    Copy the full SHA
    25bf19c View commit details

Commits on Feb 5, 2015

  1. Added serialisation check

    Minaev Alexey committed Feb 5, 2015
    Copy the full SHA
    955ca5e View commit details

Commits on Feb 6, 2015

  1. Thread sync problem issue fix

    Minaev Alexey committed Feb 6, 2015
    Copy the full SHA
    190f493 View commit details

Commits on Feb 10, 2015

  1. Copy the full SHA
    b4e8eef View commit details
  2. Copy the full SHA
    4dd3498 View commit details

Commits on Feb 17, 2015

  1. Merge pull request #38 from brandonramirez/master

    Ability to specify list of engine.io transports through the SIOSocket client.
    pcperini committed Feb 17, 2015
    Copy the full SHA
    6d9a315 View commit details
  2. merge

    pcperini committed Feb 17, 2015
    Copy the full SHA
    cf9109d View commit details
  3. Merge pull request #45 from syrp/thread-fixes

    Threading fixes
    pcperini committed Feb 17, 2015
    Copy the full SHA
    89fba37 View commit details

Commits on May 8, 2015

  1. Update README.md

    pcperini committed May 8, 2015
    Copy the full SHA
    82425d0 View commit details

Commits on Nov 9, 2015

  1. Update README.md

    pcperini committed Nov 9, 2015
    Copy the full SHA
    c1e3af8 View commit details

Commits on Jan 13, 2016

  1. Update README.md

    pcperini committed Jan 13, 2016
    Copy the full SHA
    43fc9a5 View commit details
  2. Update README.md

    pcperini committed Jan 13, 2016
    Copy the full SHA
    a9cbf05 View commit details
Showing with 100 additions and 41 deletions.
  1. +8 −2 README.md
  2. +2 −0 SocketIO/Source/SIOSocket.h
  3. +72 −23 SocketIO/Source/SIOSocket.m
  4. +18 −16 SocketIO/Source/socket.io.js.h
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

# SIOSocket

SIOSocket is simple interface for communicating with [socket.io 1.0](http://socket.io) from iOS.
SIOSocket is simple interface for communicating with [Socket.IO 1.0](http://socket.io) from iOS.

__There is now a [first-party iOS client](http://socket.io/blog/socket-io-on-ios/) for Socket.IO!__ (Therefore, this library is no longer being actively developed.) Congrats to the Socket.IO team for a job well done.

[![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/)


## How to use

@@ -54,7 +59,7 @@ Generates a new `SIOSocket` object, begins its connection to the given host, and

The host reachable at the given URL string should be running a valid instance of a socket.io server.

#### `+ (void)socketWithHost:reconnectAutomatically:attemptLimit:withDelay:maximumDelay:timeout:response:`
#### `+ (void)socketWithHost:reconnectAutomatically:attemptLimit:withDelay:maximumDelay:timeout:withTransports:response:`

- `reconnectAutomatically` whether to reconnect automatically (`YES`)
- `attemptLimit` number of times to attempt a reconnect (Infinite)
@@ -64,6 +69,7 @@ reconnection (`1`)
reconnections (`5`). Each attempt increases the reconnection by
the amount specified by `reconnectionDelay`.
- `timeout` connection timeout before an `onReconnectionError` event is emitted (`20`)
- `withTransports` specifies an array of transports for engine.io (default is 'polling', 'websocket').

## Properties

2 changes: 2 additions & 0 deletions SocketIO/Source/SIOSocket.h
Original file line number Diff line number Diff line change
@@ -24,9 +24,11 @@ typedef NSArray SIOParameterArray;
// Generators
+ (void)socketWithHost:(NSString *)hostURL response:(void(^)(SIOSocket *socket))response;
+ (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnectAutomatically attemptLimit:(NSInteger)attempts withDelay:(NSTimeInterval)reconnectionDelay maximumDelay:(NSTimeInterval)maximumDelay timeout:(NSTimeInterval)timeout response:(void(^)(SIOSocket *socket))response;
+ (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnectAutomatically attemptLimit:(NSInteger)attempts withDelay:(NSTimeInterval)reconnectionDelay maximumDelay:(NSTimeInterval)maximumDelay timeout:(NSTimeInterval)timeout withTransports:(NSArray *)transports response:(void(^)(SIOSocket *socket))response;

// Event responders
@property (nonatomic, copy) void (^onConnect)();
@property (nonatomic, copy) void (^onConnectError)(NSDictionary *errorInfo);
@property (nonatomic, copy) void (^onDisconnect)();
@property (nonatomic, copy) void (^onError)(NSDictionary *errorInfo);

95 changes: 72 additions & 23 deletions SocketIO/Source/SIOSocket.m
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@

@interface SIOSocket ()

@property (nonatomic, strong) NSThread *thread;
@property UIWebView *javascriptWebView;
@property (readonly) JSContext *javascriptContext;

@@ -50,9 +51,22 @@ + (void)socketWithHost:(NSString *)hostURL response:(void (^)(SIOSocket *))respo
}

+ (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnectAutomatically attemptLimit:(NSInteger)attempts withDelay:(NSTimeInterval)reconnectionDelay maximumDelay:(NSTimeInterval)maximumDelay timeout:(NSTimeInterval)timeout response:(void (^)(SIOSocket *))response {
return [self socketWithHost: hostURL
reconnectAutomatically: YES
attemptLimit: -1
withDelay: 1
maximumDelay: 5
timeout: 20
withTransports: @[ @"polling", @"websocket" ]
response: response];
}

+ (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnectAutomatically attemptLimit:(NSInteger)attempts withDelay:(NSTimeInterval)reconnectionDelay maximumDelay:(NSTimeInterval)maximumDelay timeout:(NSTimeInterval)timeout withTransports:(NSArray*)transports response:(void (^)(SIOSocket *))response {
SIOSocket *socket = [[SIOSocket alloc] init];
if (!socket) {
response(nil);
dispatch_async(dispatch_get_main_queue(), ^{
response(nil);
});
return;
}

@@ -63,6 +77,7 @@ + (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnec
}];

socket.javascriptContext[@"window"][@"onload"] = ^() {
socket.thread = [NSThread currentThread];
[socket.javascriptContext evaluateScript: socket_io_js];
[socket.javascriptContext evaluateScript: blob_factory_js];

@@ -71,44 +86,66 @@ + (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnec
attempts,
reconnectionDelay,
maximumDelay,
timeout
timeout,
transports
);

socket.javascriptContext[@"objc_socket"] = [socket.javascriptContext evaluateScript: socketConstructor];
if (![socket.javascriptContext[@"objc_socket"] toObject]) {
response(nil);
dispatch_async(dispatch_get_main_queue(), ^{
response(nil);
});
}

// Responders
__weak typeof(socket) weakSocket = socket;
socket.javascriptContext[@"objc_onConnect"] = ^() {
if (weakSocket.onConnect)
weakSocket.onConnect();
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onConnect)
weakSocket.onConnect();
});
};

socket.javascriptContext[@"objc_onConnectError"] = ^(NSDictionary *errorDictionary) {
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onConnectError)
weakSocket.onConnectError(errorDictionary);
});
};

socket.javascriptContext[@"objc_onDisconnect"] = ^() {
if (weakSocket.onDisconnect)
weakSocket.onDisconnect();
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onDisconnect)
weakSocket.onDisconnect();
});
};

socket.javascriptContext[@"objc_onError"] = ^(NSDictionary *errorDictionary) {
if (weakSocket.onError)
weakSocket.onError(errorDictionary);
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onError)
weakSocket.onError(errorDictionary);
});
};

socket.javascriptContext[@"objc_onReconnect"] = ^(NSInteger numberOfAttempts) {
if (weakSocket.onReconnect)
weakSocket.onReconnect(numberOfAttempts);
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onReconnect)
weakSocket.onReconnect(numberOfAttempts);
});
};

socket.javascriptContext[@"objc_onReconnectionAttempt"] = ^(NSInteger numberOfAttempts) {
if (weakSocket.onReconnectionAttempt)
weakSocket.onReconnectionAttempt(numberOfAttempts);
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onReconnectionAttempt)
weakSocket.onReconnectionAttempt(numberOfAttempts);
});
};

socket.javascriptContext[@"objc_onReconnectionError"] = ^(NSDictionary *errorDictionary) {
if (weakSocket.onReconnectionError)
weakSocket.onReconnectionError(errorDictionary);
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSocket.onReconnectionError)
weakSocket.onReconnectionError(errorDictionary);
});
};

[socket.javascriptContext evaluateScript: @"objc_socket.on('connect', objc_onConnect);"];
@@ -118,7 +155,9 @@ + (void)socketWithHost:(NSString *)hostURL reconnectAutomatically:(BOOL)reconnec
[socket.javascriptContext evaluateScript: @"objc_socket.on('reconnecting', objc_onReconnectionAttempt);"];
[socket.javascriptContext evaluateScript: @"objc_socket.on('reconnect_error', objc_onReconnectionError);"];

response(socket);
dispatch_async(dispatch_get_main_queue(), ^{
response(socket);
});
};

[socket.javascriptWebView loadHTMLString: @"<html/>" baseURL: nil];
@@ -144,10 +183,13 @@ - (void)on:(NSString *)event callback:(void (^)(SIOParameterArray *args))functio
}
}

function(arguments);
dispatch_async(dispatch_get_main_queue(), ^{
function(arguments);
});
};

[self.javascriptContext evaluateScript: [NSString stringWithFormat: @"objc_socket.on('%@', objc_%@);", event, eventID]];
NSString* script = [NSString stringWithFormat: @"objc_socket.on('%@', objc_%@);", event, eventID];
[self performSelector:@selector(evaluateScript:) onThread:_thread withObject:[script copy] waitUntilDone:NO];
}

// Emitters
@@ -172,13 +214,20 @@ - (void)emit:(NSString *)event args:(SIOParameterArray *)args {
[arguments addObject: [NSString stringWithFormat: @"blob('%@')", dataString]];
}
else if ([arg isKindOfClass: [NSArray class]] || [arg isKindOfClass: [NSDictionary class]]) {
[arguments addObject: [[NSString alloc] initWithData: [NSJSONSerialization dataWithJSONObject: arg options: 0 error: nil] encoding: NSUTF8StringEncoding]];
if ([NSJSONSerialization isValidJSONObject:arg]) {
[arguments addObject: [[NSString alloc] initWithData: [NSJSONSerialization dataWithJSONObject: arg options: 0 error: nil] encoding: NSUTF8StringEncoding]];
} else {
NSLog(@"SIOSocket serialization error at %@", NSStringFromSelector(_cmd));
}
}
}

dispatch_async(dispatch_get_main_queue(), ^{
[self.javascriptContext evaluateScript: [NSString stringWithFormat: @"objc_socket.emit(%@);", [arguments componentsJoinedByString: @", "]]];
});

NSString* script = [NSString stringWithFormat: @"objc_socket.emit(%@);", [arguments componentsJoinedByString: @", "]];
[self performSelector:@selector(evaluateScript:) onThread:_thread withObject:[script copy] waitUntilDone:NO];
}

- (void)evaluateScript:(NSString *)script {
[self.javascriptContext evaluateScript:script];
}

- (void)close {
34 changes: 18 additions & 16 deletions SocketIO/Source/socket.io.js.h
Original file line number Diff line number Diff line change
@@ -16,23 +16,25 @@ static NSString *const socket_io_js = @"!function(e){if(\"object\"==typeof expor
/*!
* socket.io client constructor format.
*/
static NSString *socket_io_js_constructor(NSString *hostURL, BOOL reconnection, NSInteger attemptLimit, NSTimeInterval reconnectionDelay, NSTimeInterval reconnectionDelayMax, NSTimeInterval timeout) {
NSString *constructorFormat = @"io('%@', { \
'reconnection': %@, \
'reconnectionAttempts': %@, \
'reconnectionDelay': %d, \
'reconnectionDelayMax': %d, \
'timeout': %d \
});";
static NSString *socket_io_js_constructor(NSString *hostURL, BOOL reconnection, NSInteger attemptLimit, NSTimeInterval reconnectionDelay, NSTimeInterval reconnectionDelayMax, NSTimeInterval timeout, NSArray *transports) {
NSString *constructorFormat = @"io('%@', { \
'reconnection': %@, \
'reconnectionAttempts': %@, \
'reconnectionDelay': %d, \
'reconnectionDelayMax': %d, \
'timeout': %d, \
'transports': [ '%@' ] \
});";

return [NSString stringWithFormat: constructorFormat,
hostURL,
reconnection? @"true" : @"false",
(attemptLimit == -1)? @"Infinity" : @(attemptLimit),
(int)(reconnectionDelay * MSEC_PER_SEC),
(int)(reconnectionDelayMax * MSEC_PER_SEC),
(int)(timeout * MSEC_PER_SEC)
];
return [NSString stringWithFormat: constructorFormat,
hostURL,
reconnection? @"true" : @"false",
(attemptLimit == -1)? @"Infinity" : @(attemptLimit),
(int)(reconnectionDelay * MSEC_PER_SEC),
(int)(reconnectionDelayMax * MSEC_PER_SEC),
(int)(timeout * MSEC_PER_SEC),
[transports componentsJoinedByString:@"', '"]
];
}

/*!