socket配置

在全局配置(根目录/config/index.js)中配置相关sokect接口地址
路径:根目录/config/index.js

  1. eg: 请更换自己的域名和端口号
  2. const LIVESOKECT = 'wss://xxxx:8273/';
  3. 域名:xxxx
  4. 端口号:8273

使用说明

  1. # /根目录/utils/websocket.js 基于uni-app封装,有断线重连、心跳检测、发送消息等功能
  2. import wsRequest from '@/utils/websocket'
  3. let websocket = new wsRequest(6000)
  4. Vue.prototype.$socket = websocket
  5. // 链接初始化
  6. Vue.prototype.$socket = new wsRequest(6000)
  7. // 断线重连
  8. this.$socket.manualReconnect()
  9. // 监听消息页面
  10. uni.onSocketMessage(res => {
  11. const e = JSON.parse(res.data)
  12. if (e.type !== 'pong') {
  13. // 业务逻辑
  14. }
  15. })
  16. // 发送消息
  17. uni.sendSocketMessage({
  18. data: JSON.stringify({
  19. })
  20. })
  21. // 断开链接
  22. this.$socket.colse()