/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/WebSocketMain.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · 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. package {
  6. import flash.display.Sprite;
  7. import flash.external.ExternalInterface;
  8. import flash.system.Security;
  9. import flash.utils.setTimeout;
  10. import mx.utils.URLUtil;
  11. /**
  12. * Provides JavaScript API of WebSocket.
  13. */
  14. public class WebSocketMain extends Sprite implements IWebSocketLogger{
  15. private var callerUrl:String;
  16. private var debug:Boolean = false;
  17. private var manualPolicyFileLoaded:Boolean = false;
  18. private var webSockets:Array = [];
  19. private var eventQueue:Array = [];
  20. public function WebSocketMain() {
  21. ExternalInterface.addCallback("setCallerUrl", setCallerUrl);
  22. ExternalInterface.addCallback("setDebug", setDebug);
  23. ExternalInterface.addCallback("create", create);
  24. ExternalInterface.addCallback("send", send);
  25. ExternalInterface.addCallback("close", close);
  26. ExternalInterface.addCallback("loadManualPolicyFile", loadManualPolicyFile);
  27. ExternalInterface.addCallback("receiveEvents", receiveEvents);
  28. ExternalInterface.call("WebSocket.__onFlashInitialized");
  29. }
  30. public function setCallerUrl(url:String):void {
  31. callerUrl = url;
  32. }
  33. public function setDebug(val:Boolean):void {
  34. debug = val;
  35. }
  36. private function loadDefaultPolicyFile(wsUrl:String):void {
  37. var policyUrl:String = "xmlsocket://" + URLUtil.getServerName(wsUrl) + ":843";
  38. log("policy file: " + policyUrl);
  39. Security.loadPolicyFile(policyUrl);
  40. }
  41. public function loadManualPolicyFile(policyUrl:String):void {
  42. log("policy file: " + policyUrl);
  43. Security.loadPolicyFile(policyUrl);
  44. manualPolicyFileLoaded = true;
  45. }
  46. public function log(message:String):void {
  47. if (debug) {
  48. ExternalInterface.call("WebSocket.__log", encodeURIComponent("[WebSocket] " + message));
  49. }
  50. }
  51. public function error(message:String):void {
  52. ExternalInterface.call("WebSocket.__error", encodeURIComponent("[WebSocket] " + message));
  53. }
  54. private function parseEvent(event:WebSocketEvent):Object {
  55. var webSocket:WebSocket = event.target as WebSocket;
  56. var eventObj:Object = {};
  57. eventObj.type = event.type;
  58. eventObj.webSocketId = webSocket.getId();
  59. eventObj.readyState = webSocket.getReadyState();
  60. if (event.message !== null) {
  61. eventObj.message = event.message;
  62. }
  63. return eventObj;
  64. }
  65. public function create(
  66. webSocketId:int,
  67. url:String, protocol:String,
  68. proxyHost:String = null, proxyPort:int = 0,
  69. headers:String = null):void {
  70. if (!manualPolicyFileLoaded) {
  71. loadDefaultPolicyFile(url);
  72. }
  73. var newSocket:WebSocket = new WebSocket(
  74. webSocketId, url, protocol, getOrigin(), proxyHost, proxyPort,
  75. getCookie(url), headers, this);
  76. newSocket.addEventListener("open", onSocketEvent);
  77. newSocket.addEventListener("close", onSocketEvent);
  78. newSocket.addEventListener("error", onSocketEvent);
  79. newSocket.addEventListener("message", onSocketEvent);
  80. webSockets[webSocketId] = newSocket;
  81. }
  82. public function send(webSocketId:int, encData:String):int {
  83. var webSocket:WebSocket = webSockets[webSocketId];
  84. return webSocket.send(encData);
  85. }
  86. public function close(webSocketId:int):void {
  87. var webSocket:WebSocket = webSockets[webSocketId];
  88. webSocket.close();
  89. }
  90. public function receiveEvents():Object {
  91. var result:Object = eventQueue;
  92. eventQueue = [];
  93. return result;
  94. }
  95. private function getOrigin():String {
  96. return (URLUtil.getProtocol(this.callerUrl) + "://" +
  97. URLUtil.getServerNameWithPort(this.callerUrl)).toLowerCase();
  98. }
  99. private function getCookie(url:String):String {
  100. if (URLUtil.getServerName(url).toLowerCase() ==
  101. URLUtil.getServerName(this.callerUrl).toLowerCase()) {
  102. return ExternalInterface.call("function(){return document.cookie}");
  103. } else {
  104. return "";
  105. }
  106. }
  107. /**
  108. * Socket event handler.
  109. */
  110. public function onSocketEvent(event:WebSocketEvent):void {
  111. var eventObj:Object = parseEvent(event);
  112. eventQueue.push(eventObj);
  113. processEvents();
  114. }
  115. /**
  116. * Process our event queue. If javascript is unresponsive, set
  117. * a timeout and try again.
  118. */
  119. public function processEvents():void {
  120. if (eventQueue.length == 0) return;
  121. if (!ExternalInterface.call("WebSocket.__onFlashEvent")) {
  122. setTimeout(processEvents, 500);
  123. }
  124. }
  125. }
  126. }