Chromium Code Reviews| Index: runtime/bin/websocket_impl.dart |
| diff --git a/runtime/bin/websocket_impl.dart b/runtime/bin/websocket_impl.dart |
| index b9534e37aba2484a79546094722aae4cfd7b34ff..6b11200d47617f4785b027b984988fca0e7c2a41 100644 |
| --- a/runtime/bin/websocket_impl.dart |
| +++ b/runtime/bin/websocket_impl.dart |
| @@ -678,8 +678,9 @@ class _WebSocketClientConnection |
| } |
| // Connection upgrade successful. |
| - _socketReady(_conn.detachSocket()); |
| + DetachedSocket detachedSocket = _conn.detachSocket(); |
| if (_onOpen != null) _onOpen(); |
| + _socketReady(detachedSocket); |
| } |
| void _generateNonce() { |
| @@ -733,3 +734,106 @@ class _WebSocketClientConnection |
| HttpClientConnection _conn; |
| String _nonce; |
| } |
| + |
| + |
| +class _WebSocket implements WebSocket { |
| + _WebSocket(String url, [protocols]) { |
| + Uri uri = new Uri.fromString(url); |
| + if (uri.scheme != "ws") { |
| + throw new WebSocketException("Unsupported URL scheme ${uri.scheme}"); |
| + } |
| + if (uri.userInfo != "") { |
| + throw new WebSocketException("Unsupported user info ${uri.userInfo}"); |
| + } |
| + int port = uri.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : uri.port; |
| + String path; |
| + if (uri.query != "") { |
| + if (uri.fragment != "") { |
| + path = "${uri.path}?${uri.query}#${uri.fragment}"; |
| + } else { |
| + path = "${uri.path}?${uri.query}"; |
| + } |
| + } else { |
| + path = uri.path; |
| + } |
| + |
| + HttpClient client = new HttpClient(); |
| + HttpClientConnection conn = client.open("GET", uri.domain, port, path); |
| + if (protocols is String) protocols = [protocols]; |
| + _wsconn = new WebSocketClientConnection(conn, protocols); |
| + _wsconn.onOpen = () { |
| + // HTTP client not needed after socket have been detached. |
| + client.shutdown(); |
|
Mads Ager (google)
2012/05/07 11:46:57
Maybe do 'client = null' as well?
Søren Gjesse
2012/05/07 12:55:24
Done.
|
| + _readyState = WebSocket.OPEN; |
| + if (_onopen != null) _onopen(); |
| + }; |
| + _wsconn.onMessage = (message) { |
| + if (_onmessage != null) { |
| + _onmessage(new _WebSocketDataEvent(message)); |
| + } |
| + }; |
| + _wsconn.onClosed = (status, reason) { |
| + _readyState = WebSocket.CLOSED; |
| + if (_onclose != null) { |
| + _onclose(new _WebSocketCloseEvent(true, status, reason)); |
| + } |
| + }; |
| + _wsconn.onNoUpgrade = (response) { |
| + if (_onerror != null) _onerror("Failed web socket connection"); |
| + }; |
| + _wsconn.onError = (e) { |
| + if (_onerror != null) _onerror(e); |
| + }; |
| + } |
| + |
| + int get readyState() => _readyState; |
| + int get bufferedAmount() => 0; |
| + |
| + void set onopen(Function callback) { |
| + _onopen = callback; |
| + } |
| + void set onerror(Function callback) { |
|
Mads Ager (google)
2012/05/07 11:46:57
I would add a blank line before each multi-line me
Søren Gjesse
2012/05/07 12:55:24
Done.
|
| + _onerror = callback; |
| + } |
| + void set onclose(Function callback) { |
| + _onclose = callback; |
| + } |
| + String get extensions() => null; |
| + String get protocol() => null; |
| + void close(int code, String reason) { |
| + if (_readyState < WebSocket.CLOSING) _readyState = WebSocket.CLOSING; |
| + _wsconn.close(code, reason); |
| + } |
| + |
| + void set onmessage(Function callback) { |
| + _onmessage = callback; |
| + } |
| + void send(data) { |
| + _wsconn.send(data); |
| + } |
| + |
| + WebSocketClientConnection _wsconn; |
| + int _readyState = WebSocket.CONNECTING; |
| + Function _onopen; |
| + Function _onerror; |
| + Function _onclose; |
| + Function _onmessage; |
| +} |
| + |
| + |
| +class _WebSocketDataEvent implements WebSocketDataEvent { |
| + _WebSocketDataEvent(this._data); |
| + get data() => _data; |
| + var _data; |
| +} |
| + |
| + |
| +class _WebSocketCloseEvent implements WebSocketCloseEvent { |
| + _WebSocketCloseEvent(this._wasClean, this._code, this._reason); |
| + bool get wasClean() => _wasClean; |
| + int get code() => _code; |
| + String get reason() => _reason; |
| + bool _wasClean; |
| + int _code; |
| + String _reason; |
| +} |