Reputation:
I want to run a specific function which is having the code of listening to the connection of WebSockets (server).
This function should be run at once when the application is started and never stop. when the application is closed this function should be stopped.
I have a class as Server.php in that i have a function called reciveData();
Once the application is started reciveData() will accept the connection and send the specific messages to them.
```
class Server extends Controller
{
public $send_param;
public $host;
public $port;
public function __construct()
{
$this->host = env('HOSTADDRESS');
$this->port = env('PORT');
}
public function recieveData()
{
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
$result = socket_bind($socket, $this->host, $this->port) or die("Could
not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket
listener\n");
$accept = socket_accept($socket) or die ('not accept the socket
connection');
do
{
$msg = socket_read($accept, 1024) or die('unable to read the
input\n');
$msg = rtrim($msg);
echo "Clinet says:>>".$msg;
echo '<br><br>';
//$line = new Chat();
//echo 'Enter Reply:>> go ahead'.$reply;
//$reply = $line->readLine();
//$reply = $this->readLine();
$reply = 'reply from server go ahead';
socket_write($accept, $reply, strlen($reply)) or die('unable to write
the output\n');
}while(true);
//socket_close($accept, $socket);
}```
The above is my code, the function in that class should be run as long as the application starts and stop o application closes.
Upvotes: 0
Views: 2112
Reputation: 55
Try this
$this->app->bind(YourController::class, function ($app) {
return YourController::yourfunction();
});
Paste this inside your AppServiceProvider.php boot method. Use your Controller and function names accordingly.
Upvotes: 1