WebSocket RFC 6455 client library for iOS and OSX.
Jetfire conforms to all of the base Autobahn test suite. The library is very simple and only a few hundred lines of code, but fully featured. It runs completely on a background thread, so processing will never block the main thread.
jetfire also has a Swift counter part here: starscream
Open a connection to your websocket server. self.socket is a property, so it can stick around.
self.socket = [[JFWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:8080"]];
self.socket.delegate = self;
[self.socket connect];
Now for the delegate methods.
/////////////////////////////////////////////////////////////////////////////
-(void)websocketDidConnect:(JFWebSocket*)socket
{
NSLog(@"websocket is connected");
}
/////////////////////////////////////////////////////////////////////////////
-(void)websocketDidDisconnect:(JFWebSocket*)socket error:(NSError*)error
{
NSLog(@"websocket is disconnected: %@",[error localizedDescription]);
}
/////////////////////////////////////////////////////////////////////////////
-(void)websocket:(JFWebSocket*)socket didReceiveMessage:(NSString*)string
{
NSLog(@"got some text: %@",string);
dispatch_async(dispatch_get_main_queue(),^{
//do some UI work
});
}
/////////////////////////////////////////////////////////////////////////////
-(void)websocket:(JFWebSocket*)socket didReceiveData:(NSData*)data
{
NSLog(@"got some binary data: %d",data.length);
}
How to send a message.
-(void)sendMessage
{
[self.socket writeString:@"hello server!"];
//[self.socket writeData:[NSData data]]; you can also write binary data like so
}
Disconnect.
-(void)disconnect
{
[self.socket disconnect];
}
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.