如何使用Flutter WebSocket

2020-09-29 11:01:16

本文是對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();
  }
}

基本上案例已經實現了開啟,關閉,重連等等。

歡迎留言評論,互相傷害。