Giter Site home page Giter Site logo

socket.io-objc's Introduction

Socket.IO / Objective C Library

Interface to communicate between Objective C and Socket.IO with the help of websockets or Long-Polling. Originally based on fpotter's socketio-cocoa it uses other libraries/classes like

  • SocketRocket Look here for further instructions how to use/install SocketRocket.

JSON serialization can be provided by SBJson (json-framework), JSONKit or by Foundation in OS X 10.7/iOS 5.0. These are selected at runtime and introduce no source-level dependencies.

Requirements

As of version 0.4, this library requires at least OS X 10.7 or iOS 5.0.

Non-ARC version

If you're old school - there's still the non-ARC version for you. This version (the non-ARC one) is out-of-date and won't be maintained any further (at least not by me).

Usage

The easiest way to connect to your Socket.IO / node.js server is

SocketIO *socketIO = [[SocketIO alloc] initWithDelegate:self];
[socketIO connectToHost:@"localhost" onPort:3000];

If required, additional parameters can be included in the handshake by adding an NSDictionary to the withParams option:

[socketIO connectToHost:@"localhost"
                 onPort:3000
             withParams:[NSDictionary dictionaryWithObjectsAndKeys:@"1234", @"auth_token", nil]
];

A namespace can also be defined in the connection details:

[socketIO connectToHost:@"localhost" onPort:3000 withParams:nil withNamespace:@"/users"];

There are different methods to send data to the server

- (void) sendMessage:(NSString *)data;
- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function;
- (void) sendJSON:(NSDictionary *)data;
- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function;
- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data;
- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data andAcknowledge:(SocketIOCallback)function;

So you could send a normal Message like this

[socketIO sendMessage:@"hello world"];

or an Event (including some data) like this

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"test1" forKey:@"key1"];
[dict setObject:@"test2" forKey:@"key2"];

[socketIO sendEvent:@"welcome" withData:dict];

If you want the server to acknowledge your Message/Event you would also pass a SocketIOCallback block

SocketIOCallback cb = ^(id argsData) {
    NSDictionary *response = argsData;
    // do something with response
};
[socketIO sendEvent:@"welcomeAck" withData:dict andAcknowledge:cb];

All delegate methods are optional - you could implement the following

- (void) socketIODidConnect:(SocketIO *)socket;
- (void) socketIODidDisconnect:(SocketIO *)socket; // deprecated
- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error;
- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket didReceiveJSON:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket didSendMessage:(SocketIOPacket *)packet;
- (void) socketIO:(SocketIO *)socket onError:(NSError *)error;

These two callbacks are deprecated - please don't use them anymore - they will be removed in upcoming releases:

- (void) socketIOHandshakeFailed:(SocketIO *)socket;
- (void) socketIO:(SocketIO *)socket failedToConnectWithError:(NSError *)error;

To process an incoming Message just

- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet
{
    NSLog(@"didReceiveMessage() >>> data: %@", packet.data);
}

Authors

Initial project by Philipp Kyeck http://beta-interactive.de.
Namespace support by Sam Lown [email protected] at Cabify.
SSL support by kayleg https://github.com/kayleg.
Different Socket Libraries + Error Handling by taiyangc https://github.com/taiyangc.

License

(The MIT License)

Copyright (c) 2011-13 Philipp Kyeck http://beta-interactive.de

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

socket.io-objc's People

Contributors

antonholmquist avatar beepscore avatar bnadim avatar bridger avatar danlite avatar insanehunter avatar kayleg avatar mennopruijssers avatar michaelscaria avatar paiv avatar pkyeck avatar progeddog avatar psineur avatar revcbh avatar samlown avatar spesholized avatar taiyangc avatar yannickl avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.