WebSocket RFC 6455 client library for iOS and OSX.
Jetfire is a conforming WebSocket (RFC 6455) client library in Objective-C for iOS and OSX.
Jetfire also has a Swift counter part here: Starscream
First thing is to import the header file. See the Installation instructions on how to add Jetfire to your project.
#import "JFRWebSocket.h"
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.
self.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:8080"] protocols:@[@"chat",@"superchat"]];
self.socket.delegate = self;
[self.socket connect];
After you are connected, there are some delegate methods that we need to implement.
-(void)websocketDidConnect:(JFRWebSocket*)socket {
NSLog(@"websocket is connected");
}
-(void)websocketDidDisconnect:(JFRWebSocket*)socket error:(NSError*)error {
NSLog(@"websocket is disconnected: %@",[error localizedDescription]);
}
-(void)websocket:(JFRWebSocket*)socket didReceiveMessage:(NSString*)string {
NSLog(@"got some text: %@",string);
}
-(void)websocket:(JFRWebSocket*)socket didReceiveData:(NSData*)data {
NSLog(@"got some binary data: %d",data.length);
}
Or you can use blocks.
self.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:8080"] protocols:@[@"chat",@"superchat"]];
//websocketDidConnect
socket.onConnect = ^{
println("websocket is connected")
};
//websocketDidDisconnect
socket.onDisconnect = ^(NSError *error) {
NSLog(@"websocket is disconnected: %@",[error localizedDescription]);
};
//websocketDidReceiveMessage
socket.onText = ^(NSString *text) {
NSLog(@"got some text: %@",string);
};
//websocketDidReceiveData
socket.onData = ^(NSData *data) {
NSLog(@"got some binary data: %d",data.length);
};
//you could do onPong as well.
[socket connect];
The delegate methods give you a simple way to handle data from the server, but how do you send data?
[self.socket writeData:[NSData data]]; // write some NSData over the socket!
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!
[self.socket disconnect];
Returns if the socket is connected or not.
if(self.socket.isConnected) {
// do cool stuff.
}
You can also override the default websocket headers with your own custom ones like so:
[self.socket setHeader:@"Sec-WebSocket-Protocol" forKey:@"someother protocols"];
[self.socket setHeader:@"Sec-WebSocket-Version" forKey:@"14"];
[self.socket setHeader:@"My-Awesome-Header" forKey:@"Everything is Awesome!"];
If you need to specify a protocol, simple add it to the init:
//chat and superchat are the example protocols here
self.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:8080"] protocols:@[@"chat",@"superchat"]];
self.socket.delegate = self;
[self.socket connect];
There are a couple of other properties that modify the stream:
//set this if you are planning on using the socket in a VOIP background setting (using the background VOIP service).
self.socket.voipEnabled = YES;
//set this you want to ignore SSL cert validation, so a self signed SSL certificate can be used.
self.socket.selfSignedSSL = YES;
SSL Pinning is also supported in Jetfire.
self.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:8080"] protocols:@[@"chat",@"superchat"]];
NSData *data = ... //load your certificate from disk
so
self.socket.security = [[JFRSecurity alloc] initWithCerts:@[[[JFRSSLCert alloc] initWithData:data]] publicKeys:YES];
//self.socket.security = [[JFRSecurity alloc] initUsingPublicKeys:YES]; //uses the .cer files in your app's bundle
You load either a NSData
blob of your certificate or you can use a SecKeyRef
if you have a public key you want to use. The usePublicKeys
bool is whether to use the certificates for validation or the public keys. The public keys will be extracted from the certificates automatically if usePublicKeys
is choosen.
A custom queue can be specified when delegate methods are called. By default dispatch_get_main_queue
is used, thus making all delegate methods calls run on the main thread. It is important to note that all WebSocket processing is done on a background thread, only the delegate method calls are changed when modifying the queue. The actual processing is always on a background thread and will not pause your app.
//create a custom queue
self.socket.queue = dispatch_queue_create("com.vluxe.jetfire.myapp", nil);
Check out the SimpleTest project in the examples directory to see how to setup a simple connection to a WebSocket server.
The recommended approach for installing Jetfire is via the CocoaPods package manager (like most libraries).
Jetfire requires at least iOS 5/OSX 10.7 or above.
Jetfire is license under the Apache License.