Skip to content

Commit

Permalink
Updated example to use streams and added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson Silva committed Dec 11, 2013
1 parent 8cba48d commit a4e616e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
30 changes: 16 additions & 14 deletions example/echo.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
library echo;

import "dart:html";
import "dart:convert";
import 'package:logging/logging.dart';
import "package:sockjs_client/sockjs.dart" as SockJS;

DivElement div = querySelector('#first div');
InputElement inp = querySelector('#first input');
FormElement form = querySelector('#first form');

print(m, [p = '']) {
if(!p.isEmpty) {
p = JSON.encode(p);
}
_log(LogRecord l) {
div
..append(new Element.html("<code/>")..text=("$m$p"))
..append(new Element.html("<br>"));

div.scrollTop += 10000;
..append(new Element.html("<code/>")..text = "${l.message}")
..append(new Element.html("<br>"))
..scrollTop += 10000;
}

main() {
print("Starting");
// Setup Logging
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen(_log);

final LOG = new Logger("sockjs");

LOG.info("Starting");
var sockjs_url = 'http://127.0.0.1:8081/echo';
var sockjs = new SockJS.Client(sockjs_url, protocolsWhitelist:['websocket', 'xhr-streaming'], debug: true);
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') );
sockjs.onOpen.listen( (_) => LOG.info('[*] open ${sockjs.protocol}') );
sockjs.onMessage.listen( (e) => LOG.info('[.] message ${e.data}') );
sockjs.onClose.listen( (_) => LOG.info('[*] close') );

inp.onKeyUp.listen( (KeyboardEvent e) {
if (e.keyCode == 13) {
print('[ ] sending ${inp.value}');
LOG.info('[ ] sending ${inp.value}');
sockjs.send(inp.value);
inp.value = '';
}
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ authors:
description: A SockJS client in Dart
dependencies:
browser: '>=0.9.0 <1.0.0'
logging: '>=0.9.0 <1.0.0'
environment:
sdk: '>=1.0.0'
sdk: '>=1.0.0 <2.0.0'

0 comments on commit a4e616e

Please sign in to comment.