1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Use while server not responding

Discussion in 'Development' started by tigerza117, Mar 18, 2017.

  1. tigerza117

    tigerza117 Spider

    Messages:
    6
    GitHub:
    tigerza117
    i use while for socket_read


    PHP:
    public function onEnable(){
            
    Server::getInstance()->getScheduler()->scheduleRepeatingTask(new ParticleTask($this), 1);
            
    //Server::getInstance()->getScheduler()->scheduleRepeatingTask(new Sign($this), 100);
            
    Server::getInstance()->getScheduler()->scheduleRepeatingTask(new Update($this), 20);
            
    Server::getInstance()->getPluginManager()->registerEvents($this$this);
            
    $workerB = new WorkerThread($this->src,$this->dst);
            
    $workerB->run();
        }
    class while
    PHP:
    class WorkerThread  {
        public function 
    __construct($src$dst) {
            
    $this->src $src;
            
    $this->dst $dst;
        }
        
        function 
    run() {
            while(
    true) {
                while ((
    $bytes socket_read($this->src1024PHP_BINARY_READ)) != "") {
                    echo 
    "running... \n";
                    echo(
    $this->dst$bytes1024);
                    
    socket_write($this->dst$bytes1024);
                }
            }
        }
    }
    and not responding

    upload_2017-3-18_16-16-45.png

    Help guide socket on plugin pls
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    applqpak likes this.
  3. tigerza117

    tigerza117 Spider

    Messages:
    6
    GitHub:
    tigerza117
  4. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    You simply forgot to extend \Thread :p
     
  5. tigerza117

    tigerza117 Spider

    Messages:
    6
    GitHub:
    tigerza117
    Is there an example? I want so much :v
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    class WorkerThread extends \Thread {
    }
     
  7. tigerza117

    tigerza117 Spider

    Messages:
    6
    GitHub:
    tigerza117
    Not work :)
     
  8. tigerza117

    tigerza117 Spider

    Messages:
    6
    GitHub:
    tigerza117
    now work

    Thank you all for replying
    PHP:
    <?php

    namespace ac;


    use 
    pocketmine\Thread;
    use 
    pocketmine\utils\Binary;

    class 
    WorkerThread extends Thread{

        public function 
    __construct() {
            
    error_reporting(0);
            
    set_time_limit(0);
            
    $host "127.0.0.1";
            
    $port 1234;
            
    $this->socket socket_create(AF_INETSOCK_STREAM0) or die("Could not create socket\n");
            
    $result socket_bind($this->socket$host$port) or die("Could not bind to socket\n");
            
    $result socket_listen($this->socket) or die("Could not set up socket listener\n");
            echo 
    "Waiting for connections... \n";
            
    $this->start();
        }
        
        public function 
    run()
        {
            while (
    true)
            {
                if((
    $newc socket_accept($this->socket)) !== false)
                {
                    echo 
    "Client $newc has connected\n";
                    
    $this->clients[$newc] = $newc;
                } else {
                    echo 
    "w";
                }
                if(
    count($this->clients) > 0)
                {
                    foreach(
    $this->clients as $c)
                    {
                        if((
    $bytes socket_read($c1024PHP_BINARY_READ)) != "")
                        {
                            echo(
    $bytes."\n");
                            
    socket_close($c);
                            unset(
    $this->clients[$c]);
                        }
                    }
                }
            }
        }

        public function 
    getThreadName()
        {
        }
    }
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.