mirror of
https://github.com/status-im/react-native-udp.git
synced 2025-01-27 09:14:52 +00:00
fix data transfer over RCTBridge, for now use base64 strings until they support binary data transfer
This commit is contained in:
parent
686dc283a8
commit
516530edbd
@ -16,6 +16,7 @@ var React = require('react-native')
|
|||||||
var mixInEventEmitter = require('mixInEventEmitter')
|
var mixInEventEmitter = require('mixInEventEmitter')
|
||||||
var DeviceEventEmitter = require('RCTDeviceEventEmitter')
|
var DeviceEventEmitter = require('RCTDeviceEventEmitter')
|
||||||
var Sockets = require('NativeModules').UdpSockets
|
var Sockets = require('NativeModules').UdpSockets
|
||||||
|
var base64 = require('base64-js')
|
||||||
var noop = function () {}
|
var noop = function () {}
|
||||||
var instances = 0
|
var instances = 0
|
||||||
var STATE = {
|
var STATE = {
|
||||||
@ -96,7 +97,7 @@ UdpSocket.prototype.close = function() {
|
|||||||
UdpSocket.prototype._onReceive = function(info) {
|
UdpSocket.prototype._onReceive = function(info) {
|
||||||
this._debug('received', info)
|
this._debug('received', info)
|
||||||
|
|
||||||
var buf = toByteArray(info.data)
|
var buf = base64.toByteArray(info.data)
|
||||||
var rinfo = {
|
var rinfo = {
|
||||||
address: info.address,
|
address: info.address,
|
||||||
port: info.port,
|
port: info.port,
|
||||||
@ -145,15 +146,23 @@ UdpSocket.prototype.send = function(buffer, offset, length, port, address, callb
|
|||||||
}
|
}
|
||||||
|
|
||||||
callback = callback || noop
|
callback = callback || noop
|
||||||
|
var str
|
||||||
if (typeof buffer === 'string') {
|
if (typeof buffer === 'string') {
|
||||||
buffer = toByteArray(buffer)
|
console.warn('socket.send(): interpreting as base64')
|
||||||
|
str = buffer
|
||||||
}
|
}
|
||||||
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(buffer)) {
|
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(buffer)) {
|
||||||
buffer = buffer.toJSON().data
|
str = buffer.toString('base64')
|
||||||
|
}
|
||||||
|
else if (buffer instanceof Uint8Array || Array.isArray(buffer)) {
|
||||||
|
str = base64.fromByteArray(buffer)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error('invalid message format')
|
||||||
}
|
}
|
||||||
|
|
||||||
self._debug('sending', buffer)
|
self._debug('sending', buffer, str)
|
||||||
Sockets.send(this._id, buffer, +port, address, function(err) {
|
Sockets.send(this._id, str, +port, address, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
self._debug('send failed', err)
|
self._debug('send failed', err)
|
||||||
return callback(err)
|
return callback(err)
|
||||||
@ -215,29 +224,4 @@ mixInEventEmitter(UdpSocket, {
|
|||||||
'error': true
|
'error': true
|
||||||
})
|
})
|
||||||
|
|
||||||
function toByteArray(obj) {
|
|
||||||
if (typeof obj === 'object') {
|
|
||||||
var i = 0
|
|
||||||
var arr = []
|
|
||||||
while (true) {
|
|
||||||
if (!(i in obj)) break
|
|
||||||
|
|
||||||
arr.push(+obj[i])
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Uint8Array(arr)
|
|
||||||
}
|
|
||||||
else if (typeof obj !== 'string') {
|
|
||||||
throw new Error('unsupported format')
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = UdpSocket
|
module.exports = UdpSocket
|
16
UdpSockets.m
16
UdpSockets.m
@ -58,13 +58,16 @@ RCT_EXPORT_METHOD(bind:(NSString*)cId
|
|||||||
}
|
}
|
||||||
|
|
||||||
RCT_EXPORT_METHOD(send:(NSString*)cId
|
RCT_EXPORT_METHOD(send:(NSString*)cId
|
||||||
data:(NSData*)data
|
base64String:(NSString*)base64String
|
||||||
port:(int)port
|
port:(int)port
|
||||||
address:(NSString*)address
|
address:(NSString*)address
|
||||||
callback:(RCTResponseSenderBlock)callback) {
|
callback:(RCTResponseSenderBlock)callback) {
|
||||||
UdpSocketClient* client = [self findClient:cId callback:callback];
|
UdpSocketClient* client = [self findClient:cId callback:callback];
|
||||||
if (!client) return;
|
if (!client) return;
|
||||||
|
|
||||||
|
// iOS7+
|
||||||
|
// TODO: use https://github.com/nicklockwood/Base64 for compatibility with earlier iOS versions
|
||||||
|
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
|
||||||
[client send:data remotePort:port remoteAddress:address callback:callback];
|
[client send:data remotePort:port remoteAddress:address callback:callback];
|
||||||
if (callback) callback(@[]);
|
if (callback) callback(@[]);
|
||||||
}
|
}
|
||||||
@ -83,15 +86,10 @@ RCT_EXPORT_METHOD(close:(NSString*)cId
|
|||||||
- (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port
|
- (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port
|
||||||
{
|
{
|
||||||
NSString *clientID = [[_clients allKeysForObject:client] objectAtIndex:0];
|
NSString *clientID = [[_clients allKeysForObject:client] objectAtIndex:0];
|
||||||
NSPropertyListFormat format;
|
NSString *base64String = [data base64EncodedStringWithOptions:0];
|
||||||
NSArray* arr = [NSPropertyListSerialization propertyListFromData:data
|
|
||||||
mutabilityOption:NSPropertyListMutableContainers
|
|
||||||
format:&format
|
|
||||||
errorDescription:NULL];
|
|
||||||
|
|
||||||
[self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"udp-%@-data", clientID]
|
[self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"udp-%@-data", clientID]
|
||||||
body:@{
|
body:@{
|
||||||
@"data": arr,
|
@"data": base64String,
|
||||||
@"address": host,
|
@"address": host,
|
||||||
@"port": [NSNumber numberWithInt:port]
|
@"port": [NSNumber numberWithInt:port]
|
||||||
}
|
}
|
||||||
|
21
index.ios.js
21
index.ios.js
@ -16,7 +16,8 @@ function randomPort() {
|
|||||||
return Math.random() * 65536 | 0
|
return Math.random() * 65536 | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
var dgram = require('RCTUDP')
|
var base64 = require('base64-js')
|
||||||
|
var dgram = require('dgram')
|
||||||
var a = dgram.createSocket('udp4')
|
var a = dgram.createSocket('udp4')
|
||||||
var aPort = randomPort()
|
var aPort = randomPort()
|
||||||
a.bind(bPort, function(err) {
|
a.bind(bPort, function(err) {
|
||||||
@ -92,24 +93,8 @@ var styles = StyleSheet.create({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// only works for 8-bit chars
|
||||||
function toByteArray(obj) {
|
function toByteArray(obj) {
|
||||||
if (typeof obj === 'object') {
|
|
||||||
var i = 0
|
|
||||||
var arr = []
|
|
||||||
while (true) {
|
|
||||||
if (!(i in obj)) break
|
|
||||||
|
|
||||||
arr.push(+obj[i])
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Uint8Array(arr)
|
|
||||||
}
|
|
||||||
else if (typeof obj !== 'string') {
|
|
||||||
throw new Error('unsupported format')
|
|
||||||
}
|
|
||||||
|
|
||||||
var uint = new Uint8Array(obj.length);
|
var uint = new Uint8Array(obj.length);
|
||||||
for (var i = 0, l = obj.length; i < l; i++){
|
for (var i = 0, l = obj.length; i < l; i++){
|
||||||
uint[i] = obj.charCodeAt(i);
|
uint[i] = obj.charCodeAt(i);
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/tradle/react-native-udp.git"
|
"url": "https://github.com/tradle/react-native-udp"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"react-component",
|
"react-component",
|
||||||
@ -22,13 +22,17 @@
|
|||||||
"sockets",
|
"sockets",
|
||||||
"ios"
|
"ios"
|
||||||
],
|
],
|
||||||
"author": "Mark Vayngrib <mark.vayngrib@lablz.com>",
|
"author": {
|
||||||
|
"name": "Mark Vayngrib",
|
||||||
|
"email": "mark.vayngrib@lablz.com"
|
||||||
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/tradle/react-native-udp/issues"
|
"url": "https://github.com/tradle/react-native-udp/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/tradle/react-native-udp",
|
"homepage": "https://github.com/tradle/react-native-udp",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"base64-js": "0.0.8",
|
||||||
"react-native": "^0.4.2"
|
"react-native": "^0.4.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user