1***@qq.com
1***@qq.com
  • 发布:2023-08-15 11:48
  • 更新:2023-08-15 14:53
  • 阅读:138

【报Bug】在uts android 插件中调用 new MulticastSocket(port) ,在Hbuilder 3.8.7版本得到的值有异常

分类:HBuilderX

产品分类: HbuilderX

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 19045.3208

HBuilderX版本号: 3.8.7

示例代码:

uts android 插件中(uni_modules/uts-udp/utssdk/app-android/index.uts)

import MulticastSocket from 'java.net.MulticastSocket'  
....  
export class UDPMulticast {  
        private group: string  
    private port: Int  
    private socket: MulticastSocket | null = null  
    private isListener = false  

    constructor(group: string, port: Int) {  
        this.group = group;  
        this.port = port;  
    }  
       listener(receive:(msg:string)=>void){  
            this.socket  = new MulticastSocket(this.port);  
            console.log('this.socket',this.socket);// -----这里有疑问  
            ....  
            receive('hhh')  
       }  
}

vue页面中(pages/index/index.vue)

import { UDPMulticast } from '../../uni_modules/uts-udp'  
export default {  
methods:{  
initUdp(){  
this.server = new UDPMulticast(MULTICAST_IP, MULTICAST_PORT)  
                this.server.listener((res) => {  
                    console.log("res",res);  
                })  
}  
}  
}

操作步骤:

在vue页面调用initUdp

预期结果:

在Hbuilder3.8.7版本运行,打印出:this.socket, java.net.MulticastSocket@3144db1 // 类似这样的值

实际结果:

在Hbuilder3.8.7版本运行,打印出:this.socket, {}

bug描述:

在Hbuilder3.8.7版本,在页面调用下面的uts插件后,得到的this.socket 是空对象 {},
但在Hbuilder3.7.11版本,得到的this.socket 是类似这样的:java.net.MulticastSocket@3144db1

在Hbuilder3.7.11版本下,得到this.socket 后,我可以receive(packet), 并且都正常运行,对比3.8.7后,发现是new MulticastSocket(port)的值有差异,不知这种情况是否是uts编译问题?请帮忙解答一下,谢谢

2023-08-15 11:48 负责人:无 分享
已邀请:
DCloud_Android_DQQ

DCloud_Android_DQQ

需要确认一下:3.8.7 功能是否正常

如果只是 console.log 打印规则变化,那么可以忽略

  • 1***@qq.com (作者)

    我重新测试了,确实不是new MulticastSocket() 的值差异的问题;

    我最终的问题是我卡在了回调函数的调用上,同样的代码在3.8.7, 不会返回信息到vue回调函数中,3.7.11则可以;代码我将附在下发回复中,请帮忙看看

    2023-08-15 14:53

1***@qq.com

1***@qq.com (作者)

// listener(receive:(msg:string)=>void) {   
    listener(receive:UTSCallback) {  
        console.log('this.socket',this.socket);  
        if (this.socket != null) return  
        this.socket  = new MulticastSocket(this.port);  
        console.log('this.socket',this.socket);  
        const address = InetAddress.getByName(this.group);  
        this.socket?.joinGroup(address);    
        const buff = new ByteArray(1024);  
        const packet = new DatagramPacket(buff, buff.size, address, this.port);  
        const _socket = this.socket;  
        console.log('top');  
        setTimeout(() => {  
            console.log(111);  

                        // case 1: 在这里这样调用receive(), 3.8.7和3.7.11版本 都可以在vue页面收到信息  
            // let index = 0  
            // while(index<10){  
            //  receive('hhh'+index)   
            //  index++  
            // }  

                         // case 2: 在这里这样调用receive(msg), 3.8.7和3.7.11版本 都可以在vue页面收到信息  
                         // if (_socket == null) return  
            // _socket.receive(packet)   
                // const msg = new String(buff, 0, packet.getLength())  
            // console.log('msg',msg);  
            // receive(msg)  

                         // case 3:   这样调用receive(msg) 3.8.7版本并不执行,vue页面无法收到信息,3.7.11可以  
            if (_socket == null) return  
            this.isListener = true;  
            while (true) {  
                if (!this.isListener) return  
                try {  
                    _socket.receive(packet)   
                    const msg = new String(buff, 0, packet.getLength())  
                    console.log('msg',msg); // ----这里可以收到信息,是string字符串  
                    receive(msg)  // -----但3.8.7版本这里并不执行,vue页面无法收到信息,3.7.11可以  
                } catch (e) {  
                    console.log('e',e);  
                    this.isListener = false  
                }  
            }  
        }, 100)  
        console.log('bottom');  
    }

vue页面中(pages/index/index.vue)

import { UDPMulticast } from '../../uni_modules/uts-udp'    
export default {    
methods:{    
initUdp(){    
this.server = new UDPMulticast(MULTICAST_IP, MULTICAST_PORT)    
                this.server.listener((res) => {    
                    console.log("res",res);    
                })    
}    
}    
}

case3是正在使用的代码,如果是代码逻辑问题,为什么3.7.11可以正常返回信息呢?

要回复问题请先登录注册