Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

WebSocket client for Cocoa

tree: 03b450799b

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 PKWebSocket.xcodeproj
Octocat-spinner-32 PKWebSocket
Octocat-spinner-32 PKWebSocketTests
Octocat-spinner-32 Submodules
Octocat-spinner-32 Vendor
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .gitmodules
Octocat-spinner-32 README.md
README.md

PKWebSocket / WebSocket client for Cocoa

TODO

  • Propper socket status handling
  • Implement propper framing for the protocols
  • Implement RFC6455 (Handshake works framing doesn't)
  • Error reporting sucks
  • Documentation
  • Implement closing handshakes
  • Test with Socket.IO & Node.js
  • Test on mac

Compatiblity

Protocols

  • Hixie 75 (Full)
  • Hixie 76 (Full)
  • RFC6455 (WIP)

Servers (tested)

  • em-websocket
  • ws://echo.websocket.org

Usage

#import "PKWebSocket.h"
#import "PKWebSocketHandler-Hixie76.h"

NSURL *url = @"ws://echo.websocket.org";
PKWebSocketHandlerHixie76 *handler = [[PKWebSocketHandlerHixie76 alloc] init];
PKWebSocket *ws = [[PKWebSocket alloc] initWithURLString:url
                                                 handler:handler
                                                delegate:self];
NSError *error;
if ([ws openWithError:&error]) {
  [ws send:@"string to send"];
  [ws close];
}

Delegate methods

On the delegate WebSocket call following methods if you opt for them:

- (void)webSocket:(PKWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocketDidOpen:(PKWebSocket *)webSocket;
- (void)webSocketDidClose:(PKWebSocket *)webSocket;
- (void)webSocket:(PKWebSocket *)webSocket didReceiveMessage:(NSString *)message;
- (void)webSocketDidSendMessage:(PKWebSocket *)webSocket;
- (void)webSocketDidSecure:(PKWebSocket *)webSocket;

Using secure WebSocket

NSURL *url = [NSURL URLWithString:@"wss://echo.websocket.org"];
NSMutableDictionary *tlsSettings = [NSMutableDictionary dictionaryWithCapacity:4];
[tlsSettings setObject:url.host forKey:(NSString *)kCFStreamSSLPeerName];

// Allow expired certificates
[tlsSettings setObject:[NSNumber numberWithBool:YES]
                forKey:(NSString *)kCFStreamSSLAllowsExpiredCertificates];

// Allow self-signed certificates
[tlsSettings setObject:[NSNumber numberWithBool:YES]
                forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];

// In fact, don't even validate the certificate chain
[tlsSettings setObject:[NSNumber numberWithBool:NO]
                forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];

NSError *error;
PKWebSocket *ws = [[PKWebSocket alloc] initWithURLString:[url absoluteString]
                                     handler:_handler
                                    delegate:self];
ws.tlsSettings = tlsSettings;
if ([ws openWithError:&error]) {
...
}

Requirements & Dependencies & Submodules

Get code & Contribute

TOWRITE

Code

If you have GIT 1.7+ ...

git clone git@github.com:pk/PKWebSocket.git --recursive

or, earlier...

git clone git@github.com:pk/PKWebSocket.git
cd PKWebSocket
git submodule init
git submodule update

Test suite

TOWRITE

Adding to your project

TOWRITE

Based on

Something went wrong with that request. Please try again.