WebSocketMain.as 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-31
  5. package {
  6. import flash.display.*;
  7. import flash.events.*;
  8. import flash.external.*;
  9. import flash.net.*;
  10. import flash.system.*;
  11. import flash.utils.*;
  12. import mx.core.*;
  13. import mx.controls.*;
  14. import mx.events.*;
  15. import mx.utils.*;
  16. import bridge.FABridge;
  17. public class WebSocketMain extends Sprite {
  18. private var policyLoaded:Boolean = false;
  19. private var callerUrl:String;
  20. private var debug:Boolean = false;
  21. public function WebSocketMain() {
  22. // This is to avoid "You are trying to call recursively into the Flash Player ..."
  23. // error which (I heard) happens when you pass bunch of messages.
  24. // This workaround was written here:
  25. // http://www.themorphicgroup.com/blog/2009/02/14/fabridge-error-you-are-trying-to-call-recursively-into-the-flash-player-which-is-not-allowed/
  26. FABridge.EventsToCallLater["flash.events::Event"] = "true";
  27. FABridge.EventsToCallLater["WebSocketMessageEvent"] = "true";
  28. FABridge.EventsToCallLater["WebSocketStateEvent"] = "true";
  29. var fab:FABridge = new FABridge();
  30. fab.rootObject = this;
  31. //log("Flash initialized");
  32. }
  33. public function setCallerUrl(url:String):void {
  34. callerUrl = url;
  35. }
  36. public function setDebug(val:Boolean):void {
  37. debug = val;
  38. }
  39. public function create(
  40. url:String, protocol:String,
  41. proxyHost:String = null, proxyPort:int = 0,
  42. headers:String = null):WebSocket {
  43. loadPolicyFile(null);
  44. return new WebSocket(this, url, protocol, proxyHost, proxyPort, headers);
  45. }
  46. public function getOrigin():String {
  47. return (URLUtil.getProtocol(this.callerUrl) + "://" +
  48. URLUtil.getServerNameWithPort(this.callerUrl)).toLowerCase();
  49. }
  50. public function getCallerHost():String {
  51. return URLUtil.getServerName(this.callerUrl);
  52. }
  53. public function loadPolicyFile(url:String):void {
  54. if (policyLoaded && !url) return;
  55. if (!url) {
  56. url = "xmlsocket://" + URLUtil.getServerName(this.callerUrl) + ":843";
  57. }
  58. log("policy file: " + url);
  59. Security.loadPolicyFile(url);
  60. policyLoaded = true;
  61. }
  62. public function log(message:String):void {
  63. if (debug) {
  64. ExternalInterface.call("webSocketLog", encodeURIComponent("[WebSocket] " + message));
  65. }
  66. }
  67. public function error(message:String):void {
  68. ExternalInterface.call("webSocketError", encodeURIComponent("[WebSocket] " + message));
  69. }
  70. public function fatal(message:String):void {
  71. ExternalInterface.call("webSocketError", encodeURIComponent("[WebSocket] " + message));
  72. throw message;
  73. }
  74. }
  75. }