Git Product home page Git Product logo

starscream's Introduction

starscream

Starscream is a conforming WebSocket (RFC 6455) client library in Swift for iOS and OSX.

It's Objective-C counter part can be found here: Jetfire

Features

  • Conforms to all of the base Autobahn test suite.
  • Nonblocking. Everything happens in the background, thanks to GCD.
  • Simple delegate pattern design.
  • TLS/WSS support.
  • Simple concise codebase at just a few hundred LOC.

Example

First thing is to import the framework. See the Installation instructions on how to add the framework to your project.

//iOS
import Starscream
//OS X
import StarscreamOSX

Once imported, you can open a connection to your WebSocket server. Note that socket is probably best as a property, so your delegate can stick around.

var socket = WebSocket(url: NSURL(scheme: "ws", host: "localhost:8080", path: "/"))
socket.delegate = self
socket.connect()

After you are connected, there are some delegate methods that we need to implement.

websocketDidConnect

websocketDidConnect is called as soon as the client connects to the server.

func websocketDidConnect() {
    println("websocket is connected")
}

websocketDidDisconnect

websocketDidDisconnect is called as soon as the client is disconnected from the server.

func websocketDidDisconnect(error: NSError?) {
	println("websocket is disconnected: \(error!.localizedDescription)")
}

websocketDidWriteError

websocketDidWriteError is called when the client gets an error on websocket connection.

func websocketDidWriteError(error: NSError?) {
    println("wez got an error from the websocket: \(error!.localizedDescription)")
}

websocketDidReceiveMessage

websocketDidReceiveMessage is called when the client gets a text frame from the connection.

func websocketDidReceiveMessage(text: String) {
	println("got some text: \(text)")
}

websocketDidReceiveData

websocketDidReceiveData is called when the client gets a binary frame from the connection.

func websocketDidReceiveData(data: NSData) {
	println("got some data: \(data.length)")
}

The delegate methods give you a simple way to handle data from the server, but how do you send data?

writeData

The writeData method gives you a simple way to send NSData (binary) data to the server.

self.socket.writeData(data) //write some NSData over the socket!

writeString

The writeString method is the same as writeData, but sends text/string.

self.socket.writeString("Hi Server!") //example on how to write text over the socket!

disconnect

The disconnect method does what you would expect and closes the socket.

self.socket.disconnect()

isConnected

Returns if the socket is connected or not.

if self.socket.isConnected {
  // do cool stuff.
}

Custom Headers

You can also override the default websocket headers with your own custom ones like so:

socket.headers["Sec-WebSocket-Protocol"] = "someother protocols"
socket.headers["Sec-WebSocket-Version"] = "14"
socket.headers["My-Awesome-Header"] = "Everything is Awesome!"

Protocols

If you need to specify a protocol, simple add it to the init:

//chat and superchat are the example protocols here
var socket = WebSocket(url: NSURL(scheme: "ws", host: "localhost:8080", path: "/"), protocols: ["chat","superchat"])
socket.delegate = self
socket.connect()

Self Signed SSL and VOIP

There are a couple of other properties that modify the stream:

var socket = WebSocket(url: NSURL(scheme: "ws", host: "localhost:8080", path: "/"), protocols: ["chat","superchat"])

//set this if you are planning on using the socket in a VOIP background setting (using the background VOIP service).
socket.voipEnabled = true

//set this you want to ignore SSL cert validation, so a self signed SSL certificate can be used.
socket.selfSignedSSL = true

Example Project

Check out the SimpleTest project in the examples directory to see how to setup a simple connection to a WebSocket server.

Requirements

Starscream requires at least iOS 7/OSX 10.10 or above.

Installation

At this time, Cocoapods support for Swift frameworks is supported in a pre-release.

To use Starscream in your project add the following 'Podfile' to your project

source 'https://github.com/CocoaPods/Specs.git'

xcodeproj 'YourProjectName.xcodeproj'
platform :ios, '8.0'

pod 'Starscream', :git => "https://github.com/daltoniam/starscream.git", :tag => "0.9.1"

target 'YourProjectNameTests' do
    pod 'Starscream', :git => "https://github.com/daltoniam/starscream.git", :tag => "0.9.1"
end

Then run:

pod install

Updating the Cocoapod

You can validate Starscream.podspec using:

pod spec lint Starscream.podspec

This should be tested with a sample project before releasing it. This can be done by adding the following line to a Podfile:

pod 'Starscream', :git => 'https://github.com/username/starscream.git'

Then run:

pod install

If all goes well you are ready to release. First, create a tag and push:

git tag 'version'
git push --tags

Once the tag is available you can send the library to the Specs repo. For this you'll have to follow the instructions in Getting Setup with Trunk.

pod trunk push Starscream.podspec

Carthage

Check out the Carthage docs on how to add a install. The Starscream framework is already setup with shared schemes.

Carthage Install

Rogue

First see the installation docs for how to install Rogue.

To install Starscream run the command below in the directory you created the rogue file.

rogue add https://github.com/daltoniam/starscream

Next open the libs folder and add the Starscream.xcodeproj to your Xcode project. Once that is complete, in your "Build Phases" add the Starscream.framework to your "Link Binary with Libraries" phase. Make sure to add the libs folder to your .gitignore file.

Other

Simply grab the framework (either via git submodule or another package manager).

Add the Starscream.xcodeproj to your Xcode project. Once that is complete, in your "Build Phases" add the Starscream.framework to your "Link Binary with Libraries" phase.

Add Copy Frameworks Phase

If you are running this in an OSX app or on a physical iOS device you will need to make sure you add the Starscream.framework or StarscreamOSX.framework to be included in your app bundle. To do this, in Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar. In the tab bar at the top of that window, open the "Build Phases" panel. Expand the "Link Binary with Libraries" group, and add Starscream.framework or StarscreamOSX.framework depending on if you are building an iOS or OSX app. Click on the + button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and add Starscream.framework or StarscreamOSX.framework respectively.

TODOs

  • Complete Docs
  • Add Unit Tests

License

Starscream is licensed under the Apache v2 License.

Contact

Dalton Cherry

Austin Cherry

starscream's People

Contributors

daltoniam avatar acmacalister avatar mqshen avatar andrewgene avatar danbev avatar alanzeino avatar interstateone 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.