FlutterSocket是一个实用的跨平台socket插件,目前已经实现client端的基本功能:创建,连接,发送消息,收消息,断开连接等功能...
video url: https://www.youtube.com/watch?v=fmtwYHeOvE0
dependencies:
flutter_socket_plugin: lastest version
import 'package:flutter_socket_plugin/flutter_socket_plugin.dart';
///
/// @Method: initSocket
/// @Parameter:
/// @ReturnType:
/// @Description: init socket
/// @author: waitwalker
/// @Date: 2019-08-23
///
initSocket() {
/// init socket
flutterSocket = FlutterSocket();
/// listen connect callback
flutterSocket.connectListener((data){
print("connect listener data:$data");
});
/// listen error callback
flutterSocket.errorListener((data){
print("error listener data:$data");
});
/// listen receive callback
flutterSocket.receiveListener((data){
print("receive listener data:$data");
if (data != null) {
receiveMessage = receiveMessage + "\n" + data;
}
setState(() {
});
});
/// listen disconnect callback
flutterSocket.disconnectListener((data){
print("disconnect listener data:$data");
});
}
创建 socket
:
await flutterSocket.createSocket("192.168.8.120", 10007, timeout: 20);
发起连接 connect
:
flutterSocket.tryConnect();
发送消息 send
:
flutterSocket.send(textEditingController.text);
断开连接 disconnect
:
flutterSocket.tryDisconnect();
你可以在这里查看FlutterSocket的全部示例.
FlutterSocket目前实现了客户端基本String消息的收发,未来在条件允许的情况下,逐步完善功能,如果你急需一些功能,你可以在FlutterSocket的基础上继续添加功能,让FlutterSocket功能更加强大和完善!
此项目为完全开源 ,license 是 MIT. 如果您喜欢,欢迎star.
Please file feature requests and bugs at the issue tracker.
作者微信:dbzy_duzhao