小工具知识网

当前位置: 首页 > 行业知识 > uniapp 小程序AP配网

行业知识

uniapp 小程序AP配网

2026-11-15 07:38:14 来源:互联网转载

一、TCPSocket.js 封装TCP协议

()
class socket {  constructor() {    this.connection = {};  }  // 创建一个TCP实例  establish(monitor) {    this.connection = wx.createTCPSocket();    this.connection.connect({ address: "000.000.0.0", port: 6800 });  }  // 发送消息  connect(message) {    this.connection.onConnect(() => {      this.connection.write(message);    });  }  // 监听端口函数  onMessage(success, failure) {    this.connection.onMessage((res) => {      success(res)    });    this.connection.onError((err) => {      failure(err);    });  }  // 关闭搜索事件  TCPclose() {    setTimeout(() => {      // 3S后,关闭socket      this.connection.close(() => {        console.log("TCP关闭");      });    }, 3000);  }}const TCPSocket = new socket();export default TCPSocket;

 二、vue文件中使用TCP

                    {{ wifiSSID }}                                    {{ loadingWIfi ? "连接中" : "连接" }}          import TCP from "@/common/util/TCPSocket.js";export default {  data() {    return {      wifiSSID: "WIFI name",      password: "",      isFirst: true,      wifiCountInterval: null,      wifiCountDown: 0,      loadingWIfi: false,    };  },  onHide() {    this.clearInterval();  },  methods: {    settiing() {      this.loadingWIfi = true;      this.onConfirm();    },    onConfirm() {      this.wifiCountDown = 60;      this.startSMSTimer();      if (this.isFirst) {        // TCP实例创建        // 连接端口        TCP.establish();        this.isFirst = false;      }      // 请求设备上报状态      this.udpsend();      // 开始监听      TCP.onMessage(        (res) => {          if (!res) return;          // 关闭TCP连接          TCP.TCPclose();          console.log(res) // 查看返回数据                  },        (err) => {          // debug          console.log(err, "err");        }      );    },    udpsend() {      const msgData = {        event: "network",        data: {          ssid: this.wifiSSID,          password: this.password,        },      };      TCP.connect(JSON.stringify(msgData));    },    startSMSTimer() {      this.wifiCountInterval = setInterval(() => {        this.wifiCountDown--;        if (this.wifiCountDown 

上一篇:30个HTML+CSS前端开发案例(完结篇)

下一篇:华为服务器供暖怎么开?