Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not find service #20

Open
lublak opened this issue May 18, 2022 · 4 comments
Open

can not find service #20

lublak opened this issue May 18, 2022 · 4 comments

Comments

@lublak
Copy link

lublak commented May 18, 2022

I use multiple application but i find any other service but not the service created with this npm package:
https://play.google.com/store/apps/details?id=com.druk.servicebrowser
https://play.google.com/store/apps/details?id=ua.com.streamsoft.pingtools

@lublak
Copy link
Author

lublak commented May 18, 2022

i found out that somehow host and port is not avaible and i think the apps filtering this out.
I created my own with: https://pub.dev/packages/bonsoir
But host and port is empty.

import Bonjour from 'bonjour-service'
const instance = new Bonjour()
instance.publish({ name: 'test', type: 'testservice', port: 3000 });

also tried with https://pub.dev/packages/nsd

@mdidon
Copy link
Member

mdidon commented May 27, 2022

@lublak Thanks for highlighting, we'll have a look into this.
If you have any flutter specific code you can share, that would be greatly appreciated.

@lublak
Copy link
Author

lublak commented May 27, 2022

@mdidon

Actually, I just used the example of that:

String type = '_testservice._tcp';
BonsoirDiscovery discovery = BonsoirDiscovery(type: type);
await discovery.ready;
await discovery.start();
discovery.eventStream.listen((event) {
  if (event.type == BonsoirDiscoveryEventType.DISCOVERY_SERVICE_RESOLVED) {
    print('Service found : ${event.service.toJson()}')
  } else if (event.type == BonsoirDiscoveryEventType.DISCOVERY_SERVICE_LOST) {
    print('Service lost : ${event.service.toJson()}')
  }
});
//await discovery.stop();

One more thing. The following bilibrary on go works great: https://github.com/grandcat/zeroconf

@savhascelik
Copy link

savhascelik commented Oct 8, 2024

this way I find the service on two different windows machines

import { app, BrowserWindow } from 'electron';
import { createServer, Socket } from 'net';
import {Bonjour} from 'bonjour-service';
const SERVICE_TYPE = 'myservice';
const SERVICE_PORT = 41235;

let mainWindow;
let isServer = false;
let serverInstance;
let bonjourInstance;

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  });

  mainWindow.loadFile('index.html');
}

function startDiscovery() {
  bonjourInstance = new Bonjour();

  const browser = bonjourInstance.find({ type: SERVICE_TYPE });

  browser.on('up', (service) => {
    console.log('Found an existing server:', service.name);
    runAsClient(service.addresses[0], service.port);
    browser.stop();
  });

  browser.on('error', (err) => {
    console.error('Error during Bonjour discovery:', err);
  });
  setTimeout(() => {
    if (!isServer) {
      console.log('No server found. Running as server.');
      runAsServer();
    }
    browser.stop();
  }, 5000);
}

function runAsServer() {
  isServer = true;
  
  const server = createServer((socket) => {
    console.log('Client connected');
    socket.on('data', (data) => {
      console.log('Received data:', data.toString());
    });
  });

  server.listen(SERVICE_PORT, () => {
    console.log('Server listening on port', SERVICE_PORT);
    
    serverInstance = bonjourInstance.publish({
      name: 'MyAppServer',
      type: SERVICE_TYPE,
      port: SERVICE_PORT,
      protocol: 'tcp'
    });
  });
}

function runAsClient(serverAddress, serverPort) {
  const client = new Socket();
  client.connect(serverPort, serverAddress, () => {
    console.log('Connected to server');
    client.write('Hello from client');
  });

  client.on('data', (data) => {
    console.log('Received from server:', data.toString());
  });
}

app.whenReady().then(() => {
  createWindow();
  startDiscovery();
}).catch(error => {
  console.error('Error during app initialization:', error);
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    if (serverInstance) {
      serverInstance.stop();
    }
    if (bonjourInstance) {
      bonjourInstance.destroy();
    }
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants