Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 final String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 5 final String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 6 | 6 |
| 7 class _WebSocketMessageType { | 7 class _WebSocketMessageType { |
| 8 static final int NONE = 0; | 8 static final int NONE = 0; |
| 9 static final int BINARY = 1; | 9 static final int BINARY = 1; |
| 10 static final int TEXT = 2; | 10 static final int TEXT = 2; |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 return; | 671 return; |
| 672 } | 672 } |
| 673 | 673 |
| 674 if (!_isWebSocketUpgrade(response)) { | 674 if (!_isWebSocketUpgrade(response)) { |
| 675 _conn.detachSocket().socket.close(); | 675 _conn.detachSocket().socket.close(); |
| 676 throw new WebSocketException("Protocol upgrade failed"); | 676 throw new WebSocketException("Protocol upgrade failed"); |
| 677 return; | 677 return; |
| 678 } | 678 } |
| 679 | 679 |
| 680 // Connection upgrade successful. | 680 // Connection upgrade successful. |
| 681 _socketReady(_conn.detachSocket()); | 681 DetachedSocket detachedSocket = _conn.detachSocket(); |
| 682 if (_onOpen != null) _onOpen(); | 682 if (_onOpen != null) _onOpen(); |
| 683 _socketReady(detachedSocket); | |
| 683 } | 684 } |
| 684 | 685 |
| 685 void _generateNonce() { | 686 void _generateNonce() { |
| 686 assert(_nonce == null); | 687 assert(_nonce == null); |
| 687 void intToBigEndianBytes(int value, List<int> bytes, int offset) { | 688 void intToBigEndianBytes(int value, List<int> bytes, int offset) { |
| 688 bytes[offset] = (value >> 24) & 0xFF; | 689 bytes[offset] = (value >> 24) & 0xFF; |
| 689 bytes[offset + 1] = (value >> 16) & 0xFF; | 690 bytes[offset + 1] = (value >> 16) & 0xFF; |
| 690 bytes[offset + 2] = (value >> 8) & 0xFF; | 691 bytes[offset + 2] = (value >> 8) & 0xFF; |
| 691 bytes[offset + 3] = value & 0xFF; | 692 bytes[offset + 3] = value & 0xFF; |
| 692 } | 693 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 } | 727 } |
| 727 return true; | 728 return true; |
| 728 } | 729 } |
| 729 | 730 |
| 730 Function _onRequest; | 731 Function _onRequest; |
| 731 Function _onOpen; | 732 Function _onOpen; |
| 732 Function _onNoUpgrade; | 733 Function _onNoUpgrade; |
| 733 HttpClientConnection _conn; | 734 HttpClientConnection _conn; |
| 734 String _nonce; | 735 String _nonce; |
| 735 } | 736 } |
| 737 | |
| 738 | |
| 739 class _WebSocket implements WebSocket { | |
| 740 _WebSocket(String url, [protocols]) { | |
| 741 Uri uri = new Uri.fromString(url); | |
| 742 if (uri.scheme != "ws") { | |
| 743 throw new WebSocketException("Unsupported URL scheme ${uri.scheme}"); | |
| 744 } | |
| 745 if (uri.userInfo != "") { | |
| 746 throw new WebSocketException("Unsupported user info ${uri.userInfo}"); | |
| 747 } | |
| 748 int port = uri.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : uri.port; | |
| 749 String path; | |
| 750 if (uri.query != "") { | |
| 751 if (uri.fragment != "") { | |
| 752 path = "${uri.path}?${uri.query}#${uri.fragment}"; | |
| 753 } else { | |
| 754 path = "${uri.path}?${uri.query}"; | |
| 755 } | |
| 756 } else { | |
| 757 path = uri.path; | |
| 758 } | |
| 759 | |
| 760 HttpClient client = new HttpClient(); | |
| 761 HttpClientConnection conn = client.open("GET", uri.domain, port, path); | |
| 762 if (protocols is String) protocols = [protocols]; | |
| 763 _wsconn = new WebSocketClientConnection(conn, protocols); | |
| 764 _wsconn.onOpen = () { | |
| 765 // HTTP client not needed after socket have been detached. | |
| 766 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.
| |
| 767 _readyState = WebSocket.OPEN; | |
| 768 if (_onopen != null) _onopen(); | |
| 769 }; | |
| 770 _wsconn.onMessage = (message) { | |
| 771 if (_onmessage != null) { | |
| 772 _onmessage(new _WebSocketDataEvent(message)); | |
| 773 } | |
| 774 }; | |
| 775 _wsconn.onClosed = (status, reason) { | |
| 776 _readyState = WebSocket.CLOSED; | |
| 777 if (_onclose != null) { | |
| 778 _onclose(new _WebSocketCloseEvent(true, status, reason)); | |
| 779 } | |
| 780 }; | |
| 781 _wsconn.onNoUpgrade = (response) { | |
| 782 if (_onerror != null) _onerror("Failed web socket connection"); | |
| 783 }; | |
| 784 _wsconn.onError = (e) { | |
| 785 if (_onerror != null) _onerror(e); | |
| 786 }; | |
| 787 } | |
| 788 | |
| 789 int get readyState() => _readyState; | |
| 790 int get bufferedAmount() => 0; | |
| 791 | |
| 792 void set onopen(Function callback) { | |
| 793 _onopen = callback; | |
| 794 } | |
| 795 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.
| |
| 796 _onerror = callback; | |
| 797 } | |
| 798 void set onclose(Function callback) { | |
| 799 _onclose = callback; | |
| 800 } | |
| 801 String get extensions() => null; | |
| 802 String get protocol() => null; | |
| 803 void close(int code, String reason) { | |
| 804 if (_readyState < WebSocket.CLOSING) _readyState = WebSocket.CLOSING; | |
| 805 _wsconn.close(code, reason); | |
| 806 } | |
| 807 | |
| 808 void set onmessage(Function callback) { | |
| 809 _onmessage = callback; | |
| 810 } | |
| 811 void send(data) { | |
| 812 _wsconn.send(data); | |
| 813 } | |
| 814 | |
| 815 WebSocketClientConnection _wsconn; | |
| 816 int _readyState = WebSocket.CONNECTING; | |
| 817 Function _onopen; | |
| 818 Function _onerror; | |
| 819 Function _onclose; | |
| 820 Function _onmessage; | |
| 821 } | |
| 822 | |
| 823 | |
| 824 class _WebSocketDataEvent implements WebSocketDataEvent { | |
| 825 _WebSocketDataEvent(this._data); | |
| 826 get data() => _data; | |
| 827 var _data; | |
| 828 } | |
| 829 | |
| 830 | |
| 831 class _WebSocketCloseEvent implements WebSocketCloseEvent { | |
| 832 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); | |
| 833 bool get wasClean() => _wasClean; | |
| 834 int get code() => _code; | |
| 835 String get reason() => _reason; | |
| 836 bool _wasClean; | |
| 837 int _code; | |
| 838 String _reason; | |
| 839 } | |
| OLD | NEW |