How to use capacitor-udp plugin

HI,

testing the plugin https://github.com/unitree-czk/capacitor-udp, but getting the following error when running in android simulator:

TypeError: Cannot read property ‘create’ of undefined

My code is the following:

async process() {
        try {
            // await UdpPlugin.closeAllSockets();

            const info = await UdpPlugin.create();
            await UdpPlugin.bind({socketId: info.socketId, address: '0.0.0.0', port: 5500});

            await UdpPlugin.addListener('receive', data => {
                console.log('receive:', data);
            });

            await UdpPlugin.addListener('receiveError', error => {
                console.log('receiveError:', error);
            });

            await UdpPlugin.send({
                socketId: info.socketId,
                address: '226.1.1.1',
                port: 3140,
                buffer: 'hello'
            });
        } catch (e) {
            console.log('error:', e);
        }
    }

Someone have a hint, what is the problem?

1 Like

So the error seems like the plugin isn’t being provided. How are you importing the plugin? Are you including

import { Plugins } from "@capacitor/core";
const { UdpPlugin } = Plugins;
import {UdpPluginUtils} from "capacitor-udp";

Yes, i am:

import {Plugins} from '@capacitor/core';

const {UdpPlugin} = Plugins;
import {UdpPluginUtils} from 'capacitor-udp'; // if you want support for converting between ArrayBuffer and String

try moving the const {UdpPlugin} part to inside the process method

Still the same error.

I just created a project and tested with this

import { Component } from '@angular/core';
import { Plugins } from '@capacitor/core';
@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
  constructor() {}
  async process() {
    const { UdpPlugin } = Plugins;
    let info = await UdpPlugin.create();
    console.log(info);
  }
}

And the create call worked

Crazy, i can not get it working. I just created a new capacitor project and used your code.
I installed the plugin and added android platform and run it it simulator:

vendor-es2015.js:44320 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'create' of undefined TypeError: Cannot read property 'create' of undefined at Tab1Page.<anonymous> (tab1-tab1-module-es2015.js:2852) at Generator.next (<anonymous>) at vendor-es2015.js:119189 at new ZoneAwarePromise (polyfills-es2015.js:3882) at Module.__awaiter (vendor-es2015.js:119185) at Tab1Page.process (tab1-tab1-module-es2015.js:2850) at Object.eval [as handleEvent] (Tab1Page.html:10) at handleEvent (vendor-es2015.js:73308) at callWithDebugContext (vendor-es2015.js:74926) at Object.debugHandleEvent [as handleEvent] (vendor-es2015.js:74562) at resolvePromise (polyfills-es2015.js:3803) at new ZoneAwarePromise (polyfills-es2015.js:3885) at Module.__awaiter (vendor-es2015.js:119185) at Tab1Page.process (tab1-tab1-module-es2015.js:2850) at Object.eval [as handleEvent] (Tab1Page.html:10) at handleEvent (vendor-es2015.js:73308) at callWithDebugContext (vendor-es2015.js:74926) at Object.debugHandleEvent [as handleEvent] (vendor-es2015.js:74562) at dispatchEvent (vendor-es2015.js:61028) at vendor-es2015.js:72240

Would you show me your package.json?