1 Star 1 Fork 0

OkCoder / uni-socket

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

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错误执行断线重连

Comments ( 0 )

Sign in for post a comment

About

停止维护! 推荐使用https://gitee.com/okcoder/uni-socket-promise spread retract
JavaScript
MIT
Cancel

Releases

No release

Gitee Metrics

Contributors

All

Activities

load more
can not load any more
JavaScript
1
https://git.oschina.net/okcoder/uni-socket.git
git@git.oschina.net:okcoder/uni-socket.git
okcoder
uni-socket
uni-socket
master

Search