搜索插件、代码仓与 APP 分类

插件详情v0.2.2

websocket_manager

一个适用于 Android 和 iOS 的 Flutter 插件,支持 WebSocket 连接

近 30 天下载
49
喜欢数
44
Pub 分数
40

安装插件

flutter pub add websocket_manager

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个专为 Android 和 iOS 设计的 Flutter 插件,可实现无缝的 WebSocket 连接。它提供了一种可靠且高效的方式,用于在应用程序和服务器之间保持实时通信,支持双向数据传输。该包简化了连接管理,包括自动重连和消息处理,非常适合实时更新、聊天应用和实时通知。该插件注重简洁性和性能,可轻松集成到 Flutter 项目中。

适合场景

  • 实时聊天应用
  • 实时数据流
  • 推送通知系统
  • 物联网设备通信
  • 在线游戏功能

主要特点

  • 断开连接后自动重连
  • 双向消息交换
  • 连接管理的简单API
  • 对 JSON 和二进制消息的支持
  • 内置错误处理

适合对象

  • 开发实时应用的开发者
  • 需要持久服务器连接的应用程序
  • 需要低延迟通信的项目

使用建议

  • 仅限 Android 和 iOS 平台
  • 无内置加密(需要外部设置)
  • 依赖稳定的网络连接

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。


NOTE: This repository is no longer maintained by ENGAPP. It was moved to be maintained by Kunlatek on the following link: https://github.com/kunlatek/flutter_websocket_manager_plugin

Any issue regarding this plugin should be reported there.


Websocket Manager

A Flutter plugin for Android and iOS supports websockets. This plugin is based on two different native libraries Starscream for iOS and okHttp for Android.

This plugin was created due to our necessity to maintain a WebSocket connection active in background while Flutter's WebSocket from cookbook doesn't keep alive while screen is locked or the application was in background.

Introduction

Websocket Manager doesn't manipulate websockets in Dart codes directly, instead, the plugin uses Platform Channel to expose Dart APIs that Flutter application can use to communicate with two very powerful websocket native libraries. Because of that, all credits belong to these libraries.

How to install

Android

You only need this configuration if your server doesn't have SSL/TLS

Since Android P http is blocked by default and there are many ways to configure. One way to configure is explicitly saying that you accept clear text for some host.

  • Create res/xml/network_security_config.xml with content:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your_domain</domain>
    </domain-config>
</network-security-config>
  • Point to this file from your manifest (for bonus points add it only for the test manifest):
<application
    android:networkSecurityConfig="@xml/network_security_config"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity android:name=" (...)
</application>
iOS

Doesn't require any configuration

Example

int messageNum = 0;
// Configure WebSocket url
final socket = WebsocketManager('wss://echo.websocket.org');
// Listen to close message
socket.onClose((dynamic message) {
    print('close');
});
// Listen to server messages
socket.onMessage((dynamic message) {
    print('recv: $message');
    if messageNum == 10 {
        socket.close();
    } else {
        messageNum += 1;
        final String msg = '$messageNum: ${DateTime.now()}';
        print('send: $msg');
        socket.send(msg);
    }
});
// Connect to server
socket.connect();

Credits

所属分类

源码仓库

使用此插件的项目

暂无相关数据。