React Antial TCP套接字API用於Android,iOS和MACOS具有SSL/TLS支持。它允許您創建TCP客戶端和服務器插座,模仿Node的Net和Node的TLS API功能(查看可用API以獲取更多信息)。
nettls使用任何一條紗線安裝庫:
yarn add react-native-tcp-socket
或NPM:
npm install --save react-native-tcp-socket
net由於react-native-tcp-socket提供與Node Net相同的API,如果您想在JavaScript中導入此模塊作為net或使用require('net') ,則必須在package.json文件中添加以下行。
{
"react-native" : {
"net" : " react-native-tcp-socket "
}
}此外,要獲取本模塊提供的TS類型(或自動完成),還必須將以下內容添加到自定義聲明文件中。
...
declare module 'net' {
import TcpSockets from 'react-native-tcp-socket' ;
export = TcpSockets ;
}如果要避免重複的net類型,請確保不要在tsconfig.json "typeRoots"屬性中使用默認的node_modules/@types 。
檢查為工作示例提供的示例應用程序。
tls tls模塊也是如此。但是,您應該知道以下內容:
Server類是非TLS。為了使用TLS服務器,您必須使用TLSServer類。您可以覆蓋默認Server類( tls.Server = tls.TLSServer )。 createServer()和connect()也是如此。為了使用TLS方法,您必須分別使用createTLSServer()和connectTLS()方法。您可以覆蓋默認方法( tls.createServer = tls.createTLSServer和tls.connect = tls.connectTLS )。tls模塊需要以字符串提供鍵和證書。但是, react-native-tcp-socket模塊要求將其導入require() 。此外,要獲取本模塊提供的TS類型(或自動完成),還必須將以下內容添加到自定義聲明文件中。
...
declare module 'tls' {
import TcpSockets from 'react-native-tcp-socket' ;
export const Server = TcpSockets . TLSServer ;
export const TLSSocket = TcpSockets . TLSSocket ;
export const connect = TcpSockets . connectTLS ;
export const createServer = TcpSockets . createTLSServer ;
}檢查為工作示例提供的示例應用程序。
自動鏈接不再需要手動鏈接包裝。
iOS平台:
$ cd ios && pod install && cd .. #iOS上的可可錄
Android平台:
修改您的android/build.gradle配置以匹配minSdkVersion = 21 :
buildscript {
ext {
...
minSdkVersion = 21
...
}
為了生成自簽名SSL所需的文件(密鑰和證書),您可以使用以下命令:
openssl genrsa -out server-key.pem 4096
openssl req -new -key server-key.pem -out server-csr.pem
openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem
openssl pkcs12 -export -out server-keystore.p12 -inkey server-key.pem -in server-cert.pem
注意: server-keystore.p12不得擁有密碼。
您將需要一個Metro.config.js文件才能使用自簽名的SSL證書。您應該已經在Root Project目錄中有此文件,但是如果您不這樣做,請創建它。在module.exports內部。Exports對象,創建一個稱為resolver的密鑰,並使用另一個稱為assetExts的對象。 assetExts的值應該是您要支持的資源文件擴展名的數組。
如果您想能夠使用.pem和.p12文件(加上所有已經支持的文件),則您的metro.config.js應該看起來像這樣:
const { getDefaultConfig } = require ( 'metro-config' ) ;
const defaultConfig = getDefaultConfig . getDefaultValues ( __dirname ) ;
module . exports = {
resolver : {
assetExts : [ ... defaultConfig . resolver . assetExts , 'pem' , 'p12' ] ,
} ,
// ...
} ; 然後,您需要將庫的本地部分鏈接到所使用的平台。鏈接庫的最簡單方法是通過從項目的根部運行此命令來使用CLI工具:
$ react-native link react-native-tcp-socket
如果您不能或不想使用CLI工具,也可以使用下面的說明手動鏈接庫(單擊箭頭以顯示它們):
Libraries Add Files to [your project's name]node_modules react-native-tcp-socket ,然後添加TcpSockets.xcodeprojlibTcpSockets.a添加到項目的Build Phases Link Binary With LibrariesCmd+R )<android/app/src/main/java/[...]/MainApplication.javaimport com.asterinet.react.tcpsocket.TcpSocketPackage;到文件頂部的導入new TcpSocketPackage()添加到getPackages()方法返回的列表android/settings.gradle : include ':react-native-tcp-socket'
project(':react-native-tcp-socket').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-tcp-socket/android')
android/app/build.gradle中插入依賴項塊中的以下行: implementation project(':react-native-tcp-socket')
要使用此庫,您需要確保使用正確版本的React Native。如果您使用的是低於0.60的React本機版本,則需要在嘗試使用最新版本之前升級。
react-native-tcp-socket版本 | 必需的反應本機版本 |
|---|---|
6.XX , 5.XX , 4.XX , 3.XX | >= 0.60.0 |
1.4.0 | >= Unknown |
導入庫:
import TcpSocket from 'react-native-tcp-socket' ;
// const net = require('react-native-tcp-socket');
// const tls = require('react-native-tcp-socket'); const options = {
port : port ,
host : '127.0.0.1' ,
localAddress : '127.0.0.1' ,
reuseAddress : true ,
// localPort: 20000,
// interface: "wifi",
} ;
// Create socket
const client = TcpSocket . createConnection ( options , ( ) => {
// Write on the socket
client . write ( 'Hello server!' ) ;
// Close socket
client . destroy ( ) ;
} ) ;
client . on ( 'data' , function ( data ) {
console . log ( 'message was received' , data ) ;
} ) ;
client . on ( 'error' , function ( error ) {
console . log ( error ) ;
} ) ;
client . on ( 'close' , function ( ) {
console . log ( 'Connection closed!' ) ;
} ) ; const server = TcpSocket . createServer ( function ( socket ) {
socket . on ( 'data' , ( data ) => {
socket . write ( 'Echo server ' + data ) ;
} ) ;
socket . on ( 'error' , ( error ) => {
console . log ( 'An error ocurred with client socket ' , error ) ;
} ) ;
socket . on ( 'close' , ( error ) => {
console . log ( 'Closed connection with ' , socket . address ( ) ) ;
} ) ;
} ) . listen ( { port : 12345 , host : '0.0.0.0' } ) ;
server . on ( 'error' , ( error ) => {
console . log ( 'An error ocurred with the server' , error ) ;
} ) ;
server . on ( 'close' , ( ) => {
console . log ( 'Server closed connection' ) ;
} ) ; const options = {
port : port ,
host : '127.0.0.1' ,
localAddress : '127.0.0.1' ,
reuseAddress : true ,
// localPort: 20000,
// interface: "wifi",
ca : require ( 'server-cert.pem' ) ,
} ;
// Create socket
const client = TcpSocket . connectTLS ( options , ( ) => {
// Write on the socket
client . write ( 'Hello server!' ) ;
// Close socket
client . destroy ( ) ;
} ) ;
client . on ( 'data' , function ( data ) {
console . log ( 'message was received' , data ) ;
} ) ;
client . on ( 'error' , function ( error ) {
console . log ( error ) ;
} ) ;
client . on ( 'close' , function ( ) {
console . log ( 'Connection closed!' ) ;
} ) ; const options = {
keystore : require ( 'server-keystore.p12' ) ,
} ;
const server = TcpSocket . createTLSServer ( options , function ( socket ) {
socket . on ( 'data' , ( data ) => {
socket . write ( 'Echo server ' + data ) ;
} ) ;
socket . on ( 'error' , ( error ) => {
console . log ( 'An error ocurred with SSL client socket ' , error ) ;
} ) ;
socket . on ( 'close' , ( error ) => {
console . log ( 'SSL closed connection with ' , socket . address ( ) ) ;
} ) ;
} ) . listen ( { port : 12345 , host : '0.0.0.0' } ) ;
server . on ( 'error' , ( error ) => {
console . log ( 'An error ocurred with the server' , error ) ;
} ) ;
server . on ( 'close' , ( ) => {
console . log ( 'Server closed connection' ) ;
} ) ;注意:為了使用自簽名證書,請確保更新您的Metro.config.js配置。
此處列出了在react-native-tcp-socket中實現的所有方法,該方法模仿了節點的NET API,它們的功能與Node Net提供的功能相當(有關#41的更多信息)。但是,接口與節點不同的方法以粗體標記。
net.connect(options[, callback])net.createConnection(options[, callback])net.createServer(connectionListener)net.isIP(input)net.isIPv4(input)net.isIPv6(input)address()destroy([error])end([data][, encoding][, callback])setEncoding([encoding])setKeepAlive([enable][, initialDelay]) initialDelaysetNoDelay([noDelay])setTimeout(timeout[, callback])write(data[, encoding][, callback])pause()ref() -不會有任何效果resume()unref() -不會有任何效果Stream.Writable .writableNeedDrainbytesReadbytesWrittenconnectingdestroyedlocalAddresslocalPortremoteAddressremoteFamilyremotePortpendingtimeoutreadyStateStream.Readable .'pause''resume''close''connect''data''drain''error''timeout'net.createConnection() net.createConnection(options[, callback])使用給定options創建TCP連接。 options參數必須是具有以下屬性的object :
| 財產 | 類型 | iOS/macOS | 安卓 | 描述 |
|---|---|---|---|---|
port | <number> | 必需的。端口應連接到插座。 | ||
host | <string> | 主機應連接到插座。 IPv4格式或'localhost'的IP地址。默認值: 'localhost' 。 | ||
localAddress | <string> | 本地地址應從插座連接。如果未指定,則操作系統將決定。強烈建議指定localAddress以防止過載錯誤並提高性能。 | ||
localPort | <number> | 本地端口應從插座連接。如果未指定,則操作系統將決定。 | ||
interface | <string> | 接口應從插座連接。如果未指定,它將使用當前的活動連接。選項是: 'wifi', 'ethernet', 'cellular' 。 | ||
reuseAddress | <boolean> | 啟用/禁用ReuseadDress插座選項。默認值: true 。 |
注意:標記為使用默認值的平台。
address()listen(options[, callback])close([callback])getConnections(callback)listening'close''connection''error''listening'Server.listen() Server.listen(options[, callback])使用給定options創建一個TCP服務器套接字。 options參數必須是具有以下屬性的object :
| 財產 | 類型 | iOS/macOS | 安卓 | 描述 |
|---|---|---|---|---|
port | <number> | 必需的。端口應聆聽插座。 | ||
host | <string> | 主持插座應該聽。 IPv4格式或'localhost'的IP地址。默認值: '0.0.0.0' 。 | ||
reuseAddress | <boolean> | 啟用/禁用ReuseadDress插座選項。默認值: true 。 |
注意:標記為使用默認值的平台。
以下列出了在react-native-tcp-socket中實現的所有方法,該方法模仿了Node的TLS API,它們的功能等同於Node的TLS提供的功能。但是,接口與節點不同的方法以粗體標記。
tls.connectTLS(options[, callback])tls.createTLSServer([options][, secureConnectionListener])Socket的所有方法getCertificate()僅AndroidgetPeerCertificate() AndroidSocket的所有屬性Socket的所有事件'secureConnect'tls.connectTLS() tls.connectTLS(options[, callback])使用給定options創建一個TLS套接字連接。 options參數必須是具有以下屬性的object :
| 財產 | 類型 | iOS/macOS | 安卓 | 描述 |
|---|---|---|---|---|
ca | <import> | CA文件(.pem格式)信任。如果null ,它將使用設備的默認SSL受信任列表。對於自簽名證書有用。檢查文檔以生成該文件。默認值: null 。 | ||
key | <import> | 私有密鑰文件(.pem格式)。檢查文檔以生成該文件。 | ||
cert | <import> | 公共證書文件(.PEM格式)。檢查文檔以生成該文件。 | ||
androidKeyStore | <string> | Android鑰匙店別名。 | ||
certAlias | <string> | Android密鑰店證書別名。 | ||
keyAlias | <string> | Android密鑰店私鑰別名。 | ||
... | <any> | 尚未列出的任何其他socket.connect()選項。 |
注意:TLS服務器在Node的TLS中命名為Server ,但在react-native-tcp-socket中命名為TLSServer ,以避免與Server類混淆。
Server的所有方法setSecureContext(options)Server的所有屬性Server的所有事件'secureConnection'tls.createTLSServer() tls.createTLSServer([options][, secureConnectionListener])創建了一個新的tls.TLSServer 。 secureConnectionListener (如果提供)將自動設置為'secureConnection'事件的偵聽器。 options參數必須是具有以下屬性的object :
| 財產 | 類型 | iOS/macOS | 安卓 | 描述 |
|---|---|---|---|---|
keystore | <import> | 必需的。 PKCS#12格式的鍵存儲,帶有服務器證書和私鑰。檢查文檔以生成該文件。 |
圖書館根據麻省理工學院許可發布。有關更多信息,請參閱LICENSE 。