パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.3.1

tcp_socket_connection

TCP ソケットを介して接続、リスニング、メッセージの送信を行うためのFlutterパッケージ。

AndroidiOSmacOSWindowsLinux
30日間ダウンロード
381
いいね数
43
Pub ポイント
150

パッケージをインストール

flutter pub add tcp_socket_connection

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

クロスプラットフォームのTCPソケット通信を可能にするFlutterパッケージで、接続、リスニング、メッセージの送信が行えます。恒久的なTCP接続の確立、リアルタイムデータ交換の処理、接続状態の管理に向けたシンプルなインターフェースを提供します。IoTデバイス、カスタムプロトコル、バックエンド統合など、低レベルのネットワーク制御が必要なアプリケーションに最適です。Android、iOS、macOS、Windows、Linuxをサポートしており、クロスプラットフォーム互換性を確保しています。Dartで構築されており、既存のFlutterプロジェクトにスムーズに統合できます。

用途

  • リアルタイムデバイス通信
  • カスタムプロトコルの実装
  • IoTアプリケーションネットワーキング
  • バックエンドサービスの統合
  • クロスプラットフォームネットワークアプリ

主な機能

  • TCP接続管理
  • メッセージの送信と受信
  • クロスプラットフォーム対応
  • 接続状態のモニタリング
  • ソケット操作のためのシンプルな API

適している対象

  • リアルタイムネットワークアプリを開発する開発者
  • IoTおよび組み込みシステムの統合
  • 直接的なTCP制御が必要なアプリケーション
  • 軽量RPCを必要とするプロジェクト
  • 複数のオペレーティングシステムをターゲットにするチーム

注意事項

  • Android/iOSでネットワーク権限が必要です
  • 接続切断のための手動エラー処理
  • 組み込みの暗号化なし(別途TLSを使用)
  • 成熟したパッケージと比べてドキュメントが限られている
  • HTTP のような高レベルなウェブプロトコルには適していません

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

tcp_socket_connection

This is a Flutter package that allows you to connect to a socket over the net. All data is then read using UTF-8.

Getting started

To use this plugin, add tcp_socket_connection as a dependency in your pubspec.yaml file.

Main Example

Here is shown an example where a connection is established and the received message is displayed in a Text widget.

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //Instantiating the class with the Ip and the PortNumber
  TcpSocketConnection socketConnection=TcpSocketConnection("192.168.1.113", 10251);

  String message="";

  @override
  void initState() {
    super.initState();
    startConnection();
  }

  //receiving and sending back a custom message
  void messageReceived(String msg){
    setState(() {
      message=msg;
    });
    socketConnection.sendMessage("MessageIsReceived :D ");
  }

  //starting the connection and listening to the socket asynchronously
  void startConnection() async{
    socketConnection.enableConsolePrint(true);    //use this to see in the console what's happening
    if(await socketConnection.canConnect(5000, attempts: 3)){   //check if it's possible to connect to the endpoint
      await socketConnection.connect(5000, messageReceived, attempts: 3);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(
          'You have received '+ message,
        ),
      ),
    );
  }
}

Usage

First of all declare a new instance of the TcpSocketConnection class:

TcpSocketConnection socketConnection=TcpSocketConnection(IP_ADDRESS, PORT_NUMBER);

Then, to establish a connection it's used the following method:

await socketConnection.connect(TIMEOUT, CALLBACK_FUNCTION);

The method connect allows you to set a TIMEOUT before finishing attempting to connect, a CALLBACK_FUNCTION that is created by the developer and takes a String as parameter. The CALLBACK_FUNCTION will be called when the message is received.

The word await is really important since the connection requires a little bit of time to be initialized. Write that line of code inside an async method to ensure it doesn't impact the UI.

To send a message use the following method:

socketConnection.sendMessage(MESSAGE);

In case you want to send a message with also an EOM you can directly use the following method:

socketConnection.sendMessageEOM(MESSAGE,EOM);

It will automatically append the EOM (End Of Message) at the end of the MESSAGE.

Examples

Imagine you are receiving a String from a server which states "Confirm?FINISH". In order to read it you have to listen to the server asynchronously until you receive the EOM (End of Message) to be sure that the message is finished. After that you have to split the String and keep only the part you need, in this case "Confirm?". This is all done by the connectEOM method in only 1 line of code!

await socketConnection.connectEOM(TIMEOUT, EOM, CALLBACK_FUNCTION, {ATTEMPTS=1});

Imagine you are receiving a String from a server. With the connect method you can read messages received (that probably need to be elaborated by you: the developer) in only 1 line of code!

await socketConnection.connect(TIMEOUT, CALLBACK_FUNCTION, {ATTEMPTS=1});

Last but not least

If you find this package useful, don't forget to leave a like! I appreciate it!

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。