uni-app websocket的服务请求使用

<template>
  <view>
    <page-head title="websocket通讯示例"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-btn-v">
        <view><textarea value="" v-model="txtinfo" placeholder="" class="txtarea" /></view>
        <view class="websocket-msg">{{ showMsg }}</view>
        <button type="primary" @click="connect">连接websocket服务</button>
        <button v-show="connected" type="primary" @click="send">发送一条消息</button>
        <button type="primary" @click="close">断开websocket服务</button>
        <view class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</view>
      </view>
    </view>
  </view>
</template>

<script>
let platform = uni.getSystemInfoSync().platform; //手机类型
console.log(platform);
export default {
  data() {
    return {
      connected: false,
      connecting: false,
      socketTask: false,
      msg: false,
      txtinfo:""
    };
  },
  onLoad() {
    // 获取系统信息
    console.log(this.txtinfo);
  },
  computed: {
    showMsg() {
      if (this.connected) {
        if (this.msg) {
          return '收到消息:' + this.msg;
        } else {
        }
      } else {
        return '尚未连接';
      }
    }
  },
  onUnload() {
    uni.hideLoading();
    if (this.socketTask && this.socketTask.close) {
      this.socketTask.close();
    }
  },
  methods: {
    connect() {
      if (this.connected || this.connecting) {
        uni.showModal({
          content: '正在连接或者已经连接,请勿重复连接',
          showCancel: false
        });
        return false;
      }
      this.connecting = true;
      uni.showLoading({
        title: '连接中...'
      });
      this.socketTask = uni.connectSocket({
        url: 'wss://echo.websocket.org',
        data() {
          return {
            msg: 'Hello'
          };
        },
        // #ifdef MP
        header: {
          'content-type': 'application/json'
        },
        // #endif
        // #ifdef MP-WEIXIN
        method: 'GET',
        // #endif
        success(res) {
          // 这里是接口调用成功的回调,不是连接成功的回调,请注意
          console.log(res);
        },
        fail(err) {
          // 这里是接口调用失败的回调,不是连接失败的回调,请注意
        }
      });
      console.log(this.socketTask);
      this.socketTask.onOpen(res => {
        this.connecting = false;
        this.connected = true;
        uni.hideLoading();
        uni.showToast({
          icon: 'none',
          title: '连接成功'
        });
        console.log('onOpen', res);
      });
      this.socketTask.onError(err => {
        this.connecting = false;
        this.connected = false;
        uni.hideLoading();
        uni.showModal({
          content: '连接失败,可能是websocket服务不可用,请稍后再试',
          showCancel: false
        });
        console.log('onError', err);
      });
      this.socketTask.onMessage(res => {
        this.msg = res.data;
        console.log('onMessage', res);
      });
      this.socketTask.onClose(res => {
        this.connected = false;
        this.startRecive = false;
        this.socketTask = false;
        this.msg = false;
        console.log('onClose', res);
      });
      console.log('task', this.socketTask);
    },
    send() {
       console.log(this.txtinfo);
      this.socketTask.send({
        data: '来自' + platform + ' : ' + this.txtinfo,
        success(res) {
          console.log(res);
        },
        fail(err) {
          console.log(err);
        }
      });
    },
    close() {
      if (this.socketTask && this.socketTask.close) {
        this.socketTask.close();
      }
    }
  }
};
</script>

<style>
.txtarea {
  width: 100%;
  height: 150px;
  background-color: #646566;
  text-indent: 10px;
  padding-top: 10px;
  color: #ffffff;
  font-size: 14px;
  letter-spacing: 1px;
  font-family: '黑体';
  line-height: 25px;
  padding-left: 10px;
  padding-right: 10px;
}
.uni-btn-v {
  padding: 10rpx 0;
}

.uni-btn-v button {
  margin: 20rpx 0;
}

.websocket-msg {
  padding: 40px 0px;
  text-align: center;
  font-size: 14px;
  line-height: 40px;
  color: #666666;
}

.websocket-tips {
  padding: 40px 0px;
  text-align: center;
  font-size: 14px;
  line-height: 24px;
  color: #666666;
}
</style>

效果如下:

在这里插入图片描述
连接:

在这里插入图片描述

输入信息并发送信息:

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值