# Socket
[![Build Status](https://travis-ci.org/reactphp/socket.svg?branch=master)](https://travis-ci.org/reactphp/socket)
Async, streaming plaintext TCP/IP and secure TLS socket server and client
connections for [ReactPHP](https://reactphp.org/).
The socket library provides re-usable interfaces for a socket-layer
server and client based on the [`EventLoop`](https://github.com/reactphp/event-loop)
and [`Stream`](https://github.com/reactphp/stream) components.
Its server component allows you to build networking servers that accept incoming
connections from networking clients (such as an HTTP server).
Its client component allows you to build networking clients that establish
outgoing connections to networking servers (such as an HTTP or database client).
This library provides async, streaming means for all of this, so you can
handle multiple concurrent connections without blocking.
**Table of Contents**
* [Quickstart example](#quickstart-example)
* [Connection usage](#connection-usage)
* [ConnectionInterface](#connectioninterface)
* [getRemoteAddress()](#getremoteaddress)
* [getLocalAddress()](#getlocaladdress)
* [Server usage](#server-usage)
* [ServerInterface](#serverinterface)
* [connection event](#connection-event)
* [error event](#error-event)
* [getAddress()](#getaddress)
* [pause()](#pause)
* [resume()](#resume)
* [close()](#close)
* [Server](#server)
* [Advanced server usage](#advanced-server-usage)
* [TcpServer](#tcpserver)
* [SecureServer](#secureserver)
* [UnixServer](#unixserver)
* [LimitingServer](#limitingserver)
* [getConnections()](#getconnections)
* [Client usage](#client-usage)
* [ConnectorInterface](#connectorinterface)
* [connect()](#connect)
* [Connector](#connector)
* [Advanced client usage](#advanced-client-usage)
* [TcpConnector](#tcpconnector)
* [DnsConnector](#dnsconnector)
* [SecureConnector](#secureconnector)
* [TimeoutConnector](#timeoutconnector)
* [UnixConnector](#unixconnector)
* [FixUriConnector](#fixeduriconnector)
* [Install](#install)
* [Tests](#tests)
* [License](#license)
## Quickstart example
Here is a server that closes the connection if you send it anything:
```php
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:8080', $loop);
$socket->on('connection', function (ConnectionInterface $conn) {
$conn->write("Hello " . $conn->getRemoteAddress() . "!\n");
$conn->write("Welcome to this amazing server!\n");
$conn->write("Here's a tip: don't say anything.\n");
$conn->on('data', function ($data) use ($conn) {
$conn->close();
});
});
$loop->run();
```
See also the [examples](examples).
Here's a client that outputs the output of said server and then attempts to
send it a string:
```php
$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);
$connector->connect('127.0.0.1:8080')->then(function (ConnectionInterface $conn) use ($loop) {
$conn->pipe(new React\Stream\WritableResourceStream(STDOUT, $loop));
$conn->write("Hello World!\n");
});
$loop->run();
```
## Connection usage
### ConnectionInterface
The `ConnectionInterface` is used to represent any incoming and outgoing
connection, such as a normal TCP/IP connection.
An incoming or outgoing connection is a duplex stream (both readable and
writable) that implements React's
[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
It contains additional properties for the local and remote address (client IP)
where this connection has been established to/from.
Most commonly, instances implementing this `ConnectionInterface` are emitted
by all classes implementing the [`ServerInterface`](#serverinterface) and
used by all classes implementing the [`ConnectorInterface`](#connectorinterface).
Because the `ConnectionInterface` implements the underlying
[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface)
you can use any of its events and methods as usual:
```php
$connection->on('data', function ($chunk) {
echo $chunk;
});
$connection->on('end', function () {
echo 'ended';
});
$connection->on('error', function (Exception $e) {
echo 'error: ' . $e->getMessage();
});
$connection->on('close', function () {
echo 'closed';
});
$connection->write($data);
$connection->end($data = null);
$connection->close();
// …
```
For more details, see the
[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
#### getRemoteAddress()
The `getRemoteAddress(): ?string` method returns the full remote address
(URI) where this connection has been established with.
```php
$address = $connection->getRemoteAddress();
echo 'Connection with ' . $address . PHP_EOL;
```
If the remote address can not be determined or is unknown at this time (such as
after the connection has been closed), it MAY return a `NULL` value instead.
Otherwise, it will return the full address (URI) as a string value, such
as `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`,
`unix://example.sock` or `unix:///path/to/example.sock`.
Note that individual URI components are application specific and depend
on the underlying transport protocol.
If this is a TCP/IP based connection and you only want the remote IP, you may
use something like this:
```php
$address = $connection->getRemoteAddress();
$ip = trim(parse_url($address, PHP_URL_HOST), '[]');
echo 'Connection with ' . $ip . PHP_EOL;
```
#### getLocalAddress()
The `getLocalAddress(): ?string` method returns the full local address
(URI) where this connection has been established with.
```php
$address = $connection->getLocalAddress();
echo 'Connection with ' . $address . PHP_EOL;
```
If the local address can not be determined or is unknown at this time (such as
after the connection has been closed), it MAY return a `NULL` value instead.
Otherwise, it will return the full address (URI) as a string value, such
as `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`,
`unix://example.sock` or `unix:///path/to/example.sock`.
Note that individual URI components are application specific and depend
on the underlying transport protocol.
This method complements the [`getRemoteAddress()`](#getremoteaddress) method,
so they should not be confused.
If your `TcpServer` instance is listening on multiple interfaces (e.g. using
the address `0.0.0.0`), you can use this method to find out which interface
actually accepted this connection (such as a public or local interface).
If your system has multiple interfaces (e.g. a WAN and a LAN interface),
you can use this method to find out which interface was actually
used for this connection.
## Server usage
### ServerInterface
The `ServerInterface` is responsible for providing an interface for accepting
incoming streaming connections, such as a normal TCP/IP connection.
Most higher-level components (such as a HTTP server) accept an instance
implementing this interface to accept incoming streaming connections.
This is usually done via dependency injection, so it's fairly simple to actually
swap this implementation against any other implementation of this interface.
This means that you SHOULD typehint against this interface instead of a concrete
implementation of this interface.
Besides defining a few methods, this interface also implements the
[`EventEmitterInterface`](https://github.com/igorw/evenement)
which allows you to react to certain events.
#### connection event
The `connection` event will be emitted whenever a new connection has been
established, i.e. a new client connects to this server socket:
```php
$server->on('connection', function (ConnectionInterface $connection) {
echo 'new connection' . PHP_EOL;
});
```
See also the [`ConnectionInterface`](#connectioninterface) for more details
about handling the incoming connection.
#### error event
The `error` event will be emit
没有合适的资源?快使用搜索试试~ 我知道了~
【WordPress插件】2022年最新版完整功能demo+插件9 November 19.zip
共1384个文件
php:1177个
md:57个
json:30个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 184 浏览量
2022-04-02
02:57:27
上传
评论
收藏 1.81MB ZIP 举报
温馨提示
"【WordPress插件】2022年最新版完整功能demo+插件9 November 19 Instagram Auto Reply with Artificial Intelligence - Chat Bot Instagram自动回复与人工智能 - 聊天机器人" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目源码、ios项目源码,更有超10000个资源可供选择,如有需要请站内联系。
资源推荐
资源详情
资源评论
收起资源包目录
【WordPress插件】2022年最新版完整功能demo+插件9 November 19.zip (1384个子文件)
lazydoctor.bat 138B
resolv.conf 19B
eksipic-cookies.dat 2KB
fazilakbulut15-cookies.dat 2KB
fazilakbulut15-settings.dat 590B
eksipic-settings.dat 586B
asdasd-settings.dat 553B
fazilakbuluts-settings.dat 548B
fazilakbuluts-cookies.dat 517B
asdasd-cookies.dat 517B
.php_cs.dist 2KB
.php_cs.dist 2KB
phpunit.xml.dist 960B
phpunit.xml.dist 834B
phpunit.xml.dist 770B
phpunit.xml.dist 666B
phpunit.xml.dist 666B
phpunit.xml.dist 666B
phpunit.xml.dist 639B
phpunit.xml.dist 637B
phpunit.xml.dist 507B
phpunit.xml.dist 501B
phpunit.xml.dist 411B
phpunit.xml.dist 363B
phpunit.xml.dist 337B
phpunit.xml.dist 233B
phpstan.neon.dist 196B
Dockerfile 1KB
Dockerfile 387B
Dockerfile 202B
.gitignore 593B
.gitignore 507B
.gitignore 134B
.gitignore 55B
.gitignore 47B
.gitignore 34B
.gitignore 33B
.gitignore 24B
.gitignore 23B
.gitignore 23B
.gitignore 21B
.gitignore 21B
.gitignore 21B
.gitignore 21B
.gitignore 21B
.gitignore 20B
.gitignore 9B
.gitignore 9B
.gitignore 7B
.pre-commit.hook 3KB
.pre-commit.hook 2KB
webwarning.htm 3KB
docs.html 93B
faqs.html 93B
package.ini 423B
phprelease.ini 112B
installed.json 42KB
composer.json 2KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 1KB
composer.json 971B
composer.json 961B
composer.json 887B
composer.json 885B
composer.json 823B
composer.json 786B
composer.json 757B
composer.json 746B
composer.json 741B
composer.json 662B
composer.json 647B
composer.json 621B
composer.json 561B
composer.json 514B
composer.json 508B
composer.json 504B
composer.json 465B
composer.json 463B
composer.json 456B
.ac-php-conf.json 403B
.ac-php-conf.json 282B
composer.json 105B
lazydoctor 31KB
lazydoctor 375B
LICENSE 34KB
LICENSE 31KB
LICENSE 11KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
共 1384 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
Lee达森
- 粉丝: 968
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功