Skip to content

Instantly share code, notes, and snippets.

@vargon
vargon / websocketclientrequest.txt
Created May 4, 2012 15:20
WebSocket Handshake Client Request
GET /some/resource HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:1111
Origin: file://
Sec-WebSocket-Key1: 3B5 A[B cQ190hn q 893 44:
Sec-WebSocket-Key2: 1 67 4 0 '4 1124&
Kj¥VO#Œ
@vargon
vargon / websocketserverresponse.txt
Created May 4, 2012 15:20
WebSocket Handshake Server Response
HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: file://
Sec-WebSocket-Location: ws://localhost:1111/some/resource
ü‘:{ZΖcöºùE2ıB
@vargon
vargon / samplewebsocketclient.js
Created May 4, 2012 14:42
Sample WebSocket client in JavaScript
var webSocketClient;
function connectToWebSocketServer() {
webSocketClient = new WebSocket('ws://localhost:1111/some/resource/');
webSocketClient.onopen = function() { alert('connection opened'); };
webSocketClient.onerror = function() { alert('connection error'); };
webSocketClient.onclose = function() { alert('connection closed'); };
webSocketClient.onmessage = function(msg) { alert('msg: '+msg.data); };
}
@vargon
vargon / myschemehandler.m
Created May 4, 2012 13:20
Handling "myscheme" in Objective C
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *scheme = [[[request URL] scheme] lowercaseString];
if([scheme isEqualToString:@"myscheme"]) {
// Pull the encoded JSON string out of the querystring
// Decode the string, convert JSON string into usable object
// Handle the message
// Return NO to prevent the webview from navigating
return NO;
}
@vargon
vargon / jsurlscheme.js
Created May 4, 2012 13:10
Sample JavaScript URL Scheme implementation
function sendMessageToNative(msg) {
document.location = 'myscheme://a.com/?m=' + encodeURIComponent(JSON.stringify(msg));
}