简要
这篇文章主要讲解在flutter中使用socket与服务器进行基于protobuf协议进行通信的基本方法包含protobuf的基本使用,数据编码的方法,以及socket建立,通信的过程
阅读时可能需要你事先对flutter,protobuf有基本了解
flutter的基本操作可以在flutter中文网中找到
在flutter中protobuf的导入
- 在pubspec中添加 然后更新
dependencies:
flutter:
sdk: flutter
// 引入protobuf
protobuf: ^0.13.4
- 使用pb的dart插件生成你的模型文件
过程简单请百度一下
举例flutter中socket的基本使用
- 引入头文件 创建地址和端口
import 'dart:io';
//地址
const String adress = '你的服务器地址';
//端口
const int port = 你的服务器端口;
- 设置一个管理类
class Client {
//socket实例
Socket _socket;
//数据接收组
var _recList;
//单例方法
factory Client() =>_getInstance();
static Client get instance => _getInstance();
static Client _instance;
Client._internal() {
// 初始化
}
static Client _getInstance() {
if (_instance == null) {
_instance = new Client._internal()