前言
本篇文章主要是记录笔者在实际开发中,关于flutter 中基于socket的业务功能开发,主要包括:
socket的 连接/断开
网络监听与重连机制
自定义拆包,封包以及黏包处理
业务开发中的套接字简单数据结构
|::::::Head:::::::| :::::::Body ::::::|Head:这里主要描述了Body 的长度,一个 4byte 的int 类型;Body:这个部分还有很多结构字段,这里只是简单表示一下;
dart:io
File, socket, HTTP, and other I/O support for non-web applications.
Important: Browser-based applications can't use this library. Only servers, command-line scripts, and Flutter mobile apps can import and use dart:io.
This library allows you to work with files, directories, sockets, processes, HTTP servers and clients, and more. Many operations related to input and output are asynchronous and are handled using Futures or Streams, both of which are defined in the dart:async library.
从官方文档中,我们可以看出dart:io在flutter中的重要地位。这里我们主要用dart:io下的socket模块。下面我们看下socket模块下都有些什么:
WebSocket
WebSocket类提供对Web套接字协议的支持。 这允许客户端和服务器应用程序之间的全双工通信。Web套接字服务器使用普通的HTTP服务器来接受Web套接字连接。 初始握手是HTTP请求,然后将其升级为Web套接字连接。 服务器使用WebSocketTransformer升级请求,并侦听返回的Web套接字上的数据。 例如,这是一个微型服务器,用于监听WebSocket上的“ ws”数据:
runZoned(() async {
var server = await HttpServer.bind('127.0.0.1', 8090);
server.listen((HttpRequest req) async {
if (网页链接 == '/ws') {
var socket = await WebSocketTransformer.upgrade(req);
socket.listen(handleMsg);
}
});
}, onError: (e) => print("An error occurred."));
客户端使用connect()方法和使用Web套接字协议的URI连接到WebSocket。 客户端可以使用add()方法写入WebSocket。
![](http://tiebapic.baidu.com/forum/w%3D580/sign=e1db70fec48ba61edfeec827713597cc/a432cbfc1e178a82e9ef0994b003738da877e8c3.jpg?tbpicau=2025-02-28-05_6de6af559569ec267adb6776f41cf9b0)
本篇文章主要是记录笔者在实际开发中,关于flutter 中基于socket的业务功能开发,主要包括:
socket的 连接/断开
网络监听与重连机制
自定义拆包,封包以及黏包处理
业务开发中的套接字简单数据结构
|::::::Head:::::::| :::::::Body ::::::|Head:这里主要描述了Body 的长度,一个 4byte 的int 类型;Body:这个部分还有很多结构字段,这里只是简单表示一下;
dart:io
File, socket, HTTP, and other I/O support for non-web applications.
Important: Browser-based applications can't use this library. Only servers, command-line scripts, and Flutter mobile apps can import and use dart:io.
This library allows you to work with files, directories, sockets, processes, HTTP servers and clients, and more. Many operations related to input and output are asynchronous and are handled using Futures or Streams, both of which are defined in the dart:async library.
从官方文档中,我们可以看出dart:io在flutter中的重要地位。这里我们主要用dart:io下的socket模块。下面我们看下socket模块下都有些什么:
WebSocket
WebSocket类提供对Web套接字协议的支持。 这允许客户端和服务器应用程序之间的全双工通信。Web套接字服务器使用普通的HTTP服务器来接受Web套接字连接。 初始握手是HTTP请求,然后将其升级为Web套接字连接。 服务器使用WebSocketTransformer升级请求,并侦听返回的Web套接字上的数据。 例如,这是一个微型服务器,用于监听WebSocket上的“ ws”数据:
runZoned(() async {
var server = await HttpServer.bind('127.0.0.1', 8090);
server.listen((HttpRequest req) async {
if (网页链接 == '/ws') {
var socket = await WebSocketTransformer.upgrade(req);
socket.listen(handleMsg);
}
});
}, onError: (e) => print("An error occurred."));
客户端使用connect()方法和使用Web套接字协议的URI连接到WebSocket。 客户端可以使用add()方法写入WebSocket。
![](http://tiebapic.baidu.com/forum/w%3D580/sign=e1db70fec48ba61edfeec827713597cc/a432cbfc1e178a82e9ef0994b003738da877e8c3.jpg?tbpicau=2025-02-28-05_6de6af559569ec267adb6776f41cf9b0)