Skip to content

Instantly share code, notes, and snippets.

@dvv
Created September 21, 2011 15:04
Show Gist options
  • Save dvv/1232284 to your computer and use it in GitHub Desktop.
Save dvv/1232284 to your computer and use it in GitHub Desktop.
wasClean for web-socket-js
diff --git a/flash-src/src/net/gimite/websocket/WebSocket.as b/flash-src/src/net/gimite/websocket/WebSocket.as
index fb3555e..ec7d769 100644
--- a/flash-src/src/net/gimite/websocket/WebSocket.as
+++ b/flash-src/src/net/gimite/websocket/WebSocket.as
@@ -158,7 +158,7 @@ public class WebSocket extends EventDispatcher {
}
}
- public function close(isError:Boolean = false, byServer:Boolean = false):void {
+ public function close(isError:Boolean = false, byServer:Boolean = false, wasClean:Boolean = false):void {
try {
if (readyState == OPEN && !isError) {
// TODO: send code and reason
@@ -174,7 +174,8 @@ public class WebSocket extends EventDispatcher {
if (byServer || isError) {
logger.log("closed");
readyState = CLOSED;
- this.dispatchEvent(new WebSocketEvent(isError ? "error" : "close"));
+ var name:String = isError ? "error" : (wasClean ? "closeclean" : "close");
+ this.dispatchEvent(new WebSocketEvent(name));
} else {
logger.log("closing");
readyState = CLOSING;
@@ -255,7 +256,7 @@ public class WebSocket extends EventDispatcher {
private function onError(message:String):void {
if (readyState == CLOSED) return;
logger.error(message);
- close(readyState != CONNECTING);
+ close(readyState != CONNECTING, false, false);
}
private function onSocketData(event:ProgressEvent):void {
@@ -298,8 +299,14 @@ public class WebSocket extends EventDispatcher {
break;
case OPCODE_CLOSE:
// TODO: extract code and reason string
- logger.log("received closing frame");
- close(false, true);
+ var status:int = 1002;
+ ///var reason:String = 'OK';
+ if (frame.payload.length >= 2) {
+ status = (frame.payload[0] << 8) | (frame.payload[1] << 0);
+ ///reason = readUTFBytes(frame.payload, 2, frame.payload.length - 2);
+ }
+ logger.error("received closing frame. Status: " + status);//"; Reason: " + reason);
+ close(false, true, status === 1000 /* CLOSE_REASON_NORMAL */);
break;
case OPCODE_PING:
sendPong(frame.payload);
diff --git a/flash-src/src/net/gimite/websocket/WebSocketMain.as b/flash-src/src/net/gimite/websocket/WebSocketMain.as
index 015fd54..d382e40 100644
--- a/flash-src/src/net/gimite/websocket/WebSocketMain.as
+++ b/flash-src/src/net/gimite/websocket/WebSocketMain.as
@@ -91,6 +91,7 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
getCookie(url), headers, this);
newSocket.addEventListener("open", onSocketEvent);
newSocket.addEventListener("close", onSocketEvent);
+ newSocket.addEventListener("closeclean", onSocketEvent);
newSocket.addEventListener("error", onSocketEvent);
newSocket.addEventListener("message", onSocketEvent);
webSockets[webSocketId] = newSocket;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment