/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/WebSocketMain.as
ActionScript | 149 lines | 112 code | 23 blank | 14 comment | 8 complexity | 142540aaf7d3e93153b88a19fb96abff MD5 | raw file
1// Copyright: Hiroshi Ichikawa <http://gimite.net/en/> 2// License: New BSD License 3// Reference: http://dev.w3.org/html5/websockets/ 4// Reference: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 5 6package { 7 8import flash.display.Sprite; 9import flash.external.ExternalInterface; 10import flash.system.Security; 11import flash.utils.setTimeout; 12 13import mx.utils.URLUtil; 14 15/** 16 * Provides JavaScript API of WebSocket. 17 */ 18public class WebSocketMain extends Sprite implements IWebSocketLogger{ 19 20 private var callerUrl:String; 21 private var debug:Boolean = false; 22 private var manualPolicyFileLoaded:Boolean = false; 23 private var webSockets:Array = []; 24 private var eventQueue:Array = []; 25 26 public function WebSocketMain() { 27 ExternalInterface.addCallback("setCallerUrl", setCallerUrl); 28 ExternalInterface.addCallback("setDebug", setDebug); 29 ExternalInterface.addCallback("create", create); 30 ExternalInterface.addCallback("send", send); 31 ExternalInterface.addCallback("close", close); 32 ExternalInterface.addCallback("loadManualPolicyFile", loadManualPolicyFile); 33 ExternalInterface.addCallback("receiveEvents", receiveEvents); 34 ExternalInterface.call("WebSocket.__onFlashInitialized"); 35 } 36 37 public function setCallerUrl(url:String):void { 38 callerUrl = url; 39 } 40 41 public function setDebug(val:Boolean):void { 42 debug = val; 43 } 44 45 private function loadDefaultPolicyFile(wsUrl:String):void { 46 var policyUrl:String = "xmlsocket://" + URLUtil.getServerName(wsUrl) + ":843"; 47 log("policy file: " + policyUrl); 48 Security.loadPolicyFile(policyUrl); 49 } 50 51 public function loadManualPolicyFile(policyUrl:String):void { 52 log("policy file: " + policyUrl); 53 Security.loadPolicyFile(policyUrl); 54 manualPolicyFileLoaded = true; 55 } 56 57 public function log(message:String):void { 58 if (debug) { 59 ExternalInterface.call("WebSocket.__log", encodeURIComponent("[WebSocket] " + message)); 60 } 61 } 62 63 public function error(message:String):void { 64 ExternalInterface.call("WebSocket.__error", encodeURIComponent("[WebSocket] " + message)); 65 } 66 67 private function parseEvent(event:WebSocketEvent):Object { 68 var webSocket:WebSocket = event.target as WebSocket; 69 var eventObj:Object = {}; 70 eventObj.type = event.type; 71 eventObj.webSocketId = webSocket.getId(); 72 eventObj.readyState = webSocket.getReadyState(); 73 if (event.message !== null) { 74 eventObj.message = event.message; 75 } 76 return eventObj; 77 } 78 79 public function create( 80 webSocketId:int, 81 url:String, protocol:String, 82 proxyHost:String = null, proxyPort:int = 0, 83 headers:String = null):void { 84 if (!manualPolicyFileLoaded) { 85 loadDefaultPolicyFile(url); 86 } 87 var newSocket:WebSocket = new WebSocket( 88 webSocketId, url, protocol, getOrigin(), proxyHost, proxyPort, 89 getCookie(url), headers, this); 90 newSocket.addEventListener("open", onSocketEvent); 91 newSocket.addEventListener("close", onSocketEvent); 92 newSocket.addEventListener("error", onSocketEvent); 93 newSocket.addEventListener("message", onSocketEvent); 94 webSockets[webSocketId] = newSocket; 95 } 96 97 public function send(webSocketId:int, encData:String):int { 98 var webSocket:WebSocket = webSockets[webSocketId]; 99 return webSocket.send(encData); 100 } 101 102 public function close(webSocketId:int):void { 103 var webSocket:WebSocket = webSockets[webSocketId]; 104 webSocket.close(); 105 } 106 107 public function receiveEvents():Object { 108 var result:Object = eventQueue; 109 eventQueue = []; 110 return result; 111 } 112 113 private function getOrigin():String { 114 return (URLUtil.getProtocol(this.callerUrl) + "://" + 115 URLUtil.getServerNameWithPort(this.callerUrl)).toLowerCase(); 116 } 117 118 private function getCookie(url:String):String { 119 if (URLUtil.getServerName(url).toLowerCase() == 120 URLUtil.getServerName(this.callerUrl).toLowerCase()) { 121 return ExternalInterface.call("function(){return document.cookie}"); 122 } else { 123 return ""; 124 } 125 } 126 127 /** 128 * Socket event handler. 129 */ 130 public function onSocketEvent(event:WebSocketEvent):void { 131 var eventObj:Object = parseEvent(event); 132 eventQueue.push(eventObj); 133 processEvents(); 134 } 135 136 /** 137 * Process our event queue. If javascript is unresponsive, set 138 * a timeout and try again. 139 */ 140 public function processEvents():void { 141 if (eventQueue.length == 0) return; 142 if (!ExternalInterface.call("WebSocket.__onFlashEvent")) { 143 setTimeout(processEvents, 500); 144 } 145 } 146 147} 148 149}