brugsel / capacitor-udp

4 stars 11 forks source link

Documentation update #2

Open panksi opened 3 years ago

panksi commented 3 years ago

Hi,

an example of how the plugin is instanced and how it works would be nice. ex. now I have this issue: image

And dunno what to do with it :)

I am using the latest Ionic with VUE and the latest Capacitor

please help, thanks upfront

panksi commented 3 years ago

I just realized that nothing has changed in this fork except the metadata :) so I would assume it still has the same problem as the plugin from the main repo... Not a bugger, I'll try to repack the plugin to match capacitor 3+imports. If you do so in the meantime, please let me know not to waste the time on the same thing. thanks

tscizzlebg commented 2 years ago

@panksi Did you ever make this compatible with capacitor 3? (Or find anything else that already is?)

acolytec3 commented 2 years ago

After much trial and error, I've been able to get this plugin to work with react (admittedly not Vue but hopefully the general steps will work for you too).

Once you install the plugin, a couple of set up steps:

  1. Make sure that the webDir key in capacitor.config.js refers to the directory your index.html file is in (public for react apps if doing development or build if building the app first)
  2. Run npx cap sync and then npx cap copy to ensure the plugin files are copied to the corresponding mobile app platform directories (android or ios).

Then, use it as below:

First, you have to import it as: import { UDP } from '@frontall/capacitor-udp' since it uses a named export.

Then, create a socket and bind it to a port:

const socket = UDP.create()
await UDP.bind({socketId: socket.socketId, socket.ipv4, port: 0})

Then set up listeners or send stuff:

UDP.addListener("receive", ({socketId, buffer}) => {
    console.log('got a message', buffer)
    UDP.send({ socketId: socket.socketId, address: '192.168.1.100', port: 100, buffer: 'hello friend'})
})

Something like this.

panksi commented 2 years ago

@panksi Did you ever make this compatible with capacitor 3? (Or find anything else that already is?)

Sorry for the late reply bro, for me it turned out that the complete Java part of the app (opening the socket for Android) was missing, so I abandoned the experiment. Maybe this from comment bellow will work...