在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Rapsssito/react-native-tcp-socket开源软件地址(OpenSource Url):https://github.com/Rapsssito/react-native-tcp-socket开源编程语言(OpenSource Language):Java 36.7%开源软件介绍(OpenSource Introduction):react-native-tcp-socket
React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support. It allows you to create TCP client and server sockets, imitating Node's net API functionalities (check the available API for more information). Table of ContentsGetting startedInstall the library using either Yarn:
or npm:
Overriding |
react-native-tcp-socket version |
Required React Native Version |
---|---|
5.X.X , 4.X.X , 3.X.X |
>= 0.60.0 |
1.4.0 |
>= Unknown |
Import the library:
import TcpSocket from 'react-native-tcp-socket';
// const net = require('react-native-tcp-socket');
// 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 client = TcpSocket.createConnection({
port: 8443,
host: "example.com",
tls: true,
// tlsCheckValidity: false, // Disable validity checking
// tlsCert: require('./selfmade.pem') // Self-signed certificate
});
// ...
Note: In order to use self-signed certificates make sure to update your metro.config.js configuration.
Here are listed all methods implemented in react-native-tcp-socket
, their functionalities are equivalent to those provided by Node's net (more info on #41). However, the methods whose interface differs from Node are marked in bold.
TcpSocket.createConnection(options[, callback])
address()
destroy([error])
end([data][, encoding][, callback])
setEncoding([encoding])
setKeepAlive([enable][, initialDelay])
- initialDelay
is ignoredsetNoDelay([noDelay])
setTimeout(timeout[, callback])
write(data[, encoding][, callback])
pause()
ref()
- Will not have any effectresume()
unref()
- Will not have any effectcreateConnection()
createConnection(options[, callback])
creates a TCP connection using the given options
.
createConnection: options
Required. Available options for creating a socket. It must be an object
with the following properties:
Property | Type | iOS/macOS | Android | Description |
---|---|---|---|---|
port |
<number> |
Required. Port the socket should connect to. | ||
host |
<string> |
Host the socket should connect to. IP address in IPv4 format or 'localhost' . Default: 'localhost' . |
||
timeout |
<number> |
If set, will be used to call setTimeout(timeout) after the socket is created, but before it starts the connection. |
||
localAddress |
<string> |
Local address the socket should connect from. If not specified, the OS will decide. It is highly recommended to specify a localAddress to prevent overload errors and improve performance. |
||
localPort |
<number> |
Local port the socket should connect from. If not specified, the OS will decide. | ||
interface |
<string> |
Interface the socket should connect from. If not specified, it will use the current active connection. The options are: 'wifi', 'ethernet', 'cellular' . |
||
reuseAddress |
<boolean> |
Enable/disable the reuseAddress socket option. Default: true . |
||
tls |
<boolean> |
Enable/disable SSL/TLS socket creation. Default: false . |
||
tlsCheckValidity |
<boolean> |
Enable/disable SSL/TLS certificate validity check. Default: true . |
||
tlsCert |
<any> |
CA file (.pem format) to trust. If null , it will use the device's default SSL trusted list. Useful for self-signed certificates. See example for more info. Default: null . |
Note: The platforms marked as
listen()
listen(options[, callback])
creates a TCP server socket using the given options
.
listen: options
Required. Available options for creating a server socket. It must be an object
with the following properties:
Property | Type | iOS/macOS | Android | Description |
---|---|---|---|---|
port |
<number> |
Required. Port the socket should listen to. | ||
host |
<string> |
Host the socket should listen to. IP address in IPv4 format or 'localhost' . Default: '0.0.0.0' . |
||
reuseAddress |
<boolean> |
Enable/disable the reuseAddress socket option. Default: true . |
Note: The platforms marked as
The library is released under the MIT license. For more information see LICENSE
.
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论