Search code examples
cordovaionic-frameworkudpionic4

Ionic 4 - chrome sockets udp not creating socket without outputting any error


I'm trying to use chrome.sockets.udp to send and receive udp packets. I'm using the following code:

async broadcasting(messageSend)
{
    var str2ab=function(str) {
        var buf=new ArrayBuffer(str.length);
        var bufView=new Uint8Array(buf);
        for (var i=0; i<str.length; i++) {
        bufView[i]=str.charCodeAt(i);
        }
        return buf;
    }

    // From https://developer.chrome.com/trunk/apps/app_hardware.html
    var ab2str=function(buf) {
        return String.fromCharCode.apply(null, new Uint8Array(buf));
    };

    var socketId;
    var onReceive = function(info) {
        console.log(info.data);
        if (info.socketId !== socketId)
        return;

    };
    console.log("Creaste");
    var socketOption = {
        persistent: true,
        name: 'udpSocket',
        bufferSize: 4096
    };
    chrome.sockets.udp.onReceiveError.addListener((data) => {
        console.log('received error');
        console.log(data);
    });

    chrome.sockets.udp.onReceive.addListener((data) => {
        console.log('received');
        console.log(data);
    });

    chrome.sockets.udp.create(socketOption, function(socketInfo) {
        socketId = socketInfo.socketId;
        // Setup event handler and bind socket.
       console.log("socketId",socketId);
       alert();

       chrome.sockets.udp.onReceive.addListener(onReceive);
        chrome.sockets.udp.bind(socketId,
            "0.0.0.0", 0, function(result) {
                console.log(" binding socket.");
            if (result < 0) {
                console.log("Erro binding socket.");
                return;
            }//52.20.16.20 40000j
            chrome.sockets.udp.send(socketId, str2ab(ab2str(messageSend)),
            '192.168.1.69', 63580, function(sendInfo) {
                console.log("sent " + sendInfo.bytesSent);
            });
        });


    });
}

But the callback of create is never called and in the console i dont get any error or any indication of why the socket is not created! I'm running it on a real android device. Any idea ?


Solution

  • The problem was due to a conflict with another package, I didn't find which one, but by starting a new project and installing only the packages that I needed it started working as supposed.