isathais1 / tcp-socket
Laravel 5 的 TCP Socket
dev-master
2018-04-13 18:42 UTC
Requires
- php: >=5.6.4
- illuminate/support: 5.*
This package is not auto-updated.
Last update: 2024-09-22 09:03:43 UTC
README
安装
要安装此包,请编辑您项目的 composer.json
文件,添加 isathais1/tcp-socket。
"require": { "isathais1/tcp-socket": "dev-master" },
现在,更新 Composer。
composer update
Composer 完成后,您需要添加服务提供者。打开 config/app.php
,并将新项目添加到 providers 数组中。
'isathais1\Socket\SocketServiceProvider',
接下来,添加一个 Facade 以便于使用。在 config/app.php
中,向 aliases 数组添加以下行
'Socket' => 'isathais1\Socket\Facades\Socket',
发布配置文件
php artisan vendor:publish --provider="isathais1\Socket\SocketServiceProvider"
要更改用户名、密码和其他配置,请修改 config/tcp-socket.php
用法
### 配置 打开 config/tcp-socket.php
输入服务器 IP 地址和端口
您可以在 SOL_TCP
和 SOL_UDP
之间更改服务器协议
连接到服务器
Socket::connect(); Socket::connect($ip); Socket::connect(null, $port); Socket::connect($ip, $port);
从服务器断开连接
Socket::disconnect();
发送消息
Socket::sendMessage('test message'); //send message to connected server Socket::sendMessageTo('test message', 'server ip', server port) // send message to a socket
读取消息
$response = Socket::readMessage(); //Read message with 2048 byte buffer Or $response = Socket::readMessage($length)//Read message with custom byte buffer; $response = Socket::readMessage($length,$type)//Read message with custom byte buffer and custom type(PHP_NORMAL_READ or PHP_BINARY_READ);