# php-socket **Repository Path**: jinko/php-socket ## Basic Information - **Project Name**: php-socket - **Description**: No description available - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-04-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## **socket 的用法** - **服务端** ```php error_reporting(E_ALL ^ E_NOTICE); use Net\Connection; use Net\Event; require_once './Net/Connection.php'; require_once './Net/Event.php'; $server = new Connection; $server ->onError(function(Event $e) { echo "服务错误: {$e->getErrorCode()} {$e->getErrorMessage()}\n"; die; }) //服务已就绪 ->onReady(function() use($server) { $bindInfo = $server->getBindInfo(); echo "服务已就绪, 监听地址 {$bindInfo['address']}:{$bindInfo['port']}\n"; }) //新连接进入 ->onNewConnect(function (Event $e) use($server) { $connection = $e->getConnection(); echo "新连接: $connection\n"; $connection ->onRecv(function(Event $e) use($connection, $server) { $message = $e->getMessage(); if(strtolower($message) == 'exit') { $server->close(); return; } echo "收到来自客户端{$connection}的数据: {$message}\n"; $connection->send('数据已收到!'); }) ->onClose(function() use($connection) { echo "$connection 已关闭\n"; }) ; }) //服务退出 ->onClose(function() { echo "服务已退出\n"; die; }) ; $server->listening('0.0.0.0', 30001); Connection::dispatch(); ``` - **客户端(阻塞方法)** ```php error_reporting(E_ALL ^ E_NOTICE); use Net\Connection; require_once './Net/Connection.php'; require_once './Net/Event.php'; $client = new Connection(); //则塞模式 $client->setBlock(true); $client->connect('127.0.0.1', 30001); if($client->isReady()) { echo "连接成功!\n"; $client->send('hello hello hello'); sleep(1); $client->send('are you ok'); sleep(1); $message = $client->recv(); echo "服务器回复: $message\n"; sleep(3); $client->send('exit'); } else { echo "连接错误: {$client->getLastError()} {$client->getLastErrorMessage()}\n"; } ``` - **客户端(非阻塞方法)** ```php error_reporting(E_ALL ^ E_NOTICE); use Net\Connection; use Net\Event; require_once './Net/Connection.php'; require_once './Net/Event.php'; $client = new Connection(); $client ->onReady(function() use($client) { echo "连接成功\n"; $client->send('hello hello hello'); sleep(1); $client->send('are you ok'); sleep(1); $message = $client->recv(); echo "服务器回复: $message\n"; sleep(3); $client->send('exit'); }) ->onRecv(function(Event $e) { echo "服务端回复: {$e->getMessage()}\n"; }) ->onConnectFail(function(Event $e) use($client) { echo "连接失败: {$e->getErrorCode()} {$e->getErrorMessage()}\n"; die; }) ->onClose(function() { die; }) ; $client->connect('127.0.0.1', 30001); Connection::dispatch(); ``` ## websocket 用法 - 服务端 ```php error_reporting(E_ALL ^ E_NOTICE); use Net\Event; use Websocket\WebsocketConnection; use Websocket\WebsocketServer; require_once './Websocket/WebsocketServer.php'; require_once './Websocket/WebsocketConnection.php'; require_once './Net/Event.php'; $server = new WebsocketServer(); $server ->onError(function(Event $e) { echo "服务错误: {$e->getErrorCode()} {$e->getErrorMessage()}\n"; die; }) //服务已就绪 ->onReady(function() use($server) { $bindInfo = $server->getBindInfo(); echo "服务已就绪, 监听地址 {$bindInfo['address']}:{$bindInfo['port']}\n"; }) //新连接进入 ->onHandshake(function (Event $e) use($server) { /** @var WebsocketConnection $connection */ $connection = $e->getConnection(); echo "新连接: $connection\n"; $connection ->onMessage(function(Event $e) use($connection, $server) { $message = $e->getMessage(); if(strtolower($message) == 'exit') { $server->close(); return; } echo "收到来自客户端{$connection}的数据: {$message}\n"; $connection->sendMessage('数据已收到!'); }) ->onClose(function() use($connection) { echo "$connection 已关闭\n"; }) ; }) //服务退出 ->onClose(function() { echo "服务已退出\n"; die; }) ; $server->listening('0.0.0.0', 30002); WebsocketConnection::dispatch(); ``` - **客户端(阻塞)** ```php error_reporting(E_ALL ^ E_NOTICE); use Websocket\WebsocketClient; require_once './Websocket/WebsocketClient.php'; require_once './Websocket/WebsocketConnection.php'; require_once './Net/Event.php'; $client = new WebsocketClient(); //则塞模式 $client->setBlock(true); $client->connect('127.0.0.1', 30002); if($client->handshake) { echo "连接成功!\n"; $client->sendMessage('hello hello hello'); sleep(1); $client->sendMessage('are you ok'); sleep(1); $message = $client->recvMessage(); echo "服务器回复: $message\n"; echo "继续发送\n"; $client->sendMessage('hello'); sleep(1); $client->sendMessage('are you ok'); sleep(3); $buffer = $client->recvMessage(true); $messages = $buffer->fetchAllMessages(); echo "收到的服务器回复: " . implode(' ', $messages) . "\n"; sleep(3); $client->sendMessage('exit'); } else { echo "连接错误: {$client->getLastError()} {$client->getLastErrorMessage()}\n"; } ``` - **客户端(非阻塞)** ```php error_reporting(E_ALL ^ E_NOTICE); use Net\Event; use Websocket\WebsocketClient; use Websocket\WebsocketConnection; require_once './Websocket/WebsocketClient.php'; require_once './Websocket/WebsocketConnection.php'; require_once './Net/Event.php'; $client = new WebsocketClient(); $client ->onReady(function() use($client) { echo "连接成功\n"; $client->sendMessage('hello hello hello'); sleep(1); $client->sendMessage('are you ok'); sleep(3); $client->sendMessage('exit'); }) ->onMessage(function(Event $e) { echo "收到的服务器回复: {$e->getMessage()}\n"; }) ->onConnectFail(function(Event $e) use($client) { echo "连接失败: {$e->getErrorCode()} {$e->getErrorMessage()}\n"; die; }) ->onClose(function() { die; }) ; $client->connect('127.0.0.1', 30002); WebsocketConnection::dispatch(); ```