本文是对WebSocket进行了一些简单的封装,当然了,你们也可以自己动手。
看下效果图吧:
首先添加依赖:
web_socket_channel: ^1.1.0
然后我针对它区分了四种状态: 连接,连接中,关闭,关闭中。
基于StreamBuilder的状态管理方式实现的,如果不清楚StreamBuilder的话,可以先自行百度学习一下。
接下来就是一个管理类,主要方法都是非常常用的几个,我贴一下这个类吧:
import 'dart:async';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
enum StatusEnum{
connect,connecting,close,closing
}
class WebsocketManager{
static WebsocketManager _singleton;
WebSocketChannel channel;
factory WebsocketManager() {
return _singleton;
}
StreamController<StatusEnum> socketStatusController = StreamController<StatusEnum>();
WebsocketManager._();
static void init() async {
if (_singleton == null) {
_singleton = WebsocketManager._();
}
}
StatusEnum isConnect=StatusEnum.close ; //默认为未连接
String _url="ws://echo.websocket.org";
Future connect() async{
if(isConnect==StatusEnum.close){
isConnect=StatusEnum.connecting;
socketStatusController.add(StatusEnum.connecting);
channel=await IOWebSocketChannel.connect(Uri.parse(_url));
isConnect=StatusEnum.connect;
socketStatusController.add(StatusEnum.connect);
return true;
}
}
Future disconnect() async{
if(isConnect==StatusEnum.connect){
isConnect=StatusEnum.closing;
socketStatusController.add(StatusEnum.closing);
await channel.sink.close(3000,"主动关闭");
isConnect=StatusEnum.close;
socketStatusController.add(StatusEnum.close);
}
}
bool send(String text){
if(isConnect==StatusEnum.connect) {
channel.sink.add(text);
return true;
}
return false;
}
void printStatus(){
if(isConnect==StatusEnum.connect){
print("websocket 已连接");
}else if(isConnect==StatusEnum.connecting){
print("websocket 连接中");
}else if(isConnect==StatusEnum.close){
print("websocket 已关闭");
}else if(isConnect==StatusEnum.closing){
print("websocket 关闭中");
}
}
void dispose(){
socketStatusController.close();
socketStatusController=null;
}
}
通过这个管理类基本上可以开始进行打开,关闭,发送这几项基础功能了,我把websocket的四种状态也是通过StreamBuilder的方式管理了一下,接下来看一下我的实际案例吧:
import 'package:flutter/material.dart';
import 'package:flutter_websocket/WebsocketManager.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
//初始化
WebsocketManager.init();
}
@override
void dispose() {
WebsocketManager().dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("如何使用Flutter WebSocket?"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed:() => _send() ,
child: Text("发送"),
),
RaisedButton(
onPressed:() => _open() ,
child: Text("打开websocket连接"),
),
RaisedButton(
onPressed:() => _close() ,
child: Text("关闭websocket连接"),
),
RaisedButton(
onPressed:() => _reconnect() ,
child: Text("重连websocket连接"),
),
StreamBuilder<StatusEnum>(
builder: (context, snapshot) {
if (snapshot.data==StatusEnum.connect){
return StreamBuilder(
builder: (context, snapshot2) {
if(snapshot2.hasData){
return Container(
child: Text("收到的消息: ${snapshot2.data}"
),
);
}else if(snapshot2.hasError){//websocket发生错误 (这里未做测试)
_reconnect();//重连
}
return Text("已连接,还没数据呢");
},
stream: WebsocketManager().channel.stream,
);
}else if(snapshot.data==StatusEnum.connecting){
return Text("连接中");
}else if(snapshot.data==StatusEnum.close){
return Text("已关闭");
}else if(snapshot.data==StatusEnum.closing){
return Text("关闭中");
}
},
initialData: StatusEnum.close,
stream:WebsocketManager().socketStatusController.stream,
)
],
),
),
);
}
_open()async {
await WebsocketManager().connect();
}
_close() async{
await WebsocketManager().disconnect();
}
int i=0;
_send() {
WebsocketManager().send("哈哈${i+=1}");
}
_reconnect() async{
await _close();
await _open();
}
}
基本上案例已经实现了打开,关闭,重连等等。
欢迎留言评论,互相伤害。