当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 2 Fork 0

OkCoder / uni-socket
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

uni-socket

介绍

uni-app的socket完全封装

之所以写这个类,是因为之前用别人的插件总是出现莫名其妙死掉,比如黑屏截屏等操作后就挂了,现在封装的这个目前运行良好,请大家检测

特点:

  1. 断线重连
  2. 自定义心跳
  3. 网络检测
  4. 断线消息队列
  5. debug
  6. 支持typescript

安装教程

npm i @i5920/uni-socket -S

使用说明

import UniSocket from "@i5920/uni-socket";

export default {
  onLaunch() {
    this.globalData.socket = new UniSocket({
        url: "ws://127.0.0.1:8080",
        timeout: 1 * 60 * 1000,   // 心跳时间间隔
        isSendHeart: true,        // 是否发送心跳
        heartData: "ping",        // 心跳数据
        isReconnection:true,      // 是否断线重连
        reConnectTime: 3000,      // 断线重连检测时间间隔
        params: {                 // 发送消息时如果时json则自动加上组合里面参数
          // token: this.$store.state.user.TOKEN
        },
        debug: process.env.NODE_ENV === "development",  // debug
        onSocketOpen: header => {},
        onSocketMessage: data => {},
        onSocketError:res => {},
        onSocketClose:res => {}
    });
  },
  // on Show 在uniapp插件市场不显示?
  // 必须在这里调用initSocket方法
  on Show() {
    // 连接socket
    this.globalData.socket.initSocket();
    // 登录
    this.loginDemo();
  },
  methods:{
    // 发送socket消息
    loginDemo(){
      this.globalData.socket.sendSocketMessage({
        event:"login",
        data:{
          "username":"okcoder",
          "password":"666666"
        }
      });
    },
    // 主动关闭socket
    closeSocket(){
      this.globalData.socket.closeSocket();
    }
  }
}

// 其它页面
getApp().globalData.socket.sendSocketMessage({
  event:"login",
  data:{
    "username":"okcoder",
    "password":"666666"
  }
});

赞助二维码

更新说明

  • v2.0.1
    1. socket错误执行断线重连
MIT License Copyright (c) 2019 OkCoder Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

停止维护! 推荐使用https://gitee.com/okcoder/uni-socket-promise 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/okcoder/uni-socket.git
git@gitee.com:okcoder/uni-socket.git
okcoder
uni-socket
uni-socket
master

搜索帮助