一个允许开发者为他们的 Flutter Android 应用开发 shiSock 客户端的包。使用 flutter-shiSock,开发者可以非常轻松地开发一个聊天应用程序。它也可以用于需要实时数据流的应用。

特点

它易于设置。

它可以用于各种应用程序,如聊天、天气预报等。

shiSock Engine 在处理数百万并发用户时具有非常低的延迟。

入门

根据您的环境运行这些命令。

使用 Dart

$ dart pub add flutter_shisock

使用 Flutter

在项目根目录的终端中运行此命令

$ flutter pub add flutter_shiSock

或者

您也可以直接将此行添加到您项目 pubspec.yml 文件中的 dependencies 部分

    dependencies:
        flutter_shiSock: ^1.0.0

用法

一个极简聊天应用程序的示例。

在此示例中,如果您的 shiSock Engine 和服务器在本地运行,那么 Android 模拟器的 localhost 将是 10.0.2.2。如果 shiSock Engine 在云端运行,您需要提供运行 Engine 的设备的公共 IP。

import 'package:flutter/material.dart';
import 'package:shisock_flutter/shisock_flutter.dart';

// main function or the starting point
void main() {
  runApp(MyApp());
}


// The main class for the flutter application and it is the root 
// of you application.
class MyApp extends StatelessWidget {

  // The declaration and initialisation of the shiSockClient object.
  ShiSockClient client = ShiSockClient();

  // If running on Android Emulator, 10.0.2.2 will the localhost
  String address = "10.0.2.2";
  int port = 8080;

  // shiSockListener object which will be used later in the application,
  // that's why it declared using late keyword.
  late ShiSockListener shiSock;

  // The construction of class. It is used to initialise the shiSock variable.
  MyApp({Key? key}) : super(key: key) {

    // Initialisation of shiSock variable using client' connect function.
    // connect function return a shiSockListener object.
    shiSock = client.connect(address, port);
  }

  // Root build function of the application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(shiSock: shiSock),
    );
  }
}

// A stateful widget class becouse there will the changes in the application UI
// on the fly.
class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.shiSock}) : super(key: key);
  final ShiSockListener shiSock;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

// A simple UI class.
class _MyHomePageState extends State<MyHomePage> {
  final myController = TextEditingController();
  List<Map<String, String>> msg = [];
  void listener() {
    widget.shiSock.listen("main", (data, sender) {
      setState(() {
        msg.add(data);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    listener();
    return Scaffold(
      appBar: AppBar(
        title: const Text("shiSock Test"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              flex: 9,
              child: ListView.builder(
                itemCount: msg.length,
                padding: const EdgeInsets.only(top: 10, bottom: 10),
                physics: const BouncingScrollPhysics(),
                itemBuilder: (context, index) {
                  String data = msg[index]["data"] ?? "";
                  String dt = msg[index]["datetime"] ?? "";
                  String datetime = dt.substring(0, 19);
                  return Container(
                    decoration: const BoxDecoration(
                        color: Color.fromARGB(255, 211, 220, 215),
                        borderRadius: BorderRadius.all(Radius.circular(20))),
                    margin: const EdgeInsets.only(
                        top: 2.5, bottom: 2.5, left: 5, right: 5),
                    child: ListTile(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      title: Text(data),
                      trailing: Text(datetime,
                          style: const TextStyle(
                              fontSize: 12,
                              color: Color.fromARGB(255, 192, 48, 217))),
                      textColor: const Color.fromARGB(255, 227, 154, 7),
                    ),
                  );
                },
              ),
            ),
            Expanded(
              flex: 1,
              child: Row(
                children: [
                  const SizedBox(
                    width: 4,
                  ),
                  Expanded(
                      flex: 1,
                      child: FloatingActionButton(
                          onPressed: () {
                            // ignore: avoid_print
                            print("Left Button");
                          },
                          child: const Icon(Icons.emoji_emotions_rounded))),
                  const SizedBox(
                    width: 4,
                  ),
                  Expanded(
                    flex: 8,
                    child: TextField(
                      controller: myController,
                      decoration: const InputDecoration(
                        border: OutlineInputBorder(
                            borderRadius:
                                BorderRadius.all(Radius.circular(20.0))),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 4,
                  ),
                  Expanded(
                    flex: 1,
                    child: FloatingActionButton(
                      onPressed: () {
                        widget.shiSock.send("main", myController.text);
                        myController.clear();
                      },
                      tooltip: 'send the content of text field to the server',
                      child: const Icon(Icons.send),
                    ),
                  ),
                  const SizedBox(
                    width: 4,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

附加信息

如果您需要更多信息,请查看 shiSock 网站:https:shisock.live/flutter_shisock

如果有人想为这个项目做出贡献,我们非常欢迎。

如果您对这个项目有任何问题/错误,请在官方 GitHub 仓库中提交一个 issue。

如果有人需要我的帮助,请在这些社交平台上联系我

LinkdeIN Instagram

Gmail

GitHub

查看 Github