Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(603)

Unified Diff: runtime/bin/websocket_impl.dart

Issue 10907047: Use onClosed instead of onError in the WebSocket classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/websocket.dart ('k') | tests/standalone/io/web_socket_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/websocket_impl.dart
diff --git a/runtime/bin/websocket_impl.dart b/runtime/bin/websocket_impl.dart
index d441bbb33786997019daadad847578b78e5fa1e4..26f2b6a3bea58a9038629d86b0a7795f3c35a2f2 100644
--- a/runtime/bin/websocket_impl.dart
+++ b/runtime/bin/websocket_impl.dart
@@ -217,8 +217,9 @@ class _WebSocketProtocolProcessor {
// Move to the next byte.
index++;
}
- } catch (e) {
- _reportError(e);
+ } catch (e, s) {
Søren Gjesse 2012/09/04 07:17:10 Are you using s?
Anders Johnsen 2012/09/04 07:21:27 I did (bug hunt) but removing.
+ if (onClosed !== null) onClosed(1006, "Protocol error");
+ _state = FAILURE;
}
}
@@ -227,7 +228,7 @@ class _WebSocketProtocolProcessor {
*/
void closed() {
if (_state == START || _state == CLOSED || _state == FAILURE) return;
- _reportError(new WebSocketException("Protocol error $_state"));
+ if (onClosed !== null) onClosed(1006, "Connection closed unexpectedly");
_state = CLOSED;
}
@@ -253,7 +254,7 @@ class _WebSocketProtocolProcessor {
if (_isControlFrame()) {
switch (_opcode) {
case _WebSocketOpcode.CLOSE:
- if (onClosed != null) onClosed(null, null);
+ if (onClosed != null) onClosed(1005, "");
_state = CLOSED;
break;
case _WebSocketOpcode.PING:
@@ -283,8 +284,8 @@ class _WebSocketProtocolProcessor {
void _controlFrameEnd() {
switch (_opcode) {
case _WebSocketOpcode.CLOSE:
- int status;
- String reason;
+ int status = 1005;
+ String reason = "";
if (_controlPayload.length > 0) {
if (_controlPayload.length == 1) {
throw new WebSocketException("Protocol error");
@@ -332,17 +333,6 @@ class _WebSocketProtocolProcessor {
_controlPayload = null;
}
- void _reportError(e) {
- // Report the error through the error callback if any. Otherwise
- // throw the error.
- if (onError != null) {
- onError(e);
- _state = FAILURE;
- } else {
- throw e;
- }
- }
-
int _state;
bool _fin;
int _opcode;
@@ -363,17 +353,13 @@ class _WebSocketProtocolProcessor {
Function onPing;
Function onPong;
Function onClosed;
- Function onError;
}
class _WebSocketConnectionBase {
void _socketConnected(Socket socket) {
_socket = socket;
- _socket.onError = (e) {
- _reportError(e);
- _socket.close();
- };
+ _socket.onError = (e) => _socket.close();
}
void _startProcessing(List<int> unparsedData) {
@@ -384,7 +370,6 @@ class _WebSocketConnectionBase {
processor.onPing = _onWebSocketPing;
processor.onPong = _onWebSocketPong;
processor.onClosed = _onWebSocketClosed;
- processor.onError = _onWebSocketError;
if (unparsedData != null) {
processor.update(unparsedData, 0, unparsedData.length);
}
@@ -401,7 +386,7 @@ class _WebSocketConnectionBase {
// that as an error.
if (_closeTimer != null) _closeTimer.cancel();
} else {
- _reportError(new WebSocketException("Unexpected close"));
+ if (_onClosed !== null) _onClosed(1006, "Unexpected close");
}
_socket.close();
};
@@ -415,10 +400,6 @@ class _WebSocketConnectionBase {
_onClosed = callback;
}
- void set onError(void callback(e)) {
- _onError = callback;
- }
-
send(message) {
if (_closeSent) {
throw new WebSocketException("Connection closed");
@@ -530,11 +511,6 @@ class _WebSocketConnectionBase {
}
}
- _onWebSocketError(e) {
- _reportError(e);
- _socket.close();
- }
-
_sendFrame(int opcode, [List<int> data]) {
bool mask = false; // Masking not implemented for server.
int dataLength = data == null ? 0 : data.length;
@@ -570,21 +546,12 @@ class _WebSocketConnectionBase {
}
}
- void _reportError(e) {
- if (_onError != null) {
- _onError(e);
- } else {
- throw e;
- }
- }
-
Socket _socket;
Timer _closeTimer;
int _hash;
Function _onMessage;
Function _onClosed;
- Function _onError;
int _currentMessageType = _WebSocketMessageType.NONE;
_StringDecoder _decoder;
@@ -671,7 +638,11 @@ class _WebSocketClientConnection
[List<String> protocols]) {
_conn.onRequest = _onHttpClientRequest;
_conn.onResponse = _onHttpClientResponse;
- _conn.onError = (e) => _reportError(e);
+ _conn.onError = (e) {
+ if (_onClosed !== null) {
+ _onClosed(1006, "$e");
+ }
+ };
// Generate the nonce now as it is also used to set the hash code.
_generateNonceAndHash();
@@ -826,10 +797,10 @@ class _WebSocket implements WebSocket {
}
};
_wsconn.onNoUpgrade = (response) {
- if (_onerror != null) _onerror("Failed web socket connection");
- };
- _wsconn.onError = (e) {
- if (_onerror != null) _onerror(e);
+ if (_onclose != null) {
Søren Gjesse 2012/09/04 07:17:10 There is a lot of inconsistency in the use of != /
Anders Johnsen 2012/09/04 07:21:27 Done. (changed entire file for consistency).
+ _onclose(
+ new _WebSocketCloseEvent(true, 1006, "Connection not upgraded"));
+ }
};
}
@@ -840,9 +811,7 @@ class _WebSocket implements WebSocket {
_onopen = callback;
}
- void set onerror(Function callback) {
- _onerror = callback;
- }
+ void set onerror(Function callback) {}
void set onclose(Function callback) {
_onclose = callback;
@@ -867,7 +836,6 @@ class _WebSocket implements WebSocket {
WebSocketClientConnection _wsconn;
int _readyState = WebSocket.CONNECTING;
Function _onopen;
- Function _onerror;
Function _onclose;
Function _onmessage;
}
« no previous file with comments | « runtime/bin/websocket.dart ('k') | tests/standalone/io/web_socket_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698