Skip to content

Commit

Permalink
Minor documentation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 29, 2021
1 parent ddda421 commit 1b97797
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.0.0-dev.7

### Minor documentation improvements
- Fix rename of `SocketMode` to `SocketType` in README
- Add receiving messages (`ZMessage`, `ZFrame` and payloads) to README and example
- Override `toString` function in `ZMessage` and `ZFrame` for better debugging experience


## 1.0.0-dev.6

### Free pointers before throwing a ZeroMQException
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final ZContext context = ZContext();

Create socket
```dart
final ZSocket socket = context.createSocket(SocketMode.req);
final ZSocket socket = context.createSocket(SocketType.req);
```

Connect socket
Expand All @@ -49,6 +49,27 @@ Send message
socket.send([1, 2, 3, 4, 5]);
```

Receive `ZMessage`s
```dart
_socket.messages.listen((message) {
// Do something with message
});
```

Receive `ZFrame`s
```dart
_socket.frames.listen((frame) {
// Do something with frame
});
```

Receive payloads (`Uint8List`)
```dart
_socket.payloads.listen((payload) {
// Do something with payload
});
```

Destroy socket
```dart
socket.close();
Expand Down
34 changes: 30 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:dartzmq/dartzmq.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -31,19 +33,43 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
final ZContext _context = ZContext();
late final ZSocket _socket;
String _receivedData = '';
late StreamSubscription _subscription;

@override
void initState() {
_socket = _context.createSocket(SocketType.req);
_socket.connect("tcp://localhost:5566");
// _socket.connect("tcp://192.168.2.18:5566");

// listen for messages
_subscription = _socket.messages.listen((message) {
setState(() {
_receivedData = message.toString();
});
});

// listen for frames
// _subscription = _socket.frames.listen((frame) {
// setState(() {
// _receivedData = frame.toString();
// });
// });

// listen for payloads
// _subscription = _socket.payloads.listen((payload) {
// setState(() {
// _receivedData = payload.toString();
// });
// });
super.initState();
}

@override
void dispose() {
_socket.close();
_context.stop();
_subscription.cancel();
super.dispose();
}

Expand All @@ -61,14 +87,14 @@ class _MyHomePageState extends State<MyHomePage> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Press to send a message',
),
const Text('Press to send a message'),
MaterialButton(
onPressed: _sendMessage,
color: Colors.blue,
child: const Text('Send'),
)
),
const Text('Received'),
Text(_receivedData),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0-dev.4"
version: "1.0.0-dev.7"
fake_async:
dependency: transitive
description:
Expand Down
7 changes: 7 additions & 0 deletions lib/src/zeromq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ class ZFrame {
final bool hasMore;

ZFrame(this.payload, {this.hasMore = false});

@override
String toString() => 'ZFrame[$payload]';
}

/// ZMessage provides a list-like container interface,
Expand Down Expand Up @@ -496,6 +499,10 @@ class ZMessage implements Queue<ZFrame> {

@override
Iterable<T> whereType<T>() => _frames.whereType<T>();

@override
String toString() =>
IterableBase.iterableToFullString(this, 'ZMessage[', ']');
}

/// ZeroMQ sockets present an abstraction of an asynchronous message queue,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dartzmq
description: A simple dart zeromq implementation/wrapper around the libzmq C++ library
version: 1.0.0-dev.6
version: 1.0.0-dev.7
homepage: https://github.com/enwi/dartzmq

environment:
Expand Down

0 comments on commit 1b97797

Please sign in to comment.