How to use named pipes in PHP between different functions or even different processes without fork?

后端 未结 3 1516
一向
一向 2021-01-13 14:34

I want to write an Ajax web application, a game to be specific. Two web clients have to communicate with each other via the PHP server. My approach to solve this is to use A

3条回答
  •  余生分开走
    2021-01-13 15:16

    Just a short statement, that I actually found a nice solution using named pipes:

    public function createPipe_test(){
        posix_mkfifo("testpipe", 0777);
    }
    
    public function writePipe_test($value){
        $pipeA = fopen("testpipe",'w');
        $valueString = number_format($value);
        $valueLen = number_format(strlen($valueString));
        fwrite($pipeA, $valueLen);
        fwrite($pipeA, $valueString);
    }
    
    public function readPipe_test(){
        $pipeB = fopen("testpipe",'r');
        $valueLen = fread($pipeB, 1);
        return fread($pipeB, $valueLen);
    }
    

    I have two processes.

    If process 1 calls writePipe_test(), then it waits until process 2 calls readPipe_test() to read the data out of the pipe.

    If process 1 calls readPipe_test(), then it waits until process 2 calls writePipe_test() to write something into the pipe.

    The trick is 'w' and 'r' instead of 'r+'.

提交回复
热议问题