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

套件詳情v0.6.0

flutter_socket_io

Flutter Socket IO 插件

近 30 天下載
158
喜歡數
66
Pub 分數
30

安裝套件

flutter pub add flutter_socket_io

套件介紹

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

一個 Flutter 插件,可實現 Flutter 應用與 Socket.IO 伺服器之間的即時雙向通訊。它簡化了與 Socket.IO 協定的整合,支援事件驅動的訊息傳遞、基於房間的廣播以及連接管理。此外,該插件專為需要即時更新的應用程式(例如聊天應用、協作工具或即時儀表板)設計。它可在跨平台環境中使用,並透過 Dart 繫結利用原始 Socket.IO JavaScript 庫的穩健性。

適用情境

  • 即時聊天應用
  • 即時資料流
  • 協作編輯工具
  • 在線遊戲
  • 物聯網裝置監控

主要特色

  • 雙向通訊
  • 事件驅動的消息傳遞
  • 房間和命名空間支援
  • 自動重連
  • 跨平台相容性

適合對象

  • 需要即時更新的應用
  • 熟悉 Socket.IO 的開發人員
  • 需要低延遲通訊的項目
  • 建構互動式網頁體驗

使用建議

  • 需要相容的 Socket.IO 伺服器
  • 可能需要為網路問題自訂錯誤處理
  • 依賴穩定的互聯網連接
  • 不適合高頻率、低延遲的微交易

原始 README

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

flutter_socket_io

Flutter Socket IO Plugin, supported Android + iOS (iOS installation guide is coming soon)

How to install on iOS

    1. Copy the folder example/ios/Runner/SocketObj to ${PROJECT_ROOT}/ios/Runner/
    1. Replace the example/ios/Runner/AppDelegate.m line with ${PROJECT_ROOT}/ios/Runner/AppDelegate.m. (Notice: You should merge the old one in your project to merge with the new from this plugin if you have some change on that file)
    1. Open ${PROJECT_ROOT}/ios/Podfile, paste this line pod 'Socket.IO-Client-Swift', '~> 13.3.0' before the end of target 'Runner' do block
    1. Run and Enjoy the plugin :)

Use the plugin

1. Add the following import to your Dart code:

import  'package:flutter_socket_io/flutter_socket_io.dart';

2. SocketIOManager: to manage (create/destroy) list of SocketIO

Create SocketIO with SocketIOManager:

SocketIO socketIO = SocketIOManager().createSocketIO("http://127.0.0.1:3000", "/chat", query: "userId=21031", socketStatusCallback: _socketStatus);  

Destroy SocketIO with socketIOManager:

SocketIOManager().destroySocket(socketIO);  

3. SocketIO:

Get Id (Url + Namespace) of the socket

String getId();


Create a new socket and connects the client

Future<void> connect();


Init socket before doing anything with socket

Future<void> init();


Subscribe to a channel with a callback

Future<void> subscribe(String event, Function callback); 


Unsubscribe from a channel. When no callback is provided, unsubscribe all subscribers of the channel. Otherwise, unsubscribe only the callback passed in

Future<void> unSubscribe(String event, [Function callback]); 


Send a message via a channel (i.e. event, the native code will convert string message to JsonObject before sending)

Future<void> sendMessage(String event, dynamic message, [Function callback]);


Disconnect from the socket

Future<void> disconnect(); 


Unsubscribe all subscribers from all channels

Future<void> unSubscribesAll();


4. Example: Link

SocketIO socketIO;
_connectSocket01() { 
	//update your domain before using  
	 socketIO = SocketIOManager().createSocketIO("http://127.0.0.1:3000", "/chat", query: "userId=21031", socketStatusCallback: _socketStatus); 

	//call init socket before doing anything 
	socketIO.init(); 

	//subscribe event
	socketIO.subscribe("socket_info", _onSocketInfo); 

	//connect socket 
	socketIO.connect(); 
}

_socketStatus(dynamic data) { 
	print("Socket status: " + data); 
}

_subscribes() { 
	if (socketIO != null) { 
		socketIO.subscribe("chat_direct", _onReceiveChatMessage); 
	} 
}

void _onReceiveChatMessage(dynamic message) { 
	print("Message from UFO: " + message); 
}

void _sendChatMessage(String msg) async { 
	if (socketIO != null) { 
		String jsonData = '{"message":{"type":"Text","content": ${(msg != null && msg.isNotEmpty) ? '"${msg}"' : '"Hello SOCKET IO PLUGIN :))"'},"owner":"589f10b9bbcd694aa570988d","avatar":"img/avatar-default.png"},"sender":{"userId":"589f10b9bbcd694aa570988d","first":"Ha","last":"Test 2","location":{"lat":10.792273999999999,"long":106.6430356,"accuracy":38,"regionId":null,"vendor":"gps","verticalAccuracy":null},"name":"Ha Test 2"},"receivers":["587e1147744c6260e2d3a4af"],"conversationId":"589f116612aa254aa4fef79f","name":null,"isAnonymous":null}'; 
		socketIO.sendMessage("chat_direct", jsonData, _onReceiveChatMessage); 
	}
 }

_destroySocket() { 
	if (socketIO != null) { 
		SocketIOManager().destroySocket(socketIO); 
	} 
}


所屬分類

原始碼儲存庫

使用此套件的專案