Skip to content

matthewbga/starscream

 
 

Repository files navigation

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.

import Starscream

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, we some delegate methods 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()

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

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.

TODOs

  • Complete Docs
  • Add Unit Tests
  • Add Swallow Installation Docs

License

Starscream is licensed under the Apache v2 License.

Contact

Dalton Cherry

Austin Cherry

About

Websockets in swift for iOS and OSX

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published