9***@qq.com
9***@qq.com
  • 发布:2024-05-06 13:55
  • 更新:2024-05-06 13:55
  • 阅读:168

使用websocket没能与unity中的webserver通信,onopen函数没有执行过

分类:HBuilderX

手机这边的功能就是读取位置信息发送给电脑那边的unity中的webserver,测试过那个webserver是可以收信息的,感觉是uniapp这边的信息没发出去,onopen函数没有输出过,两个设备是在一个局域网的,目的地址的ip应该也没错,但是不知道为什么信息发不出去
代码段如下

 created() {  
    this.websocket = uni.connectSocket({  
      url: 'ws:192.168.137.1:16468',  // 电脑 IP 和端口  
      success: () => console.log('WebSocket 连接成功!'),  
      fail: (err) => console.error('WebSocket 连接失败!', err)  
    });  

    this.websocket.onOpen(() => {  
      console.log('WebSocket 已经打开!');  
      this.startSendingData();  
    });  

    this.websocket.onError((err) => {  
      console.error('WebSocket 出错:', JSON.stringify(err));  
    });  
  },  

  startSendingData() {  
    uni.onAccelerometerChange((res) => {  
      if (this.websocket) {  
        let message = JSON.stringify({  
          x: res.x,  
          y: res.y,  
          z: res.z  
        });  
        this.websocket.send({  
          data: message,  
          fail: err => console.error('发送数据失败', err)  
        });  
      }  
    });  
  },
2024-05-06 13:55 负责人:无 分享
已邀请:

要回复问题请先登录注册