Native iOS client for SocketCluster
The project is in beta. If you have issues with this client, there are others to choose from: https://github.com/socketcluster/client-drivers#list-of-clients
Podfile
platform :ios, '8.0'
pod 'SocketClusteriOS', '0.3'
Run pod install
, you are all set.
Notice: Do not use use_frameworks!
in Podfile, it will put the bundle in frameworks folder to cause incorrect path issue. If you need to use this, please consider to send a pull request.
#import <SocketClusteriOS/SocketCluster.h>
libSocketClusteriOS.a
, SocketCluster.h
and SocketClusterDelegate.h
and SocketClusteriOSBundle.bundle
to your project.#import <SocketClusteriOS/SocketCluster.h>
<SocketClusterDelegate>
, eg. @interface ViewController : UIViewController <SocketClusterDelegate>
SocketCluster *sc = [[SocketCluster alloc] init];
sc.delegate = self; //Self
[socketCluster connectToHost:@"127.0.0.1" onPort:8000 securly:NO];
[socketCluster disconnect];
[socketCluster getState];
Make sure you have implemented protocol <SocketClusterDelegate>
- (void)socketClusterReceivedEvent:(NSString *)eventName WithData:(NSDictionary *)data;
{
if ([@"rand" isEqualToString:eventName]) {
NSArray *positiveFaces = @[@";p", @":D", @":)", @":3", @";)"];
NSNumber *index = data[@"rand"];
NSString *face = [positiveFaces objectAtIndex:[index intValue]];
NSString *msg = [NSString stringWithFormat:@"rand event received: %@", face];
[self logMessage:msg];
}
}
data
can be any format, but they should be serializable
[socketCluster emitEvent:@"eventName" WithData:data];
[socketCluster subscribeToChannel:@"channelName"];
[socketCluster unsubscribeFromChannel:@"channelName"];
data
can be in any format
[socketCluster publishToChannel:@"channelName" WithData:data];
watch
in JS client)Make sure you have implemented protocol <SocketClusterDelegate>
- (void)socketclusterChannelReceivedEvent:(NSString *)channel WithData:(NSDictionary *)data
{
if ([@"pong" isEqualToString:channel]) {
[self logMessage:[NSString stringWithFormat:@"Channel %@ received message %@", channel, data[@"data"]]];
}
}
NSArray * subscriptions = [socketCluster getSubscriptions];
NSArray * subscriptions = [socketCluster getSubscriptionsIncludingPending];
All these callback would only work if you have implemented protocol <SocketClusterDelegate>
- (void)socketClusterDidConnect
{
[self logMessage:@"Connected"];
}
- (void)socketClusterDidDisconnect
{
[self logMessage:@"Disconnected"];
}
- (void)socketClusterOnError
{
[self logMessage:@"Error"];
}