Permalink
Browse files

Some cleanup.

Pulling messages from JavaScript instead of pushing from Flash, to keep message order.
Merging WebSocketController to WebSocket.
  • Loading branch information...
1 parent 0591d5d commit 978e31ce6ff15926391b616667c4e4370bbac800 @gimite committed Feb 27, 2011
View
BIN WebSocketMain.swf 100755 → 100644
Binary file not shown.
View
Binary file not shown.
View
@@ -24,16 +24,14 @@ import mx.core.*;
import mx.events.*;
import mx.utils.*;
-[Event(name="event", type="flash.events.Event")]
public class WebSocket extends EventDispatcher {
private static var CONNECTING:int = 0;
private static var OPEN:int = 1;
private static var CLOSING:int = 2;
private static var CLOSED:int = 3;
- private var socketId : Number;
-
+ private var id:int;
private var rawSocket:Socket;
private var tlsSocket:TLSSocket;
private var tlsConfig:TLSConfig;
@@ -54,11 +52,11 @@ public class WebSocket extends EventDispatcher {
private var expectedDigest:String;
public function WebSocket(
- main:WebSocketMain, webSocketId : Number, url:String, protocol:String,
+ main:WebSocketMain, id:int, url:String, protocol:String,
proxyHost:String = null, proxyPort:int = 0,
headers:String = null) {
this.main = main;
- this.socketId = webSocketId;
+ this.id = id;
initNoiseChars();
this.url = url;
var m:Array = url.match(/^(\w+):\/\/([^\/:]+)(:(\d+))?(\/.*)?$/);
@@ -106,19 +104,17 @@ public class WebSocket extends EventDispatcher {
}
/**
- * socketId accessor
- * @return This WebSocket's ID.
- */
- public function get webSocketId() : Number {
- return this.socketId;
+ * @return This WebSocket's ID.
+ */
+ public function getId():int {
+ return this.id;
}
/**
- * readyState accessor
- * @return this WebSocket's readyState.
- */
- public function get readOnly_readyState() : int {
- return this.readyState;
+ * @return this WebSocket's readyState.
+ */
+ public function getReadyState():int {
+ return this.readyState;
}
public function send(encData:String):int {
@@ -140,18 +136,18 @@ public class WebSocket extends EventDispatcher {
}
}
- public function close():void {
+ public function close(isError:Boolean = false):void {
main.log("close");
try {
- if (readyState == OPEN) {
+ if (readyState == OPEN && !isError) {
socket.writeByte(0xff);
socket.writeByte(0x00);
socket.flush();
}
socket.close();
} catch (ex:Error) { }
readyState = CLOSED;
- this.dispatchEvent(new WebSocketEvent(WebSocketEvent.CLOSE));
+ this.dispatchEvent(new WebSocketEvent(isError ? "error" : "close"));
}
private function onSocketConnect(event:Event):void {
@@ -198,7 +194,7 @@ public class WebSocket extends EventDispatcher {
private function onSocketClose(event:Event):void {
main.log("closed");
readyState = CLOSED;
- this.dispatchEvent(new WebSocketEvent(WebSocketEvent.CLOSE));
+ this.dispatchEvent(new WebSocketEvent("close"));
}
private function onSocketIoError(event:IOErrorEvent):void {
@@ -224,12 +220,9 @@ public class WebSocket extends EventDispatcher {
}
private function onError(message:String):void {
- var state:int = readyState;
- if (state == CLOSED) return;
+ if (readyState == CLOSED) return;
main.error(message);
-
- this.dispatchEvent(new WebSocketEvent(WebSocketEvent.ERROR, encodeURIComponent(message)));
- close();
+ close(readyState != CONNECTING);
}
private function onSocketData(event:ProgressEvent):void {
@@ -264,7 +257,7 @@ public class WebSocket extends EventDispatcher {
removeBufferBefore(pos + 1);
pos = -1;
readyState = OPEN;
- this.dispatchEvent(new WebSocketEvent(WebSocketEvent.OPEN));
+ this.dispatchEvent(new WebSocketEvent("open"));
}
} else {
if (buffer[pos] == 0xff && pos > 0) {
@@ -274,7 +267,7 @@ public class WebSocket extends EventDispatcher {
}
var data:String = readUTFBytes(buffer, 1, pos - 1);
main.log("received: " + data);
- this.dispatchEvent(new WebSocketEvent(WebSocketEvent.MESSAGE, encodeURIComponent(data)));
+ this.dispatchEvent(new WebSocketEvent("message", encodeURIComponent(data)));
removeBufferBefore(pos + 1);
pos = -1;
} else if (pos == 1 && buffer[0] == 0xff && buffer[1] == 0x00) { // closing
@@ -1,32 +1,33 @@
-package
-{
- import flash.events.Event;
-
- /**
- * This class represents a generic websocket event. It contains the standard "type"
- * parameter as well as a "message" parameter.
- */
- public class WebSocketEvent extends Event
- {
- public static const OPEN : String = "open";
- public static const CLOSE : String = "close";
- public static const MESSAGE : String = "message";
- public static const ERROR : String = "error";
-
- public var message : String;
-
- public function WebSocketEvent(type:String, inMessage:String=null, bubbles:Boolean=false, cancelable:Boolean=false)
- {
- super(type, bubbles, cancelable);
- this.message = inMessage;
- }
-
- public override function clone() : Event {
- return new WebSocketEvent(this.type, this.message, this.bubbles, this.cancelable);
- }
-
- public override function toString() : String {
- return "WebSocketEvent: " + this.type + ": " + this.message;
- }
- }
-}
+package {
+
+import flash.events.Event;
+
+/**
+ * This class represents a generic websocket event. It contains the standard "type"
+ * parameter as well as a "message" parameter.
+ */
+public class WebSocketEvent extends Event {
+
+ public static const OPEN:String = "open";
+ public static const CLOSE:String = "close";
+ public static const MESSAGE:String = "message";
+ public static const ERROR:String = "error";
+
+ public var message:String;
+
+ public function WebSocketEvent(
+ type:String, message:String = null, bubbles:Boolean = false, cancelable:Boolean = false) {
+ super(type, bubbles, cancelable);
+ this.message = message;
+ }
+
+ public override function clone():Event {
+ return new WebSocketEvent(this.type, this.message, this.bubbles, this.cancelable);
+ }
+
+ public override function toString():String {
+ return "WebSocketEvent: " + this.type + ": " + this.message;
+ }
+}
+
+}
Oops, something went wrong.

0 comments on commit 978e31c

Please sign in to comment.