Giter Club home page Giter Club logo

socketiococoa's Introduction

SocketIOCocoa

The socket 1.0 client in Swift. It includes xhr polling and websocket transports.

This is a port from socketio.client, which means the code structure are quite similar.

Motivation & Contribute

For our startup project, we running socketio1.0 in our server, and needs an iOS client. I tried my best to find the iOS client, but with no luck.

I am still dogfooding it, any issue report, pull request are welcomed.

Installation

Untill CocoaPod's swift library support story completes, it is much easier just copy all files and put them in your project. The framework way is troublesome and I won't bother to mark them down here.

Files needs to copied:

All swift files under Vender

All swift files under SocketIOCocoa

Usage

Swift

Create a client

var client = SocketIOClient(uri: uri, reconnect: true, timeout: 30)
client.open()

Create a socket

var socket = client.socket("namespace")
// Set a delegate on socket

The SocketIOSocketDelegate

@objc public protocol SocketIOSocketDelegate {
    // Called when the socket received a low level packet
    optional func socketOnPacket(socket: SocketIOSocket, packet: SocketIOPacket)
    
    // Called when the socket received an event
    func socketOnEvent(socket: SocketIOSocket, event: String, data: AnyObject?)
    
    // Called when the socket is open
    func socketOnOpen(socket: SocketIOSocket)
    
    // Called when the socket is on error
    func socketOnError(socket: SocketIOSocket, error: String, description: String?)
}

Send message to server, when a callback provides, it will require an ACK or BinaryACK from server, then the callback will be called.

socket.event("message", data: [1,2,3]) { (packet) -> Void in
    expectation.fulfill()
}
self.waitForExpectationsWithTimeout(300, handler: nil)

ObjC

Create a client

self.client = [[SocketIOClient alloc] initWithUri: @"http://<server ip>/socket.io/" transports:@[@"polling", @"websocket"] autoConnect:YES reconnect:YES reconnectAttempts:0 reconnectDelay:5 reconnectDelayMax:30 timeout:30];
[self.client open];

Create a socket

self.apiSocket = [self.client socket:@"namespace"];
self.apiSocket.delegate = self;

The SocketIOSocketDelegate

Socket received event with data

- (void)socketOnEvent:(SocketIOSocket *)socket event:(NSString *)event data:(id)data

Socket on open

- (void)socketOnOpen:(SocketIOSocket *)socket{

Socket on error, description is a detailed message for the error

- (void)socketOnError:(SocketIOSocket *)socket error:(NSString *)error description:(NSString *)description

Socket on packet, lower level packet, used to tracking socket activity.

- (void)socketOnPacket:(SocketIOSocket *)socket packet:(SocketIOPacket *)packet;

Under the hood

Alamofire for xhr polling, starscream for websocket. GCD for async tasks.

TODO

Performance enhaucement

LICENSE

MIT

CREDIT

socketiococoa's People

Contributors

shuoli84 avatar thebehera avatar

Watchers

James Cloos 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.