refactor into lib
This commit is contained in:
parent
1bf5626024
commit
798c1d5a2c
33
.flowconfig
33
.flowconfig
|
@ -1,33 +0,0 @@
|
|||
[ignore]
|
||||
|
||||
# We fork some components by platform.
|
||||
.*/*.web.js
|
||||
.*/*.android.js
|
||||
|
||||
# Some modules have their own node_modules with overlap
|
||||
.*/node_modules/node-haste/.*
|
||||
|
||||
# Ignore react-tools where there are overlaps, but don't ignore anything that
|
||||
# react-native relies on
|
||||
.*/node_modules/react-tools/src/vendor/core/ExecutionEnvironment.js
|
||||
.*/node_modules/react-tools/src/browser/eventPlugins/ResponderEventPlugin.js
|
||||
.*/node_modules/react-tools/src/browser/ui/React.js
|
||||
.*/node_modules/react-tools/src/core/ReactInstanceHandles.js
|
||||
.*/node_modules/react-tools/src/event/EventPropagators.js
|
||||
|
||||
# Ignore commoner tests
|
||||
.*/node_modules/react-tools/node_modules/commoner/test/.*
|
||||
|
||||
# See https://github.com/facebook/flow/issues/442
|
||||
.*/react-tools/node_modules/commoner/lib/reader.js
|
||||
|
||||
# Ignore jest
|
||||
.*/react-native/node_modules/jest-cli/.*
|
||||
|
||||
[include]
|
||||
|
||||
[libs]
|
||||
node_modules/react-native/Libraries/react-native/react-native-interface.js
|
||||
|
||||
[options]
|
||||
module.system=haste
|
|
@ -1,30 +0,0 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directory
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
*.xcodeproj/project.xcworkspace/
|
||||
*.xcodeproj/xcuserdata/
|
22
LICENSE
22
LICENSE
|
@ -1,22 +0,0 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Tradle
|
||||
|
||||
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.
|
||||
|
File diff suppressed because it is too large
Load Diff
68
README.md
68
README.md
|
@ -1,68 +0,0 @@
|
|||
# UDP in React Native
|
||||
|
||||
node's [dgram](https://nodejs.org/api/dgram.html) API in React Native
|
||||
|
||||
## This module is used by [Tradle](https://github.com/tradle)
|
||||
|
||||
PR's welcome!
|
||||
|
||||
## Install
|
||||
|
||||
* Create a new react-native project. [Check react-native getting started](http://facebook.github.io/react-native/docs/getting-started.html#content)
|
||||
|
||||
* in PROJECT_DIR/node_modules/react-native, execute:
|
||||
```
|
||||
npm install --save react-native-udp
|
||||
```
|
||||
|
||||
* Drag RCTUDP.xcodeproj from node_modules/react-native-udp into your XCode project. Click on the project in XCode, go to Build Phases, then Link Binary With Libraries and add libReactUDP.a
|
||||
|
||||
Buckle up, Dorothy
|
||||
|
||||
## Usage
|
||||
|
||||
### package.json
|
||||
|
||||
_only if you want to write require('dgram') in your javascript_
|
||||
|
||||
```json
|
||||
{
|
||||
"browser": {
|
||||
"dgram": "react-native-udp"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### JS
|
||||
|
||||
_see/run index.ios.js for a complete example, but basically it's just like dgram_
|
||||
|
||||
var dgram = require('dgram')
|
||||
// OR, if not shimming via package.json "browser" field:
|
||||
// var dgram = require('RCTUDP')
|
||||
var socket = dgram.createSocket('udp4')
|
||||
socket.bind(12345)
|
||||
socket.once('listening', function() {
|
||||
var buf = toByteArray('excellent!')
|
||||
socket.send(buf, 0, buf.length, remotePort, remoteHost, function(err) {
|
||||
if (err) throw err
|
||||
|
||||
console.log('message was sent')
|
||||
})
|
||||
})
|
||||
|
||||
socket.on('message', function(msg, rinfo) {
|
||||
console.log('message was received', msg)
|
||||
})
|
||||
|
||||
### Note
|
||||
|
||||
If you want to send and receive node Buffer objects, you'll have to "npm install buffer" and set it as a global for RCTUDP to pick it up:
|
||||
|
||||
```js
|
||||
global.Buffer = global.Buffer || require('buffer').Buffer
|
||||
```
|
||||
|
||||
## Contributors
|
||||
|
||||
[Tradle, Inc.](https://github.com/tradle/about/wiki)
|
|
@ -6,7 +6,7 @@
|
|||
//
|
||||
|
||||
/**
|
||||
* @providesModule RCTUDPSocket
|
||||
* @providesModule UdpSocket
|
||||
* @flow
|
||||
*/
|
||||
|
||||
|
@ -20,7 +20,7 @@ var {
|
|||
var mixInEventEmitter = require('mixInEventEmitter')
|
||||
var DeviceEventEmitter = require('RCTDeviceEventEmitter')
|
||||
var NativeModules = require('NativeModules')
|
||||
var sockets = NativeModules.UDP // just UDP for now
|
||||
var sockets = NativeModules.UdpSockets
|
||||
var noop = function () {}
|
||||
var instances = 0
|
||||
var STATE = {
|
||||
|
@ -61,13 +61,19 @@ class RCTSocket extends Component {
|
|||
|
||||
bind(port, address, callback) {
|
||||
var self = this
|
||||
|
||||
if (this._state !== STATE.UNBOUND) throw new Error('Socket is already bound')
|
||||
|
||||
if (typeof address === 'function') {
|
||||
callback = address
|
||||
address = undefined
|
||||
}
|
||||
|
||||
// address = address || '0.0.0.0' //'127.0.0.1'
|
||||
if (callback) this.once('listening', callback)
|
||||
if (!address) address = '0.0.0.0'
|
||||
|
||||
if (!port) port = 0
|
||||
|
||||
if (callback) this.once('listening', callback.bind(this))
|
||||
|
||||
this._state = STATE.BINDING
|
||||
this._debug('binding, address:', address, 'port:', port)
|
||||
|
@ -80,6 +86,7 @@ class RCTSocket extends Component {
|
|||
return self.emit('error', err)
|
||||
}
|
||||
|
||||
self._debug('bound to address:', addr.address, 'port:', addr.port)
|
||||
self._address = addr.address
|
||||
self._port = addr.port
|
||||
self._state = STATE.BOUND
|
|
@ -25,15 +25,15 @@ enum RCTUDPError
|
|||
|
||||
typedef enum RCTUDPError RCTUDPError;
|
||||
|
||||
@class RCTUDPClient;
|
||||
@class UdpSocketClient;
|
||||
|
||||
@protocol SocketClientDelegate <NSObject>
|
||||
|
||||
- (void)onData:(RCTUDPClient*) 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;
|
||||
|
||||
@end
|
||||
|
||||
@interface RCTUDPClient : NSObject
|
||||
@interface UdpSocketClient : NSObject
|
||||
|
||||
@property (nonatomic, retain) NSString* id;
|
||||
@property (nonatomic, retain) NSString* host;
|
|
@ -8,13 +8,13 @@
|
|||
|
||||
#import <netinet/in.h>
|
||||
#import <arpa/inet.h>
|
||||
#import "RCTUDPClient.h"
|
||||
#import "UdpSocketClient.h"
|
||||
#import "RCTBridgeModule.h"
|
||||
#import "GCDAsyncUdpSocket.h"
|
||||
|
||||
NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain";
|
||||
|
||||
@interface RCTUDPClient()
|
||||
@interface UdpSocketClient()
|
||||
{
|
||||
@private
|
||||
uint16_t _port;
|
||||
|
@ -29,7 +29,7 @@ NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain";
|
|||
|
||||
@end
|
||||
|
||||
@implementation RCTUDPClient
|
||||
@implementation UdpSocketClient
|
||||
|
||||
+ (id)socketClientWithConfig:(id<SocketClientDelegate>)delegate
|
||||
{
|
||||
|
@ -66,14 +66,14 @@ NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain";
|
|||
if (address) {
|
||||
struct sockaddr_in ip;
|
||||
ip.sin_family = AF_INET;
|
||||
ip.sin_port = htons(6003);
|
||||
ip.sin_port = htons(_port);
|
||||
inet_pton(AF_INET, [address cStringUsingEncoding:NSASCIIStringEncoding], &ip.sin_addr);
|
||||
|
||||
NSData * hostAndPort = [NSData dataWithBytes:&ip length:sizeof(ip)];
|
||||
result = [_udpSocket bindToAddress:hostAndPort error:error];
|
||||
}
|
||||
else {
|
||||
result = [_udpSocket bindToPort:port error:error];
|
||||
result = [_udpSocket bindToPort:_port error:error];
|
||||
}
|
||||
|
||||
return result && [_udpSocket beginReceiving:error];
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Stub of UdpSockets for Android.
|
||||
*
|
||||
* @providesModule UdpSockets
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var warning = require('warning');
|
||||
|
||||
var UdpSockets = {
|
||||
test: function() {
|
||||
warning("Not yet implemented for Android.");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = UdpSockets;
|
|
@ -7,12 +7,14 @@
|
|||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "RCTUDPClient.h"
|
||||
#import <Availability.h>
|
||||
#import "GCDAsyncUdpSocket.h"
|
||||
#import "UdpSocketClient.h"
|
||||
#import "RCTBridgeModule.h"
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
|
||||
@interface RCTUDP : NSObject<SocketClientDelegate, RCTBridgeModule>
|
||||
@interface UdpSockets : NSObject<SocketClientDelegate, RCTBridgeModule>
|
||||
|
||||
@property(retain, nonatomic)NSMutableDictionary *clients;
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
|
||||
/**
|
||||
* @providesModule RCTUDP
|
||||
* @providesModule UdpSockets
|
||||
* @flow
|
||||
*/
|
||||
|
||||
var RCTSocket = require('RCTUDPSocket')
|
||||
var UdpSocket = require('UdpSocket')
|
||||
|
||||
module.exports = {
|
||||
createSocket: function(type) {
|
||||
return new RCTSocket({
|
||||
return new UdpSocket({
|
||||
type: type
|
||||
})
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
//
|
||||
// UdpSockets.m
|
||||
// react-native-udp
|
||||
//
|
||||
// Created by Mark Vayngrib on 5/8/15.
|
||||
// Copyright (c) 2015 Tradle, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RCTAssert.h"
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTConvert.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "RCTLog.h"
|
||||
#import "UdpSockets.h"
|
||||
#import "UdpSocketClient.h"
|
||||
|
||||
@implementation UdpSockets
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
@synthesize bridge = _bridge;
|
||||
|
||||
RCT_EXPORT_METHOD(createSocket:(NSString*)cId withOptions:(NSDictionary*)options)
|
||||
{
|
||||
if (!_clients) _clients = [[NSMutableDictionary alloc] init];
|
||||
|
||||
if (!cId) {
|
||||
RCTLogError(@"%@.createSocket called with nil id parameter.", [self class]);
|
||||
return;
|
||||
}
|
||||
|
||||
UdpSocketClient *client = [_clients objectForKey:cId];
|
||||
if (client) {
|
||||
RCTLogError(@"%@.createSocket called twice with the same id.", [self class]);
|
||||
return;
|
||||
}
|
||||
|
||||
client = [UdpSocketClient socketClientWithConfig:self];
|
||||
[_clients setObject:client forKey:cId];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(bind:(NSString*)cId
|
||||
port:(int)port
|
||||
address:(NSString *)address
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
UdpSocketClient* client = [self findClient:cId callback:callback];
|
||||
if (!client) return;
|
||||
|
||||
NSError *error = nil;
|
||||
if (![client bind:port address:address error:&error])
|
||||
{
|
||||
callback(@[error]);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(@[[NSNull null], [client address]]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(send:(NSString*)cId
|
||||
data:(NSData*)data
|
||||
port:(int)port
|
||||
address:(NSString*)address
|
||||
callback:(RCTResponseSenderBlock)callback) {
|
||||
UdpSocketClient* client = [self findClient:cId callback:callback];
|
||||
if (!client) return;
|
||||
|
||||
[client send:data remotePort:port remoteAddress:address callback:callback];
|
||||
if (callback) callback(@[]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(close:(NSString*)cId
|
||||
callback:(RCTResponseSenderBlock)callback) {
|
||||
UdpSocketClient* client = [self findClient:cId callback:callback];
|
||||
if (!client) return;
|
||||
|
||||
[client close];
|
||||
[_clients removeObjectForKey:cId];
|
||||
|
||||
if (callback) callback(@[]);
|
||||
}
|
||||
|
||||
- (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port
|
||||
{
|
||||
NSString *clientID = [[_clients allKeysForObject:client] objectAtIndex:0];
|
||||
NSPropertyListFormat format;
|
||||
NSArray* arr = [NSPropertyListSerialization propertyListFromData:data
|
||||
mutabilityOption:NSPropertyListMutableContainers
|
||||
format:&format
|
||||
errorDescription:NULL];
|
||||
|
||||
[self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"udp-%@-data", clientID]
|
||||
body:@{
|
||||
@"data": arr,
|
||||
@"address": host,
|
||||
@"port": [NSNumber numberWithInt:port]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
-(UdpSocketClient*)findClient:(NSString*)cId callback:(RCTResponseSenderBlock)callback
|
||||
{
|
||||
UdpSocketClient *client = [_clients objectForKey:cId];
|
||||
if (!client) {
|
||||
if (!callback) {
|
||||
RCTLogError(@"%@.missing callback parameter.", [self class]);
|
||||
}
|
||||
else {
|
||||
callback(@[[NSString stringWithFormat:@"no client found with id %@", cId]]);
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@end
|
|
@ -0,0 +1,268 @@
|
|||
// !$*UTF8*$!
|
||||
{
|
||||
archiveVersion = 1;
|
||||
classes = {
|
||||
};
|
||||
objectVersion = 46;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
13BE3DEE1AC21097009241FE /* UdpSockets.m in Sources */ = {isa = PBXBuildFile; fileRef = 13BE3DED1AC21097009241FE /* UdpSockets.m */; };
|
||||
7350006B1AFF9AB600ED3C82 /* UdpSocketClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 7350006A1AFF9AB600ED3C82 /* UdpSocketClient.m */; };
|
||||
73D9377D1AFF9EBE00450142 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73D9377C1AFF9EBE00450142 /* CFNetwork.framework */; };
|
||||
73D9377F1AFF9F6E00450142 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73D9377E1AFF9F6E00450142 /* UIKit.framework */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
58B511D91A9E6C8500147676 /* CopyFiles */ = {
|
||||
isa = PBXCopyFilesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
dstPath = "include/$(PRODUCT_NAME)";
|
||||
dstSubfolderSpec = 16;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
134814201AA4EA6300B7C361 /* libUdpSockets.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libUdpSockets.a; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
13BE3DEC1AC21097009241FE /* UdpSockets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UdpSockets.h; sourceTree = "<group>"; };
|
||||
13BE3DED1AC21097009241FE /* UdpSockets.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UdpSockets.m; sourceTree = "<group>"; };
|
||||
735000681AFF9AB600ED3C82 /* CocoaAsyncSocket */ = {isa = PBXFileReference; lastKnownFileType = folder; path = CocoaAsyncSocket; sourceTree = "<group>"; };
|
||||
735000691AFF9AB600ED3C82 /* UdpSocketClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UdpSocketClient.h; sourceTree = "<group>"; };
|
||||
7350006A1AFF9AB600ED3C82 /* UdpSocketClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UdpSocketClient.m; sourceTree = "<group>"; };
|
||||
73D9377C1AFF9EBE00450142 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
|
||||
73D9377E1AFF9F6E00450142 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
58B511D81A9E6C8500147676 /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
73D9377F1AFF9F6E00450142 /* UIKit.framework in Frameworks */,
|
||||
73D9377D1AFF9EBE00450142 /* CFNetwork.framework in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
134814211AA4EA7D00B7C361 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
134814201AA4EA6300B7C361 /* libUdpSockets.a */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
58B511D21A9E6C8500147676 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
73D9377E1AFF9F6E00450142 /* UIKit.framework */,
|
||||
73D9377C1AFF9EBE00450142 /* CFNetwork.framework */,
|
||||
735000681AFF9AB600ED3C82 /* CocoaAsyncSocket */,
|
||||
735000691AFF9AB600ED3C82 /* UdpSocketClient.h */,
|
||||
7350006A1AFF9AB600ED3C82 /* UdpSocketClient.m */,
|
||||
13BE3DEC1AC21097009241FE /* UdpSockets.h */,
|
||||
13BE3DED1AC21097009241FE /* UdpSockets.m */,
|
||||
134814211AA4EA7D00B7C361 /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
58B511DA1A9E6C8500147676 /* UdpSockets */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "UdpSockets" */;
|
||||
buildPhases = (
|
||||
58B511D71A9E6C8500147676 /* Sources */,
|
||||
58B511D81A9E6C8500147676 /* Frameworks */,
|
||||
58B511D91A9E6C8500147676 /* CopyFiles */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = UdpSockets;
|
||||
productName = RCTDataManager;
|
||||
productReference = 134814201AA4EA6300B7C361 /* libUdpSockets.a */;
|
||||
productType = "com.apple.product-type.library.static";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
58B511D31A9E6C8500147676 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastUpgradeCheck = 0610;
|
||||
ORGANIZATIONNAME = Facebook;
|
||||
TargetAttributes = {
|
||||
58B511DA1A9E6C8500147676 = {
|
||||
CreatedOnToolsVersion = 6.1.1;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "UdpSockets" */;
|
||||
compatibilityVersion = "Xcode 3.2";
|
||||
developmentRegion = English;
|
||||
hasScannedForEncodings = 0;
|
||||
knownRegions = (
|
||||
en,
|
||||
);
|
||||
mainGroup = 58B511D21A9E6C8500147676;
|
||||
productRefGroup = 58B511D21A9E6C8500147676;
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
58B511DA1A9E6C8500147676 /* UdpSockets */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
||||
/* Begin PBXSourcesBuildPhase section */
|
||||
58B511D71A9E6C8500147676 /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
7350006B1AFF9AB600ED3C82 /* UdpSocketClient.m in Sources */,
|
||||
13BE3DEE1AC21097009241FE /* UdpSockets.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
58B511ED1A9E6C8500147676 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu99;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
58B511EE1A9E6C8500147676 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = YES;
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu99;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
SDKROOT = iphoneos;
|
||||
VALIDATE_PRODUCT = YES;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
58B511F01A9E6C8500147676 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
HEADER_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
|
||||
"$(SRCROOT)/../../React/**",
|
||||
);
|
||||
LIBRARY_SEARCH_PATHS = "$(inherited)";
|
||||
OTHER_LDFLAGS = "-ObjC";
|
||||
PRODUCT_NAME = UdpSockets;
|
||||
SKIP_INSTALL = YES;
|
||||
USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/../../**";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
58B511F11A9E6C8500147676 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
HEADER_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
|
||||
"$(SRCROOT)/../../React/**",
|
||||
);
|
||||
LIBRARY_SEARCH_PATHS = "$(inherited)";
|
||||
OTHER_LDFLAGS = "-ObjC";
|
||||
PRODUCT_NAME = UdpSockets;
|
||||
SKIP_INSTALL = YES;
|
||||
USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/../../**";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "UdpSockets" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
58B511ED1A9E6C8500147676 /* Debug */,
|
||||
58B511EE1A9E6C8500147676 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "UdpSockets" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
58B511F01A9E6C8500147676 /* Debug */,
|
||||
58B511F11A9E6C8500147676 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
rootObject = 58B511D31A9E6C8500147676 /* Project object */;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:">
|
||||
</FileRef>
|
||||
</Workspace>
|
118
iOS/RCTUDP.m
118
iOS/RCTUDP.m
|
@ -1,118 +0,0 @@
|
|||
//
|
||||
// RCTUDP.m
|
||||
// react-native-udp
|
||||
//
|
||||
// Created by Mark Vayngrib on 5/8/15.
|
||||
// Copyright (c) 2015 Tradle, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RCTAssert.h"
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTConvert.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "RCTLog.h"
|
||||
#import "RCTUDP.h"
|
||||
#import "RCTUDPClient.h"
|
||||
|
||||
@implementation RCTUDP
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
@synthesize bridge = _bridge;
|
||||
|
||||
RCT_EXPORT_METHOD(createSocket:(NSString*)cId withOptions:(NSDictionary*)options)
|
||||
{
|
||||
if (!_clients) _clients = [[NSMutableDictionary alloc] init];
|
||||
|
||||
if (!cId) {
|
||||
RCTLogError(@"%@.createSocket called with nil id parameter.", [self class]);
|
||||
return;
|
||||
}
|
||||
|
||||
RCTUDPClient *client = [_clients objectForKey:cId];
|
||||
if (client) {
|
||||
RCTLogError(@"%@.createSocket called twice with the same id.", [self class]);
|
||||
return;
|
||||
}
|
||||
|
||||
client = [RCTUDPClient socketClientWithConfig:self];
|
||||
[_clients setObject:client forKey:cId];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(bind:(NSString*)cId
|
||||
port:(int)port
|
||||
address:(NSString *)address
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
RCTUDPClient* client = [self findClient:cId callback:callback];
|
||||
if (!client) return;
|
||||
|
||||
NSError *error = nil;
|
||||
if (![client bind:port address:address error:&error])
|
||||
{
|
||||
callback(@[error]);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(@[[NSNull null], [client address]]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(send:(NSString*)cId
|
||||
data:(NSData*)data
|
||||
port:(int)port
|
||||
address:(NSString*)address
|
||||
callback:(RCTResponseSenderBlock)callback) {
|
||||
RCTUDPClient* client = [self findClient:cId callback:callback];
|
||||
if (!client) return;
|
||||
|
||||
[client send:data remotePort:port remoteAddress:address callback:callback];
|
||||
if (callback) callback(@[]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(close:(NSString*)cId
|
||||
callback:(RCTResponseSenderBlock)callback) {
|
||||
RCTUDPClient* client = [self findClient:cId callback:callback];
|
||||
if (!client) return;
|
||||
|
||||
[client close];
|
||||
[_clients removeObjectForKey:cId];
|
||||
|
||||
if (callback) callback(@[]);
|
||||
}
|
||||
|
||||
- (void) onData:(RCTUDPClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port
|
||||
{
|
||||
NSString *clientID = [[_clients allKeysForObject:client] objectAtIndex:0];
|
||||
NSPropertyListFormat format;
|
||||
NSArray* arr = [NSPropertyListSerialization propertyListFromData:data
|
||||
mutabilityOption:NSPropertyListMutableContainers
|
||||
format:&format
|
||||
errorDescription:NULL];
|
||||
|
||||
[self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"udp-%@-data", clientID]
|
||||
body:@{
|
||||
@"data": arr,
|
||||
@"address": host,
|
||||
@"port": [NSNumber numberWithInt:port]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
-(RCTUDPClient*)findClient:(NSString*)cId callback:(RCTResponseSenderBlock)callback
|
||||
{
|
||||
RCTUDPClient *client = [_clients objectForKey:cId];
|
||||
if (!client) {
|
||||
if (!callback) {
|
||||
RCTLogError(@"%@.missing callback parameter.", [self class]);
|
||||
}
|
||||
else {
|
||||
callback(@[[NSString stringWithFormat:@"no client found with id %@", cId]]);
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@end
|
|
@ -12,6 +12,10 @@ var {
|
|||
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
|
||||
}
|
||||
|
|
10
package.json
10
package.json
|
@ -2,10 +2,13 @@
|
|||
"name": "react-native-udp",
|
||||
"version": "0.0.1",
|
||||
"description": "node's dgram API for react-native",
|
||||
"main": "./dgram.js",
|
||||
"main": "./UdpSockets.ios.js",
|
||||
"scripts": {
|
||||
"start": "exit 1"
|
||||
},
|
||||
"browser": {
|
||||
"dgram": "./dgram.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/tradle/react-native-udp.git"
|
||||
|
@ -27,5 +30,10 @@
|
|||
"homepage": "https://github.com/tradle/react-native-udp",
|
||||
"dependencies": {
|
||||
"react-native": "^0.4.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"assert": "^1.3.0",
|
||||
"buffer": "^3.2.2",
|
||||
"process": "^0.11.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue