Skip to content

Instantly share code, notes, and snippets.

@uchoaaa
Created September 23, 2010 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uchoaaa/593114 to your computer and use it in GitHub Desktop.
Save uchoaaa/593114 to your computer and use it in GitHub Desktop.
/*
First all, you need put the WebSocketMain classes in your classpath.
To do that you can just copy-and-paste from web-socket-js/flash-src (http://github.com/gimite/web-socket-js/tree/master/flash-src/) to your project or generate a SWC and import it.
To generate a SWC you can follow http://www.mikechambers.com/blog/2006/05/19/quick-example-using-compc-to-compile-swcs/
Now you can instantiate a WebSocket class in your Air app.
NOTE:
With the code above I can connect to Pusher and got initial messages (connection_established).
But, for that, I had to comment some line of codes (don't ask my why! :-):
- line 52 of WebSocketMain.as, the line calling loadPolicyFile(null);
- lines 75 and 79 of WebSocketMain.as. This lines call 'ExternalInterface.call()' wich generate a 'Error: Error #2067: The ExternalInterface is not available in this container.' message.
Well, now I need to figure out how can I send a message to subscribe in a channel.
Any help is apreciated.
Thanks
**************
*** UPDATE ***
**************
1) After connect, to subscribe in a channel, you have to send a JSON message like '"data":{"channel":"my-chanel"},"event":"pusher:subscribe"}'.
For that, I created this function:
private function subscribe():void {
var pusherData:Object = new Object();
pusherData.channel = 'my-channel';
var pusherEvent:Object = new Object();
pusherEvent.event = 'pusher:subscribe';
pusherEvent.data = pusherData;
var msg:String = JSON.encode(pusherEvent);
server.send(msg);
}
2) To get a human readable message, you have to decode the receive string, like this:
private function onMessage( event:WebSocketMessageEvent ):void {
//Example: {"event":"pusher:connection_established","data":"{\"socket_id\":\"1234\"}"}
var msg:String = decodeURIComponent(event.data);
var pusherEvent:Object = JSON.decode(msg);
var pusherData:Object = JSON.decode(pusherEvent.data);
trace('Event: ' +pusherEvent.event+ '| SocketId: '+ pusherData.socket_id)
}
3) I'm using http://github.com/mikechambers/as3corelib to encode/decode JSON.
4) Soon I'll make a nice library to wrap this things. ;)
5) Have fun!
*/
protected function initApp():void {
var url:String = "ws://ws.pusherapp.com:80/app/YourPusherKey";
var protocol:String = null; //I really don't know what this 'protocol' is
var main:WebSocketMain = new WebSocketMain();
main.setCallerUrl('http://localhost:3000');
server = main.create(url, protocol);
server.addEventListener(Event.OPEN, onOpen);
server.addEventListener(Event.CLOSE, onClose);
server.addEventListener("message", onMessage);
server.addEventListener("stateChange", onStateChange);
}
private function onOpen( event:Event ):void {
trace('open!');
}
private function onClose( event:Event ):void {
trace('close!');
}
private function onMessage( event:WebSocketMessageEvent ):void {
trace('Message: ' +event.data);
}
private function onStateChange( event:WebSocketStateEvent ):void {
trace('state changed: ' +event.readyState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment