Skip to content

Commit

Permalink
fixup! feat: add experimental CoAP server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Oct 31, 2022
1 parent 45c5b7f commit 6913d52
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions example/coap_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:coap/coap.dart';

FutureOr<void> main() async {
Expand All @@ -24,9 +25,9 @@ FutureOr<void> main() async {
print('Received the following request: $request\n');
print('Sending response...\n');
server
..reply(
..respond(
request,
payload: utf8.encode('Hello World'),
payload: Uint8List.fromList(utf8.encode('Hello World')),
responseCode: CoapCode.content,
contentFormat: CoapMediaType.applicationTdJson,
)
Expand Down
15 changes: 11 additions & 4 deletions lib/src/coap_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ abstract class CoapServer extends Stream<CoapRequest> {
final int port,
);

void reply(
void respond(
final CoapRequest request, {
required final CoapCode responseCode,
final List<int>? payload,
final Uint8List? payload,
final CoapMediaType? contentFormat,
});

void reject(final CoapRequest request);

void close();
}

Expand Down Expand Up @@ -188,10 +190,10 @@ class _CoapUdpServer extends CoapServer {
}

@override
void reply(
void respond(
final CoapRequest request, {
required final CoapCode responseCode,
final List<int>? payload,
final Uint8List? payload,
final CoapMediaType? contentFormat,
}) {
final response = CoapResponse.createResponse(
Expand All @@ -208,4 +210,9 @@ class _CoapUdpServer extends CoapServer {

_send(response, request.source!, request.uriPort);
}

@override
void reject(final CoapRequest request) {
_rejectRequest(request, request.source!, request.uriPort);
}
}

0 comments on commit 6913d52

Please sign in to comment.