'use strict'; var React = require('react-native'); var { AppRegistry, StyleSheet, Text, View, } = React; // require('./test/simple/test-dgram-address') // require('./test/simple/test-dgram-bind-default-address') // require('./test/simple/test-dgram-bind-shared-ports') function randomPort() { return Math.random() * 65536 | 0 } var base64 = require('base64-js') var dgram = require('dgram') var a = dgram.createSocket('udp4') var aPort = randomPort() a.bind(bPort, function(err) { if (err) throw err console.log('address', a.address()) }) var b = dgram.createSocket('udp4') var bPort = randomPort() b.bind(bPort, function(err) { if (err) throw err console.log('address', b.address()) }) a.on('message', function(data, rinfo) { var str = String.fromCharCode.apply(null, new Uint8Array(data)); console.log('a received', str, rinfo) a.close() b.close() }) b.on('message', function(data, rinfo) { var str = String.fromCharCode.apply(null, new Uint8Array(data)); console.log('b received', str, rinfo) // echo back b.send(data, 0, data.length, aPort, '127.0.0.1', function(err) { if (err) throw err console.log('sent') }) }) b.once('listening', function() { var msg = toByteArray('hello') a.send(msg, 0, msg.length, bPort, '127.0.0.1', function(err) { if (err) throw err console.log('sent') }) }) var rctsockets = React.createClass({ render: function() { return ( Open Dev Tools to see socket chatter ); } }); var styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, }); // only works for 8-bit chars function toByteArray(obj) { var uint = new Uint8Array(obj.length); for (var i = 0, l = obj.length; i < l; i++){ uint[i] = obj.charCodeAt(i); } return new Uint8Array(uint); } AppRegistry.registerComponent('react-native-udp', () => rctsockets);