swoole-src
swoole-src copied to clipboard
[QUESTION] How to reuse TCPClient on coroutines.
Please answer these questions before submitting your issue. Thanks!
- What did you do? If possible, provide a simple script for reproducing the error. I'm trying to replace and old implementation of the deprecated async client and i'm trying to implement the coroutine client as far as I know all the calls to the coroutine client needs to be wrapped on the go() function, i will like to call the functions and then redirect the results to others functions but when i call i think there are more coroutines linked to the client, what i would like to know is if there is a way to access to this properties and functions on the swoole and correct way.
<?php
namespace Socket\Client;
use Socket\Client\Exceptions\ClientCantConnectException;
use Swoole\Coroutine\Socket;
class Client
{
/**
* Undocumented variable
*
* @var Socket
*/
protected $client;
public function __construct()
{
$this->client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
}
public function getClient()
{
return $this->client;
}
public function connect($host, $port)
{
go(function() use($host, $port){
if (!$this->getClient()->connect($host,$port))
{
throw new ClientCantConnectException();
}
});
}
public function disconnect()
{
go(function(){
$this->getClient()->close();
});
}
public function checkConnection()
{
go(function(){
$this->getClient()->isConnected();
});
}
public function getResponse()
{
go(function(){
var_dump($this->getClient()->recv());
});
}
public function sendMessage($messageContent)
{
go(function() use ($messageContent){
$this->getClient()->send($messageContent);
});
}
}
- What did you expect to see?
results on calls
- What did you see instead? coroutine overwrites.