Giter Club home page Giter Club logo

swiftwebsocket's Introduction

# SwiftWebSocket

Conforming WebSocket (RFC 6455) client library for iOS and Mac OSX.

SwiftWebSocket passes all 521 of the Autobahn's fuzzing tests, including strict UTF-8, and message compression.

Important: Xcode 7.2 and Swift 2.0 Users
If you are developing in Xcode 7.2 or Swift 2.0 then please use the swift/2.0 branch.

Features

  • High performance.
  • 100% conforms to Autobahn Tests. Including base, limits, compression, etc. Test results.
  • TLS / WSS support. Self-signed certificate option.
  • The API is modeled after the Javascript API.
  • Reads compressed messages (permessage-deflate). RFC 7692
  • Send pings and receive pong events.
  • Strict UTF-8 processing.
  • binaryType property to choose between [UInt8] or NSData messages.
  • Zero asserts. All networking, stream, and protocol errors are routed through the error event.
  • iOS / Objective-C support.

Example

func echoTest(){
    var messageNum = 0
    let ws = WebSocket("wss://echo.websocket.org")
    let send : ()->() = {
        let msg = "\(++messageNum): \(NSDate().description)"
        print("send: \(msg)")
        ws.send(msg)
    }
    ws.event.open = {
        print("opened")
        send()
    }
    ws.event.close = { code, reason, clean in
        print("close")
    }
    ws.event.error = { error in
        print("error \(error)")
    }
    ws.event.message = { message in
        if let text = message as? String {
            print("recv: \(text)")
            if messageNum == 10 {
                ws.close()
            } else {
                send()
            }
        }
    }
}

Custom Headers

let request = NSMutableURLRequest(URL: NSURL(string:"ws://url")!)
request.addValue("AUTH_TOKEN", forHTTPHeaderField: "Authorization")
request.addValue("Value", forHTTPHeaderField: "X-Another-Header")
let ws = WebSocket(request: request)

Reuse and Delaying WebSocket Connections

v2.3.0+ makes available an optional open method. This will allow for a WebSocket object to be instantiated without an immediate connection to the server. It can also be used to reconnect to a server following the close event.

For example,

let ws = WebSocket()
ws.event.close = { _ in
    ws.open()                 // reopen the socket to the previous url
    ws.open("ws://otherurl")  // or, reopen the socket to a new url
}
ws.open("ws://url") // call with url

Compression

The compression flag may be used to request compressed messages from the server. If the server does not support or accept the request, then connection will continue as normal, but with uncompressed messages.

let ws = WebSocket("ws://url")
ws.compression.on = true

Self-signed SSL Certificate

let ws = WebSocket("ws://url")
ws.allowSelfSignedSSL = true

Network Services (VoIP, Video, Background, Voice)

// Allow socket to handle VoIP in the background.
ws.services = [.VoIP, .Background] 

##Installation (iOS and OS X)

Add the following to your Cartfile:

github "tidwall/SwiftWebSocket"

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

The import SwiftWebSocket directive is required in order to access SwiftWebSocket features.

Add the following to your Podfile:

use_frameworks!
pod 'SwiftWebSocket'

Then run pod install with CocoaPods 0.36 or newer.

The import SwiftWebSocket directive is required in order to access SwiftWebSocket features.

###Manually

Copy the SwiftWebSocket/WebSocket.swift file into your project.
You must also add the libz.dylib library. Project -> Target -> Build Phases -> Link Binary With Libraries

There is no need for import SwiftWebSocket when manually installing.

Contact

Josh Baker @tidwall

License

SwiftWebSocket source code is available under the MIT License.

swiftwebsocket's People

Contributors

tidwall avatar ricardopereira avatar abhisheksengar avatar danielrhodes avatar marcboeker avatar mlilback avatar

Watchers

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