0

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.

15
  • every request into your site is booting (starting) the application and when the response is returned the entire thing ends, the process is over ... are you wanting to have a command that is long running or something? Commented Oct 12, 2020 at 5:48
  • i think singleton-pattern will give you the solution..... also are you try something ? ... . Commented Oct 12, 2020 at 5:52
  • Yes, @noor singleton-pattern. On application, launch invoke a function for listening to the connection of WebSockets. I haven't tried anything yet. Commented Oct 12, 2020 at 5:58
  • @lagbox, i don't want any command. wanted from the application itself. Commented Oct 12, 2020 at 5:59
  • 1
    how are you "launching" the application? Commented Oct 12, 2020 at 6:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.