搜尋套件、儲存庫與 APP 分類

套件詳情v1.0.1+1

adhara_socket_io

由 adhara 提供的適用於 Flutter 的 socket.io WebSocket,支援 iOS 與 Android

近 30 天下載
110
喜歡數
86
Pub 分數
30

安裝套件

flutter pub add adhara_socket_io

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Adhara Socket.IO 是一個 Flutter 套件,可使用 Socket.IO 協定實現 WebSocket 通訊,支援 iOS 與 Android 平台。由 Infitio 所開發,提供一種可靠且高效的方式,於 Flutter 應用中實作即時功能。此套件簡化了連線管理、事件處理與訊息廣播,非常適合需要即時更新的應用程式,例如即時聊天系統、即時通知或協作工具。它能與現有的 Socket.IO 伺服器無縫整合,並為開發者提供乾淨、符合 Dart 風格的 API。

適用情境

  • 即時聊天應用
  • 即時資料流
  • 協作編輯工具
  • 物聯網裝置監控
  • 推送通知

主要特色

  • 支援 Socket.IO 協定
  • 跨平台(iOS 與 Android)
  • 簡單的事件驅動 API
  • 自動重連
  • 輕便且快速

適合對象

  • 開發即時 Flutter 應用的開發者
  • 使用 Socket.IO 伺服器的團隊
  • 需要即時數據同步的應用

使用建議

  • 需要相容的 Socket.IO 伺服器
  • 限制為基於 WebSocket 的通訊
  • 無內建加密(使用 HTTPS)

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

adhara_socket_io

.github/workflows/flutter_integration.yaml

socket.io for flutter by adhara

supports both Android and iOS

socket.io version supported: v2 development in progress for v3 and v4

If you are using v3/v4 socket on server side, you may face connection issues, please downgrade and try in such scenario.

Usage:

See example/lib/main.dart for more detailed example

	final SOCKET_SERVER = 'http://192.168.1.2:7070/';	//To be modified accordingly
        SocketIO socket;
	StreamSubscription connectSubscription;
	StreamSubscription echoSubscription;

	Future<void> demonstrateSocket() async {
    	// Create a socket instance
		socket = await SocketIOManager().createInstance(
        	SocketOptions(SOCKET_SERVER),
        );

	// Listen to socket connect event
	subscription = socket.onConnect.listen((data){
	  print('connected: $data');
	  socket.emit('message', ['Hello world!']);
	});

        // Listen to an custom ("news") event
	echoSubscription = socket.on('echo', (data){
  	    print("news event recieved with data: $data");
	});

	// There are 2 ways to connect to socket server
	//  - normal: doesn't wait for connectio success
	//  - sync: ensures connection or errors out on failure

        // normal:
        //  connect to socket server - will initialize connection,
        //  but not ensure the connection yet.
        //  If this method used to connect to server, then emit events should be sent
        //  only after ensuring connection to socket server is successful by listening
        //  to onConnect events
	// await socket.connect();

	// sync:
        //  This API will ensure connection to server is successful
        //  or will throw error on connect error
        await socket.connectSync();

        // publish data - will publish to server, won't ensure the delivery
	await socket.emit('echo', ['hello']);

        // emit with acknowledgement - will publish to server
        //  and ensure delivery with ack if ack is implemented in server
        dynamic ackData = await socket.emitWithAck('echo', ['hello']);
            print('acknowledgement recieved from server: $ackData');
    	}

	Future<void> dispose() async {
	    // cancel echo and onConnect subscriptions
	    await echoSubscription.cancel();
	    await connectSubscription.cancel();

            // clear socket instance from manager
            await SocketIOManager().clearInstance(socket);
        }

	// register liteners, connect to a socket, and publish data
	demonstrateSocket();

	// will dispose listeners and socket
	dispose();

Running example:

  1. clone the project
  2. start socket server in the background
cd socket.io.server
npm i
./node_modules/.bin/pm2/ index.js
cd ../
  1. open example/lib/main.dart and edit the URI in #7 to point to your hosted/local socket server instances as mentioned step 2

    For example:

    const String URI = "http://192.168.1.2:7000/";
    
    const String URI = "http://mysite.com/";
    
  2. run example

cd example
flutter run

iOS support 📢📢

This project uses Swift for iOS support, please enable Swift support for your project for this plugin to work

Android support for SDK > 27

Configure android:usesCleartextTraffic="true" as a property of <application ...> tag in android/app/src/main/AndroidManifest.xml

For example:


<application
        android:name="io.flutter.app.FlutterApplication"
        android:label="adhara_socket_io_example"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"...>...</activity>
        ...
</application>

Refer to discussion here

Running tests

This plugin uses flutter driver to run integration tests tests. Use below command to run integration tests on Android/iOS

sh bin/run_tests.sh

Sample Video - Running the example

Running adhara socket io for flutter, example

FAQ's

AdharaSocketIoPlugin.m:2:9: fatal error: 'adhara_socket_io/adhara_socket_io-Swift.h' file not found

add use_frameworks! to your Podfile as in the example https://github.com/infitio/flutter_socket_io/blob/master/example/ios/Podfile#L30

Read more about this: discussion

Other Packages:

Feel free to checkout our Adhara package

所屬分類

依賴

依賴清單 1 項

  • pedantic^1.9.2

原始碼儲存庫

使用此套件的專案

暫無相關資料。