From 2ac02f239bed9b3d0cc7505ac53956b19935f2ea Mon Sep 17 00:00:00 2001 From: sdeleuze Date: Sun, 8 Dec 2013 23:09:53 +0100 Subject: [PATCH 1/5] First set of fixes for Dart 1.0 compatibility --- example/echo.dart | 16 ++++++++-------- example/echo.html | 2 +- lib/sockjs.dart | 3 +-- lib/src/ajax.dart | 2 +- lib/src/transport/sender.dart | 8 ++++---- lib/src/utils.dart | 22 ++++++++++------------ 6 files changed, 25 insertions(+), 28 deletions(-) diff --git a/example/echo.dart b/example/echo.dart index eab25ff..1c24708 100644 --- a/example/echo.dart +++ b/example/echo.dart @@ -1,18 +1,18 @@ library echo; import "dart:html"; -import "dart:json"; +import "dart:convert"; import "package:sockjs_client/sockjs.dart" as SockJS; -DivElement div = query('#first div'); -InputElement inp = query('#first input'); -FormElement form = query('#first form'); +DivElement div = querySelector('#first div'); +InputElement inp = querySelector('#first input'); +FormElement form = querySelector('#first form'); print(m, [p = '']) { if(!p.isEmpty) { - p = JSON.stringify(p); + p = JSON.encode(p); } - div.elements + div.children ..add(new Element.html("")..text=("$m$p")) ..add(new Element.html("
")); @@ -23,13 +23,13 @@ main() { print("Starting"); var sockjs_url = 'http://127.0.0.1:8081/echo'; var sockjs = new SockJS.Client(sockjs_url, protocolsWhitelist:['websocket', 'xhr-streaming'], debug: true); - query('#first input').focus(); + querySelector('#first input').focus(); sockjs.on.open.add( (_) => print('[*] open ${sockjs.protocol}') ); sockjs.on.message.add( (e) => print('[.] message ${e.data}') ); sockjs.on.close.add( (_) => print('[*] close') ); - inp.on.keyUp.add( (KeyboardEvent e) { + inp.onKeyUp.listen( (KeyboardEvent e) { if (e.keyCode == 13) { print('[ ] sending ${inp.value}'); sockjs.send(inp.value); diff --git a/example/echo.html b/example/echo.html index c407f20..8f06fee 100644 --- a/example/echo.html +++ b/example/echo.html @@ -44,6 +44,6 @@

SockJS Echo example

- + diff --git a/lib/sockjs.dart b/lib/sockjs.dart index 42a3188..8a12eff 100644 --- a/lib/sockjs.dart +++ b/lib/sockjs.dart @@ -1,8 +1,7 @@ library sockjs_client; import "dart:html"; -import "dart:json"; -import "dart:isolate"; +import "dart:convert"; import "src/events.dart" as event; import "src/utils.dart" as utils; diff --git a/lib/src/ajax.dart b/lib/src/ajax.dart index 2714924..8cf442b 100644 --- a/lib/src/ajax.dart +++ b/lib/src/ajax.dart @@ -54,7 +54,7 @@ class AbstractXHRObject implements event.Emitter { // "This never affects same-site requests." xhr.withCredentials = true; } - if (?headers) { + if (headers != null) { headers.forEach((k, v) => xhr.setRequestHeader(k, v)); } diff --git a/lib/src/transport/sender.dart b/lib/src/transport/sender.dart index 12b29d3..18f2403 100644 --- a/lib/src/transport/sender.dart +++ b/lib/src/transport/sender.dart @@ -43,7 +43,7 @@ class BufferedSender { sendSchedule() { if (!sendBuffer.isEmpty) { - var payload = '[${Strings.join(sendBuffer, ',')}]'; + var payload = '[${sendBuffer.join(',')}]'; sendStop = sender(transUrl, payload, ([status, reason]) { @@ -82,8 +82,8 @@ class JsonPGenericSender { form.method = 'POST'; form.enctype = 'application/x-www-form-urlencoded'; form.acceptCharset = "UTF-8"; - form.elements.add(area); - document.body.elements.add(form); + form.children.add(area); + document.body.children.add(form); } form = _sendForm; area = _sendArea; @@ -100,7 +100,7 @@ class JsonPGenericSender { iframe.name = id; } iframe.id = id; - form.elements.add(iframe); + form.children.add(iframe); iframe.style.display = 'none'; try { diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 50f4c2a..a849fb6 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -2,20 +2,19 @@ library utils; import 'dart:math' as Math; import 'dart:html'; -import 'dart:json'; -import 'dart:isolate'; +import 'dart:convert'; import "package:sockjs_client/sockjs.dart" as SockJS; const random_string_chars = 'abcdefghijklmnopqrstuvwxyz0123456789_'; String random_string(length, [max]) { - if (!?max) max = random_string_chars.length; + if (max == null) max = random_string_chars.length; var i, ret = [], rnd = new Math.Random(), r; for(i=0; i < length; i++) { r = rnd.nextInt(max); ret.add( random_string_chars.substring(r, r+1) ); } - return Strings.join(ret, ''); + return ret.join(''); } int random_number(max) => new Math.Random().nextInt(max); @@ -26,7 +25,7 @@ String random_number_string(max) { for (int i = 0; i < t+1; i++) { l.add('0'); } - var p = Strings.join(l,''); + var p = l.join(''); var s = "$p${random_number(max)}"; return s.substring(s.length - t); } @@ -44,11 +43,11 @@ amendUrl(String url) { } // '//abc' --> 'http://abc' - if (url.indexOf('//') === 0) { + if (identical(url.indexOf('//'), 0)) { url = "${dl.protocol}$url"; } // '/abc' --> 'http://localhost:80/abc' - if (url.indexOf('/') === 0) { + if (identical(url.indexOf('/'), 0)) { url = "${dl.protocol}//${dl.host}$url"; } // strip trailing slashes @@ -76,14 +75,13 @@ num countRTO(num rtt) { bool isSameOriginUrl(String url_a, [String url_b]) { // location.origin would do, but it's not always available. - if (!?url_b) url_b = window.location.toString(); + if (url_b == null) url_b = window.location.toString(); - return ( Strings.join( url_a.split('/').getRange(0,3), '/') - == - Strings.join(url_b.split('/').getRange(0,3),'/')); + return ( url_a.split('/').getRange(0,3).join('/') + == url_b.split('/').getRange(0,3).join('/')); } -String quote(String string) => JSON.stringify(string); +String quote(String string) => JSON.encode(string); const _all_protocols = const [ 'websocket', From 36320e709bdcff92e9872bf02fb685f47f83769d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 9 Dec 2013 11:42:02 +0100 Subject: [PATCH 2/5] Finish to fix last warnings and errors, now I need to test this with a Node or Spring 4 backend ... --- lib/sockjs.dart | 1 + lib/src/ajax.dart | 11 +++-- lib/src/client.dart | 18 +++---- lib/src/events.dart | 2 +- lib/src/info.dart | 16 +++---- lib/src/transport/sender.dart | 80 +------------------------------- lib/src/transport/websocket.dart | 12 +++-- lib/src/utils.dart | 5 +- 8 files changed, 36 insertions(+), 109 deletions(-) diff --git a/lib/sockjs.dart b/lib/sockjs.dart index 8a12eff..58694a3 100644 --- a/lib/sockjs.dart +++ b/lib/sockjs.dart @@ -2,6 +2,7 @@ library sockjs_client; import "dart:html"; import "dart:convert"; +import "dart:async"; import "src/events.dart" as event; import "src/utils.dart" as utils; diff --git a/lib/src/ajax.dart b/lib/src/ajax.dart index 8cf442b..64cac0b 100644 --- a/lib/src/ajax.dart +++ b/lib/src/ajax.dart @@ -18,6 +18,7 @@ class AbstractXHRObject implements event.Emitter { XHREvents on = new XHREvents(); HttpRequest xhr; + StreamSubscription orschss; _start(method, url, payload, {noCredentials: false, headers}) { @@ -41,7 +42,7 @@ class AbstractXHRObject implements event.Emitter { //that.unload_ref = utils.unload_add(function(){that._cleanup(true);}); try { - xhr.open(method, url, true); + xhr.open(method, url); } catch(e) { // IE raises an exception on wrong port. on.finish.dispatch(new StatusEvent()); @@ -58,7 +59,7 @@ class AbstractXHRObject implements event.Emitter { headers.forEach((k, v) => xhr.setRequestHeader(k, v)); } - xhr.on.readyStateChange.add(_readyStateHandler); + orschss = xhr.onReadyStateChange.listen(_readyStateHandler); xhr.send(payload); } @@ -91,7 +92,7 @@ class AbstractXHRObject implements event.Emitter { // utils.unload_del(that.unload_ref); // IE needs this field to be a function - xhr.on.readyStateChange.remove(_readyStateHandler); + orschss.cancel(); if (abort) { try { @@ -109,7 +110,7 @@ class AbstractXHRObject implements event.Emitter { class XHRCorsObject extends AbstractXHRObject { XHRCorsObject(method, url, payload, {noCredentials, headers} ) { - utils.delay(()=>_start(method, url, payload, noCredentials: false)); + Timer.run(_start(method, url, payload, noCredentials: false)); } } @@ -117,7 +118,7 @@ class XHRCorsObject extends AbstractXHRObject { class XHRLocalObject extends AbstractXHRObject { XHRLocalObject (method, url, payload, {noCredentials, headers}) { - utils.delay(() => _start(method, url, payload, noCredentials: true)); + Timer.run(_start(method, url, payload, noCredentials: true)); } } diff --git a/lib/src/client.dart b/lib/src/client.dart index b89138c..8ce0901 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -110,7 +110,7 @@ class Client implements event.Emitter { } readyState = CLOSED; - utils.delay(() => on.close.dispatch(close_event)); + Timer.run(on.close.dispatch(close_event)); } _dispatchOpen() { @@ -136,7 +136,7 @@ class Client implements event.Emitter { } _dispatchHeartbeat() { - if (readyState !== OPEN) { + if (!identical(readyState, OPEN)) { return; } on.heartbeat.dispatch(); @@ -151,7 +151,7 @@ class Client implements event.Emitter { case 'a': var s = data.substring(1); if (s == null) s = '[]'; - var payload = JSON.parse(s); + var payload = JSON.decode(s); for(var i=0; i < payload.length; i++){ _dispatchMessage(payload[i]); } @@ -159,13 +159,13 @@ class Client implements event.Emitter { case 'm': var s = data.substring(1); if (s == null) s = 'null'; - var payload = JSON.parse(s); + var payload = JSON.decode(s); _dispatchMessage(payload); break; case 'c': var s = data.substring(1); if (s == null) s = '[]'; - var payload = JSON.parse(s); + var payload = JSON.decode(s); _didClose(payload[0], payload[1]); break; case 'h': @@ -198,9 +198,9 @@ class Client implements event.Emitter { PROTOCOLS[protocol].needBody && ( (document.body == null) || (document.readyState != null && document.readyState != 'complete')) ) { - _protocols.insertRange(0, 1, protocol); + _protocols.insert(0, protocol); this.protocol = 'waiting-for-load'; - document.on.load.add( (_) => _tryNextProtocol()); + document.onLoad.listen( (_) => _tryNextProtocol()); return true; } @@ -211,14 +211,14 @@ class Client implements event.Emitter { var roundTrips = PROTOCOLS[protocol].roundTrips; var to = rto * roundTrips; if (to == 0) to = 5000; - _transportTref = utils.delay(() { + _transportTref = new Timer(new Duration(milliseconds:to), () { if (readyState == CONNECTING) { // I can't understand how it is possible to run // this timer, when the state is CLOSED, but // apparently in IE everythin is possible. _didClose(2007, "Transport timeouted"); } - }, to); + }); var connid = utils.random_string(8); var trans_url = "$_baseUrl/$server/$connid"; diff --git a/lib/src/events.dart b/lib/src/events.dart index d26d137..dea2973 100644 --- a/lib/src/events.dart +++ b/lib/src/events.dart @@ -11,7 +11,7 @@ class Events { Map _listeners; - Events() : _listeners = {}; + Events() : _listeners = {}; ListenerList operator [](String type) => _listeners.putIfAbsent(type, () => new ListenerList(type)); diff --git a/lib/src/info.dart b/lib/src/info.dart index 478e400..2afee6f 100644 --- a/lib/src/info.dart +++ b/lib/src/info.dart @@ -56,21 +56,21 @@ abstract class InfoReceiver implements event.Emitter { class AjaxInfoReceiver extends InfoReceiver { AjaxInfoReceiver(String baseUrl, AjaxObjectFactory xhrFactory) { - utils.delay(() => doXhr(baseUrl, xhrFactory)); + Timer.run(doXhr(baseUrl, xhrFactory)); } doXhr(String baseUrl, AjaxObjectFactory xhrFactory) { - var t0 = new Date.now().millisecondsSinceEpoch; + var t0 = new DateTime.now().millisecondsSinceEpoch; var xo = xhrFactory('GET', "$baseUrl/info"); - var tref = utils.delay(() => xo.on.timeout.dispatch(), 8000); + var tref = new Timer(new Duration(milliseconds:8000), xo.on.timeout.dispatch()); xo.on.finish.add( (StatusEvent evt) { tref.cancel(); tref = null; if (evt.status == 200) { - var rtt = new Date.now().millisecondsSinceEpoch - t0; - var info = new Info.fromJSON(JSON.parse(evt.text)); + var rtt = new DateTime.now().millisecondsSinceEpoch - t0; + var info = new Info.fromJSON(JSON.decode(evt.text)); on.finish.dispatch(new InfoReceiverEvent(info, rtt)); } else { on.finish.dispatch(new InfoReceiverEvent()); @@ -88,7 +88,7 @@ class InfoReceiverIframe extends InfoReceiver { InfoReceiverIframe (base_url) { if(document.body == null) { - document.on.load.add((_) => go()); + document.onLoad.listen((_) => go()); } else { go(); } @@ -127,7 +127,7 @@ class InfoReceiverFake extends InfoReceiver { // It may not be possible to do cross domain AJAX to get the info // data, for example for IE7. But we want to run JSONP, so let's // fake the response, with rtt=2s (rto=6s). - utils.delay(() => on.finish.dispatch(), 2000); + new Timer(new Duration(milliseconds:2000), on.finish.dispatch()); } } @@ -137,7 +137,7 @@ class WInfoReceiverIframe { WInfoReceiverIframe(ri, _trans_url, baseUrl) { var ir = new AjaxInfoReceiver(baseUrl, XHRLocalObjectFactory); ir.on.finish.add( (evt) { - ri._didMessage('m${JSON.stringify([evt.info, evt.rtt])}'); + ri._didMessage('m${JSON.encode([evt.info, evt.rtt])}'); ri._didClose(); }); } diff --git a/lib/src/transport/sender.dart b/lib/src/transport/sender.dart index 18f2403..7bcacd1 100644 --- a/lib/src/transport/sender.dart +++ b/lib/src/transport/sender.dart @@ -35,10 +35,10 @@ class BufferedSender { sendStop = null; tref.cancel(); }; - tref = utils.delay(() { + tref = new Timer(new Duration(milliseconds:25), () { sendStop = null; sendSchedule(); - }, 25); + }); } sendSchedule() { @@ -62,82 +62,6 @@ class BufferedSender { } } -class JsonPGenericSender { - - FormElement _sendForm = null; - TextAreaElement _sendArea = null; - - var completed; - - JsonPGenericSender(url, payload, callback) { - FormElement form; - TextAreaElement area; - - if (_sendForm == null) { - form = _sendForm = new Element.tag('form'); - area = _sendArea = new Element.tag('textarea'); - area.name = 'd'; - form.style.display = 'none'; - form.style.position = 'absolute'; - form.method = 'POST'; - form.enctype = 'application/x-www-form-urlencoded'; - form.acceptCharset = "UTF-8"; - form.children.add(area); - document.body.children.add(form); - } - form = _sendForm; - area = _sendArea; - var id = 'a${utils.random_string(8)}'; - form.target = id; - form.action = '$url/jsonp_send?i=$id'; - - IFrameElement iframe; - try { - // ie6 dynamic iframes with target="" support (thanks Chris Lambacher) - iframe = new Element.html('