mirror of
https://github.com/status-im/react-native-udp.git
synced 2025-02-25 23:15:15 +00:00
refactor to use regular es5 js, no need to make socket a component
This commit is contained in:
parent
dea64e5b7a
commit
cacff29bab
@ -13,14 +13,9 @@
|
||||
'use strict';
|
||||
|
||||
var React = require('react-native')
|
||||
var {
|
||||
Component
|
||||
} = React
|
||||
|
||||
var mixInEventEmitter = require('mixInEventEmitter')
|
||||
var DeviceEventEmitter = require('RCTDeviceEventEmitter')
|
||||
var NativeModules = require('NativeModules')
|
||||
var sockets = NativeModules.UdpSockets
|
||||
var Sockets = require('NativeModules').UdpSockets
|
||||
var noop = function () {}
|
||||
var instances = 0
|
||||
var STATE = {
|
||||
@ -29,35 +24,30 @@ var STATE = {
|
||||
BOUND: 2
|
||||
}
|
||||
|
||||
class RCTSocket extends Component {
|
||||
id: String;
|
||||
_state: Integer;
|
||||
_address: String;
|
||||
_port: Integer;
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.id = instances++
|
||||
this.subscriptiom = DeviceEventEmitter.addListener(
|
||||
'udp-' + this.id + '-data', this._onReceive.bind(this)
|
||||
function UdpSocket(type) {
|
||||
this._id = instances++
|
||||
this._state = STATE.UNBOUND
|
||||
this._subscriptiom = DeviceEventEmitter.addListener(
|
||||
'udp-' + this._id + '-data', this._onReceive.bind(this)
|
||||
);
|
||||
|
||||
// ensure compatibility with node's EventEmitter
|
||||
if (!this.on) this.on = this.addListener.bind(this)
|
||||
|
||||
this._state = STATE.UNBOUND
|
||||
sockets.createSocket(this.id, {
|
||||
type: props.type || 'udp4'
|
||||
Sockets.createSocket(this._id, {
|
||||
type: type || 'udp4'
|
||||
}) // later
|
||||
}
|
||||
|
||||
_debug() {
|
||||
UdpSocket.prototype._debug = function() {
|
||||
// for now
|
||||
var args = [].slice.call(arguments)
|
||||
args.unshift(this.id)
|
||||
args.unshift(this._id)
|
||||
console.log.apply(console, args)
|
||||
}
|
||||
|
||||
bind(port, address, callback) {
|
||||
|
||||
UdpSocket.prototype.bind = function(port, address, callback) {
|
||||
var self = this
|
||||
|
||||
if (this._state !== STATE.UNBOUND) throw new Error('Socket is already bound')
|
||||
@ -75,7 +65,7 @@ class RCTSocket extends Component {
|
||||
|
||||
this._state = STATE.BINDING
|
||||
this._debug('binding, address:', address, 'port:', port)
|
||||
sockets.bind(this.id, port, address, function(err, addr) {
|
||||
Sockets.bind(this._id, port, address, function(err, addr) {
|
||||
if (err) {
|
||||
// questionable: may want to self-destruct and
|
||||
// force user to create a new socket
|
||||
@ -92,11 +82,18 @@ class RCTSocket extends Component {
|
||||
})
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.subscription.remove();
|
||||
UdpSocket.prototype.close = function() {
|
||||
if (this._destroyed) return
|
||||
|
||||
this._destroyed = true
|
||||
this._debug('closing')
|
||||
this._subscription.remove();
|
||||
|
||||
Sockets.close(this._id, this._debug.bind(this, 'closed'))
|
||||
this.emit('close')
|
||||
}
|
||||
|
||||
_onReceive(info) {
|
||||
UdpSocket.prototype._onReceive = function(info) {
|
||||
this._debug('received', info)
|
||||
|
||||
var buf = toByteArray(info.data)
|
||||
@ -112,7 +109,6 @@ class RCTSocket extends Component {
|
||||
this.emit('message', buf, rinfo)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* socket.send(buf, offset, length, port, address, [callback])
|
||||
*
|
||||
@ -135,8 +131,8 @@ class RCTSocket extends Component {
|
||||
* @param {function} callback Callback when message is done being delivered.
|
||||
* Optional.
|
||||
*/
|
||||
// Socket.prototype.send = function (buf, host, port, cb) {
|
||||
send(buffer, offset, length, port, address, callback) {
|
||||
// UdpSocket.prototype.send = function (buf, host, port, cb) {
|
||||
UdpSocket.prototype.send = function(buffer, offset, length, port, address, callback) {
|
||||
var self = this
|
||||
|
||||
if (offset !== 0) throw new Error('Non-zero offset not supported yet')
|
||||
@ -157,7 +153,7 @@ class RCTSocket extends Component {
|
||||
}
|
||||
|
||||
self._debug('sending', buffer)
|
||||
sockets.send(this.id, buffer, +port, address, function(err) {
|
||||
Sockets.send(this._id, buffer, +port, address, function(err) {
|
||||
if (err) {
|
||||
self._debug('send failed', err)
|
||||
return callback(err)
|
||||
@ -168,7 +164,7 @@ class RCTSocket extends Component {
|
||||
})
|
||||
}
|
||||
|
||||
address() {
|
||||
UdpSocket.prototype.address = function() {
|
||||
if (this._state !== STATE.BOUND) {
|
||||
throw new Error('socket is not bound yet')
|
||||
}
|
||||
@ -180,53 +176,39 @@ class RCTSocket extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
var self = this
|
||||
if (this._destroyed) return
|
||||
|
||||
this._destroyed = true
|
||||
this._debug('closing')
|
||||
sockets.close(this.id, function() {
|
||||
self._debug('closed')
|
||||
})
|
||||
|
||||
this.emit('close')
|
||||
}
|
||||
|
||||
setBroadcast(flag) {
|
||||
UdpSocket.prototype.setBroadcast = function(flag) {
|
||||
// nothing yet
|
||||
}
|
||||
|
||||
setTTL(ttl) {
|
||||
UdpSocket.prototype.setTTL = function(ttl) {
|
||||
// nothing yet
|
||||
}
|
||||
|
||||
setMulticastTTL(ttl, callback) {
|
||||
UdpSocket.prototype.setMulticastTTL = function(ttl, callback) {
|
||||
// nothing yet
|
||||
}
|
||||
|
||||
setMulticastLoopback(flag, callback) {
|
||||
UdpSocket.prototype.setMulticastLoopback = function(flag, callback) {
|
||||
// nothing yet
|
||||
}
|
||||
|
||||
addMembership(multicastAddress, multicastInterface, callback) {
|
||||
UdpSocket.prototype.addMembership = function(multicastAddress, multicastInterface, callback) {
|
||||
// nothing yet
|
||||
}
|
||||
|
||||
dropMembership(multicastAddress, multicastInterface, callback) {
|
||||
UdpSocket.prototype.dropMembership = function(multicastAddress, multicastInterface, callback) {
|
||||
// nothing yet
|
||||
}
|
||||
|
||||
ref() {
|
||||
UdpSocket.prototype.ref = function() {
|
||||
// anything?
|
||||
}
|
||||
|
||||
unref() {
|
||||
UdpSocket.prototype.unref = function() {
|
||||
// anything?
|
||||
}
|
||||
}
|
||||
|
||||
mixInEventEmitter(RCTSocket, {
|
||||
mixInEventEmitter(UdpSocket, {
|
||||
'listening': true,
|
||||
'message': true,
|
||||
'close': true,
|
||||
@ -258,4 +240,4 @@ function toByteArray(obj) {
|
||||
return new Uint8Array(uint);
|
||||
}
|
||||
|
||||
module.exports = RCTSocket
|
||||
module.exports = UdpSocket
|
@ -4,7 +4,7 @@
|
||||
* @flow
|
||||
*/
|
||||
|
||||
var UdpSocket = require('UdpSocket')
|
||||
var UdpSocket = require('./UdpSocket.ios')
|
||||
|
||||
module.exports = {
|
||||
createSocket: function(type) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user