Coder Social home page Coder Social logo

netbeast / react-native-ssdp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from diversario/node-ssdp

40.0 10.0 19.0 159 KB

SSDP client + server to use in your react-native apps

Home Page: https://netbeast.co

Makefile 1.60% JavaScript 98.40%
iot node smart-home

react-native-ssdp's Introduction

SSDP fork for React Native from node

This is a fork of node-ssdp that uses react-native-udp instead of dgram to enable react multicast messaging and plain socket control. The API is the same as in the forked version.

works with yeti

 This package powers Yeti Smart Home and is used in production. It is maintained with our developers's free time, PRs and issues are more than welcome.

Installation

Unless React Native Version is > 0.29 use rnpm else use react-native link.

npm install react-native-ssdp
rnpm link

Make sure you set Buffer as global in react-native-udp's UdpSockets.js (noted filename end with letter s)

global.Buffer = global.Buffer || require('buffer').Buffer

Things to take note

  • Be aware that the Android emulator does not support incoming UDP traffic, so the client will not get any responses to search requests. For best results, test on a real device when using Android. The iOS simulator, on the other hand, works just fine.

  • Make sure you close all the sockets you open and never try to reopen one already open, mobile OSs and React Native are very aggresive both with security and performance, so a misuse could kill your process.

Usage (Android)

android/settings.gradle

...
include ':react-native-udp'
project(':react-native-udp').projectDir = new File(settingsDir, '../node_modules/react-native-udp/android')
include ':react-native-network-info'
project(':react-native-network-info').projectDir = new File(settingsDir, '../node_modules/react-native-network-info/android')

android/app/build.gradle

...
dependencies {
	...
	compile project(':react-native-udp')
	compile project(':react-native-network-info')
}

register module in MainActivity.java

For RN 0.19.0 and higher

import com.tradle.react.UdpSocketsModule;  // <--- import
import com.pusherman.networkinfo.RNNetworkInfoPackage; // <--- import

public class MainActivity extends ReactActivity {
   // ...
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(), // <---- add comma
		new UdpSocketsModule(), // <---- add package
        new RNNetworkInfoPackage() // <---------- add package
      );
    }

For react-native 0.29.0 and higher ( in MainApplication.java )

import com.tradle.react.UdpSocketsModule;  // <--- import
import com.pusherman.networkinfo.RNNetworkInfoPackage; // <--- import

public class MainApplication extends Application implements ReactApplication {
   // ...
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(), // <---- add comma
        new UdpSocketsModule(), // <---- add package
		new RNNetworkInfoPackage() <---- add package
      );
    }

Usage (iOS)

Adding with CocoaPads

Add the RNFS pod to your list of applications pods in your podfile, using the path from the Podfile to the installed module.

pod 'RNUDP', :path => './node_modules/react-native-udp'
pod 'RNNetworkInfo', :path => './node_modules/react-native-network-info'

Install pods as usual:

pod install

Adding manually in Xcode

In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name] Go to node_modules ➜ react-native-fs and add the .xcodeproj file

In XCode, in the project navigator, select your project. Add the lib*.a from the RNFS project to your project's Build Phases ➜ Link Binary With Libraries. Click the .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both $(SRCROOT)/../react-native/React and $(SRCROOT)/../../React - mark both as recursive.

Run your project (Cmd+R)

Note: Library uses these installed native_modules internally for sockets communication.

Usage - Client

    var Client = require('react-native-ssdp').Client
      , client = new Client();

    client.on('response', function (headers, statusCode, rinfo) {
      console.log('Got a response to an m-search.');
    });

    // search for a service type
    client.search('urn:schemas-upnp-org:service:ContentDirectory:1');

    // Or get a list of all services on the network

    client.search('ssdp:all');

Usage - Server

    var Server = require('react-native-ssdp').Server
      , server = new Server()
    ;

    server.addUSN('upnp:rootdevice');
    server.addUSN('urn:schemas-upnp-org:device:MediaServer:1');
    server.addUSN('urn:schemas-upnp-org:service:ContentDirectory:1');
    server.addUSN('urn:schemas-upnp-org:service:ConnectionManager:1');

    server.on('advertise-alive', function (headers) {
      // Expire old devices from your cache.
      // Register advertising device somewhere (as designated in http headers heads)
    });

    server.on('advertise-bye', function (headers) {
      // Remove specified device from cache.
    });

    // start the server
    server.start();

    process.on('exit', function(){
      server.stop() // advertise shutting down and stop listening
    })

Legacy docs from the forked repo

Take a look at example directory as well to see examples or client and server.

##Configuration new SSDP([options, [socket]])

SSDP constructor accepts an optional configuration object and an optional initialized socket. At the moment, the following is supported:

  • ssdpSig String SSDP signature. Default: node.js/NODE_VERSION UPnP/1.1 node-ssdp/PACKAGE_VERSION
  • ssdpIp String SSDP multicast group. Default: 239.255.255.250.
  • ssdpPort Number SSDP port. Default: 1900
  • ssdpTtl Number Multicast TTL. Default: 1
  • adInterval Number advertise event frequency (ms). Default: 10 sec.
  • unicastHost String IP address or hostname of server where SSDP service is running. This is used in HOST header. Default: 0.0.0.0.
  • location String URL pointing to description of your service, or a function which returns that URL
  • udn String Unique Device Name. Default: uuid:f40c2981-7329-40b7-8b04-27f187aecfb5.
  • description String Path to description file. Default: upnp/desc.php.
  • ttl Number Packet TTL. Default: 1800.
  • allowWildcards Boolean Accept wildcards (*) in serviceTypes of M-SEARCH packets, e.g. usn:Belkin:device:**. Default: false

###Logging

You can enable logging via an environment variable DEBUG. Set DEBUG=node-ssdp* to enable all logs. To enable only client or server logs, use DEBUG=node-ssdp:client or DEBUG=node-ssdp:server respectively.

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-native-ssdp's People

Contributors

aleno avatar axit-joost avatar dhleong avatar diversario avatar feross avatar franrios avatar jsdario avatar luisfpinto avatar lukewis avatar mnkhouri avatar mrose17 avatar muka avatar oeuillot avatar pablopi avatar tohjg avatar waffle-iron avatar wagmarcel avatar yaronyg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-ssdp's Issues

Cannot plugin

Hi bro, when i install this lib then run my project. I have a problem.
pls help me :)

I used:
"react": "15.4.2",
"react-native": "0.42.0"

Thanks

image

Help to bring this library, work as expected in react-native apps.

Library installation showed linking process, but there were no android / iOS directories in the project to help linking native modules to the application.

Business Case:
Use SSDP client where I can search for endpoints supporting a specific service type (ST).
Client was initialized without any extra parameters (since I want to default it to ssdpIp & ssdpPort)

Problems Faced:

Cannot resolve module dgram(To fix this, installed react-native-udp and link process, since library was using dgram from nativemodules of RN)

After solving this, got error like 'getIPAddress' method invoked on undefined object. Because it required another native module react-native-network-info & link process.

Post successful loading of files, without error. I got some console messages while debugging namely
"socket-1 binding, address: 0.0.0.0 port: 0"
"socket-1 bound to address: 0.0.0.0 port: 0"
"WebSocket error [object Object]"

While debugging, error object was undefined, was not able to derive what was I missing ?

Versions Used:
"react-native": "0.40.0",
"react-native-ssdp": "^2.7.5",
"react-native-udp": "^1.2.9",

Let me know whether these additional installation processes are required, to make it
load without errors ?
& Let me know how to bring this to working condition.

Pod install not work

There is no podspec under node_modules/react-native-udp' and there is no node_modules/react-native-network-info, please make sure this .

pod 'RNUDP', :path => './node_modules/react-native-udp'
pod 'RNNetworkInfo', :path => './node_modules/react-native-network-info'

iOS no response

Hi All,

I'm using this library on iOS and Android. The Android version works great, however the iOS version simply get no response upon client.search("ssd:all");.
Another difference I noticed was when starting the app.

On android it prints:

socket-0 binding, address: 0.0.0.0 port: 0
socket-0 bound to address: 0.0.0.0 port: 0

But on ios it prints

socket-0 binding, address: 0.0.0.0 port: 0
socket-0 bound to address: 0.0.0.0 port: 53061 

The port 53061 is just a random port and changes all the time.
I though it might have something to do with entitlements/capabilities on iOS, so I enabled everything that sounded like it might have something to do with this issue. No luck so far.

Any ideas of what is going on?
Cheers

getting NetworkInfo.getIPAddress is not a function

So I tried adding this package to a brand new React-Native Project and I got the error NetworkInfo.getIPAddress is not a function in SsdpClient.SSDP index.js:91.

"react": "16.0.0-alpha.6",
"react-native": "0.44.0",
"react-native-network-info": "2.2.0"
"react-native-udp":"2.0.0"

I fixed this by changing the import statement in line 14 in index.js from:
var NetworkInfo = require('react-native-network-info')
to ES6 style:
import { NetworkInfo } from 'react-native-network-info'
even though it's not consistent with the other import statements in this project it seems to at least get around that error when instantiating a new Client object.

Also just a note where you mention adding buffer to Sockets.js in react-native-udp, I believe you also have to npm install buffer (at least according to the react-native-udp docs). That was getting me stuck for a bit with setting this up for anyone else who happens to run across this. Also one note is you mention npm install react-native-udp but not npm install react-native-network-info in the documentation, which also gave me hiccup when trying to set up my project.

Not able to find devices on the network

I am trying to get all devices on the network.

Here is my code:

//imported the lib
const Client = require('react-native-ssdp').Client;

//In componentDidMount, created the client and started search.
componentDidMount() {
	var client = new Client();

	client.on('response', function (headers, statusCode, rinfo) {
		console.log('Got a response to an m-search.', rinfo);
	});

	client.search('ssdp:all');
}

Below is windows ssdp result[Same network as mobile]:
image

Please guide me if I'm doing something wrong.
Thanks in advance.

Need a way to setBroadcast from clients

We may need to expose a function in SSDP prototype where clients can set broadcast property to true/false.

I saw a @ReactMethod exposed from the UdpSockets native module, which we can make use of.

I can not get the example to work

I am trying to get the client example to work. ( I am using a Samsung S7 and the emulator ) neither of them works.

I follow all of it and also follow the fix for network-info (NetworkInfo.getIPAddress)

but when I run the client sample all I see is

07-28 19:48:31.499 3037 3078 I ReactNativeJS: 'socket-1', 'binding, address:', '0.0.0.0', 'port:', 0

07-28 19:48:31.507 3037 3078 I ReactNativeJS: 'socket-1', 'bound to address:', '0.0.0.0', 'port:', 0

07-28 19:48:41.517 3037 3078 I ReactNativeJS: 'socket-1', 'closing'

07-28 19:48:41.520 3037 3078 I ReactNativeJS: 'socket-1', 'closed'

I got plenty of upnp devices also downloaded http://www.meshcommander.com/upnptools

those tools do see my upnp devices

any hints on how to trouble shoot it ?

Thanks

require('react-native-ssdp').Server returns undefined

I've got this module all installed but for some reason there's no .Server property when you require the module. In your example code you have

var Server = require('react-native-ssdp').Server
      , server = new Server()
    ;

But that's throwing undefined is not a constructor because there's no .Server property on what comes out of require.

The module's definitely installed as the require is fine, and .Client works fine, returning the SsdpClient constructor. I logged out all the props on what's returned from require('react-native-ssdp') and the only two properties are Client and Base. Where's the Server, and how do I access it?

react-native 0.51.0

Publish version 2.8.2 to npm

Hi there,

I was wondering if you would be able to publish the latest version 2.8.2 to npm. Plus thanks for open sourcing this lib.

Nimesh

Build failed

I am getting below error while building the application.

FAILURE: Build failed with an exception.

  • Where:
    Build file 'E:\ReactApps\Duo\node_modules\react-native-network-info\android\buil
    d.gradle' line: 4

  • What went wrong:
    A problem occurred evaluating project ':react-native-network-info'.

Could not find method google() for arguments [] on repository container.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug
    option to get more log output.

BUILD FAILED

I've followed every steps as written on the description.
Please help me to resolve it.

Thnaks in advance

Different behaviour between iOS [No results] and Android [Working]

I am using react-native-ssdp in a react native example project.

I have the following code:

import { Client } from 'react-native-ssdp';
import { filter } from './constants'

export function startSSDPDiscovery(){
    console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Starting Discovery");

    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Creating Client');
    var client = new Client();

    client.on('response', (headers, statusCode, rinfo) => {
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> rinfo:', rinfo);
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Headers:', headers);
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> statusCode:', statusCode)
    });
    
    
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Starting Search');

    client.search(filter);
}

I am getting the following logs on Android:

 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Starting Discovery
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Creating Client
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Starting Search
 LOG  socket-8 binding, address: 0.0.0.0 port: 0
 LOG  socket-9 bound to address: 0.0.0.0 port: 0
 WARN  react-native-udp: setMulticastTTL() is not implemented
 LOG  socket-8 bound to address: 0.0.0.0 port: 0
 WARN  react-native-udp: setMulticastTTL() is not implemented
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> rinfo: {...}
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Headers: {...}
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> statusCode: 200
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> rinfo: {...}
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Headers: {...}
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> statusCode: 200

However on iOS I am not getting any response just the following logs:

 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Starting Discovery
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Creating Client
 LOG  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Starting Search
 LOG  socket-0 binding, address: 0.0.0.0 port: 0
 LOG  socket-1 bound to address: 0.0.0.0 port: 57910
 WARN  react-native-udp: setMulticastTTL() is not implemented
 LOG  socket-0 bound to address: 0.0.0.0 port: 50691
 WARN  react-native-udp: setMulticastTTL() is not implemented

I am using the following:

Header Header
react-native 0.73.4
react-native-network-info ^5.2.1
react-native-ssdp ^2.8.2
react-native-udp ^4.1.7
iOS 16.7.5
Android 12

Unable to resolve "buffer/"

I get this error when I start the program:

Failed building JavaScript bundle.
Unable to resolve "buffer/" from "node_modules\react-native-ssdp\lib\client.js"

Usage with expo

Hi, I'm trying to integrate this project with expo, but it seems that the structure of that apps are quite a bit different...I was wondering if you could add expo-specific instructions to the docs? I'm pretty new to react native as it is, and it's kind of hard to figure this stuff out

Galaxy S7 is not receiving UDP messages

I can send UDP messages but I can not receive any UDP message on my galaxy 7 device.

I can see the messages with wireshark. anyone got this running on a Galaxy S7 ?

I am trying to get other devices to test. besides using react-native-ssdp, I also tested it with using react-native-udp same issue. I can send but not receive messages.

upgrading react-native-udp dependency to 4.0 will break

Upgrading react-native-udp to ^4.0.0 will break, causing a

dgram.createSocket is not a function

I suggest to use import dgram from 'react-native-udp'; in lib/index.js instead of var dgram = require('react-native-udp');

A better example on how to use this

Hello, fairly new on the react-native scenario.

I need a few informations on how to use this lib properly, excuse me for my lack of experience on this framework.

Ok, I made a componente to use this lib, I need my device to get SSDP information from another device. So I believe I suppose to use this as a client.

I also understand the better usage would be on componentDidMount() so it can find the device I want when it loads.

My supper newbie questions are:

What do I have to import in order to make this work?
On the client example you can find on this github, what I can declare globaly like a variable and what I should put inside of componentDidMount to trigger the search?


the way I understood, the functions below are all based on client.search to search for all devices once every 5 seconds for 10 seconds

client.search('urn:schemas-upnp-org:service:ContentDirectory:1')

// Or maybe if you want to scour for everything after 5 seconds
setTimeout(function() {
  client.search('ssdp:all')
}, 5000)

// And after 10 seconds, you want to stop
setTimeout(function () {
  client.stop()
}, 10000)

and those are declarations I should make globally outside of classes and functions.

var ssdp = require('../index').Client
  , client = new ssdp({
//    unicastHost: '192.168.11.63'
  })

client.on('notify', function () {
  //console.log('Got a notification.')
})

client.on('response', function inResponse(headers, code, rinfo) {
  console.log('Got a response to an m-search:\n%d\n%s\n%s', code, JSON.stringify(headers, null, '  '), JSON.stringify(rinfo, null, '  '))
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.