From 509f71eef90ec5afb5486b69dab7fed97b9f1eef Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Wed, 5 Jul 2023 17:59:39 -0700 Subject: [PATCH] Fix the example to act as a server (#100) 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. --- README.md | 14 ++++++++++++-- example/client.dart | 2 ++ example/main.dart | 12 ++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e95a2e8..8ccfc57 100644 --- a/README.md +++ b/README.md @@ -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` because it might emit binary // `List`, but JSON RPC 2 only works with Strings so we assert it only // emits those by casting it. @@ -122,6 +130,8 @@ void main() async { } on RpcException catch (error) { print('RPC error ${error.code}: ${error.message}'); } + + await client.close(); } ``` diff --git a/example/client.dart b/example/client.dart index d23be94..aa8f7ed 100644 --- a/example/client.dart +++ b/example/client.dart @@ -38,4 +38,6 @@ void main() async { } on RpcException catch (error) { print('RPC error ${error.code}: ${error.message}'); } + + await client.close(); } diff --git a/example/main.dart b/example/main.dart index fc6042e..7d5ab73 100644 --- a/example/main.dart +++ b/example/main.dart @@ -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` because it might emit binary // `List`, but JSON RPC 2 only works with Strings so we assert it only // emits those by casting it.