Skip to content

Commit

Permalink
Fix the example to act as a server (#100)
Browse files Browse the repository at this point in the history
Fixes a bug mentioned in #50

Both the client and server examples were shown with
`WebSocketChannel.connect`. Update the server to bind the `HttpServer`
instead of trying to connect to a server that hasn't been started.

Add a `client.close()` call to the example so the VM does not continue
running.
  • Loading branch information
natebosch authored Jul 6, 2023
1 parent d80cbd0 commit 509f71e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ A JSON-RPC 2.0 server exposes a set of methods that can be called by clients.
These methods can be registered using `Server.registerMethod`:

```dart
import 'dart:io';
import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() {
var socket = WebSocketChannel.connect(Uri.parse('ws://localhost:4321'));
void main() async {
var httpServer = await HttpServer.bind(InternetAddress.loopbackIPv4, 4321);
var connectedChannels =
httpServer.transform(WebSocketTransformer()).map(IOWebSocketChannel.new);
connectedChannels.listen(handleClient);
}
void handleClient(WebSocketChannel socket) {
// The socket is a `StreamChannel<dynamic>` because it might emit binary
// `List<int>`, but JSON RPC 2 only works with Strings so we assert it only
// emits those by casting it.
Expand Down Expand Up @@ -122,6 +130,8 @@ void main() async {
} on RpcException catch (error) {
print('RPC error ${error.code}: ${error.message}');
}
await client.close();
}
```

Expand Down
2 changes: 2 additions & 0 deletions example/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ void main() async {
} on RpcException catch (error) {
print('RPC error ${error.code}: ${error.message}');
}

await client.close();
}
12 changes: 10 additions & 2 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

void main() {
var socket = WebSocketChannel.connect(Uri.parse('ws://localhost:4321'));
void main() async {
var httpServer = await HttpServer.bind(InternetAddress.loopbackIPv4, 4321);
var connectedChannels =
httpServer.transform(WebSocketTransformer()).map(IOWebSocketChannel.new);
connectedChannels.listen(handleClient);
}

void handleClient(WebSocketChannel socket) {
// The socket is a `StreamChannel<dynamic>` because it might emit binary
// `List<int>`, but JSON RPC 2 only works with Strings so we assert it only
// emits those by casting it.
Expand Down

0 comments on commit 509f71e

Please sign in to comment.