Coder Social home page Coder Social logo

iezed / easy_udp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zgramming/easy_udp

0.0 0.0 0.0 10 KB

This library provides a wrapper over RawDatagramSocket to gracefully handle UDP connections in dartlang. ๐Ÿ˜†

Home Page: https://pub.dev/packages/easy_udp

License: MIT License

Dart 100.00%

easy_udp's Introduction

Features

  • receive Datagrams on demand without subscription.
  • receive timeout.
  • Various shorthand methods.

Usage

usage example:

import 'dart:convert';
import 'package:easy_udp/easy_udp.dart';

start_server() async {
  // Create a EasyUDPSocket and bind to localhost:7777,
  // Note that you can also manually create a RawDatagramSocket 
  // and pass it to EasyUDPSocket(..) to create a EasyUDPSocket.
  final socket = await EasyUDPSocket.bind('localhost', 7777);

  while (true) {
    // Rather than subscribing to a stream of RawSocketEvents and
    // calling receive in the callback, which is the case when using
    // RawDatagramSocket, with EasyUDPSocket, you can get Datagram 
    // on demand with `receive` method.
    final datagram = await socket.receive();
    print('Server received: ${ascii.decode(datagram.data)} from ${datagram.port}');

    // use `sendBack` to send message to where a Datagram comes from.
    // This is a shorthand of:
    // socket.send(somedata, datagram.address, datagram.port);
    socket.sendBack(datagram, ascii.encode('pong'));
  }
}

start_client(int port) async {
  final socket = await EasyUDPSocket.bind('localhost', port);
  socket.send(ascii.encode('ping'), 'localhost', 7777);
  final resp = await socket.receive();
  print('Client $port received: ${ascii.decode(resp.data)}');

  // `close` method of EasyUDPSocket is awaitable.
  await socket.close();
  print('Client $port closed');
}

main() async {
  start_server();
  await Future.delayed(Duration(seconds: 1));
  start_client(8001);
  start_client(8002);
  start_client(8003);
}

// Output:
//
// Server received: ping from 8001
// Server received: ping from 8002
// Server received: ping from 8003
// Client 8001 received: pong
// Client 8002 received: pong
// Client 8003 received: pong
// Client 8001 closed
// Client 8002 closed
// Client 8003 closed

with timeout:

...
final datagram = await socket.receive(timeout: 1000);
if(datagram == null) {
  print('Receive timeout');
  continue;
}
...

// Output:
//
// Server received: ping from 8001
// Server received: ping from 8002
// Server received: ping from 8003
// Client 8001 received: pong
// Client 8002 received: pong
// Client 8003 received: pong
// Client 8001 closed
// Client 8002 closed
// Client 8003 closed
// Receive timeout
// Receive timeout
// Receive timeout
// Receive timeout

Features and bugs

Please file feature requests and bugs at the issue tracker.

easy_udp's People

Contributors

xtyxtyx avatar zgramming avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.