From 62794cb85668904f3f23502f27efb89a1491b6a8 Mon Sep 17 00:00:00 2001 From: Benjamin Jung Date: Wed, 29 Jan 2020 11:39:51 +0900 Subject: [PATCH 1/2] WebSocket without name. It's from v0.4.0, but, the websocket message format is changed without name. --- example/app.dart | 9 +++------ lib/socket.dart | 14 ++++++-------- lib/src/message.dart | 35 ----------------------------------- lib/src/socket.dart | 13 +++++-------- lib/src/socket_base.dart | 6 ++---- 5 files changed, 16 insertions(+), 61 deletions(-) delete mode 100644 lib/src/message.dart diff --git a/example/app.dart b/example/app.dart index 6fea2bb..dc824ed 100644 --- a/example/app.dart +++ b/example/app.dart @@ -38,13 +38,10 @@ void main() { }); }); - app.ws('/socket').listen((socket) { - socket.on('connected').listen((data) { - socket.send('ping', 'data-from-ping'); - }); + app.ws('/').listen((socket) { - socket.on('pong').listen((data) { - print('pong: $data'); + socket.onMessage().listen((data) { + print('msg: $data'); socket.close(1000, 'requested'); }); diff --git a/lib/socket.dart b/lib/socket.dart index 928f7e0..9bf562b 100644 --- a/lib/socket.dart +++ b/lib/socket.dart @@ -14,19 +14,17 @@ class Socket implements SocketBase { this._ws = new WebSocket(url) { _messages = _messageController.stream.asBroadcastStream(); _ws.onMessage.listen((e) { - var msg = new Message.fromPacket(e.data); + var msg = e.data; _messageController.add(msg); }); } - void send(String messageName, [ data ]) { - var message = new Message(messageName, data); - _ws.send(message.toPacket()); + void send(String message) { + _ws.send(message); } - Stream on(String messageName) { - return _messages.where((msg) => msg.name == messageName).map((msg) => - msg.data); + Stream onMessage() { + return _messages; } Stream get onOpen => _ws.onOpen; @@ -36,4 +34,4 @@ class Socket implements SocketBase { void close([int status, String reason]) { _ws.close(status, reason); } -} \ No newline at end of file +} diff --git a/lib/src/message.dart b/lib/src/message.dart deleted file mode 100644 index 28c6b9c..0000000 --- a/lib/src/message.dart +++ /dev/null @@ -1,35 +0,0 @@ -library start_message; - -import 'dart:convert'; - -class Message { - final String name; - final Object data; - - Message(this.name, [this.data]); - - factory Message.fromPacket(String message) { - if (message.isEmpty) { - return new Message.empty(); - } - - List parts = message.split(':'); - String name = parts.first; - var data = null; - - if (parts.length > 1 && !parts[1].isEmpty) { - data = jsonDecode(parts.sublist(1).join(':')); - } - - return new Message(name, data); - } - - Message.empty() : this(''); - - String toPacket() { - if (data == null) { - return name; - } - return '$name:${jsonEncode(data)}'; - } -} \ No newline at end of file diff --git a/lib/src/socket.dart b/lib/src/socket.dart index 9a01aea..d47a476 100644 --- a/lib/src/socket.dart +++ b/lib/src/socket.dart @@ -13,22 +13,19 @@ class Socket implements SocketBase { _openController.add(_ws); _ws.listen((data) { - var msg = new Message.fromPacket(data); - _messageController.add(msg); + _messageController.add(data); }, onDone: () { _closeController.add(_ws); }); } - void send(String messageName, [ data ]) { - var message = new Message(messageName, data); - _ws.add(message.toPacket()); + void send(String message) { + _ws.add(message); } - Stream on(String messageName) { - return _messages.where((msg) => msg.name == messageName).map((msg) => - msg.data); + Stream onMessage() { + return _messages; } Stream get onOpen => _openController.stream; diff --git a/lib/src/socket_base.dart b/lib/src/socket_base.dart index 76c4247..332b7d4 100644 --- a/lib/src/socket_base.dart +++ b/lib/src/socket_base.dart @@ -2,12 +2,10 @@ library start_socket; import 'dart:async'; -export 'message.dart' show Message; - abstract class SocketBase { - void send(String msg_name, [ data ]); + void send(String msg); - Stream on(String message_name); + Stream onMessage(); void close([int status, String reason]); } From a50e597c8472beca1f749d4e3817996ad18cf18f Mon Sep 17 00:00:00 2001 From: Benjamin Jung Date: Wed, 29 Jan 2020 11:49:36 +0900 Subject: [PATCH 2/2] Update pubspec.yaml - version 0.5.0 --- CHANGELOG.md | 4 ++++ pubspec.yaml | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4392e1e..7345508 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.5.0 + +Websocket format is changed. + ## 0.4.0 Fix Websockets diff --git a/pubspec.yaml b/pubspec.yaml index 9593a15..afcad66 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: start -author: Yehor Lvivski -version: 0.4.0 +author: Yehor Lvivski , Benjamin Jung +version: 0.5.0 homepage: http://github.com/lvivski/start description: Sinatra inspired Web framework environment: