Skip to content

Commit

Permalink
增加win兼容,需要pthreads扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
lbbniu committed Oct 17, 2016
1 parent 2482072 commit 219fba8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
依赖扩展
---
-> curl
-> pcntl
-> pcntl 或者 pthreads
-> gd
78 changes: 66 additions & 12 deletions WebWeiXin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env php
<?php
require_once 'QRcode.class.php';

function raw_input($str){
echo("监听输入:");
fwrite(STDOUT,$str);
return trim(fgets(STDIN));
}
Expand Down Expand Up @@ -970,20 +970,32 @@ public function start(){
else
$this->_echo('[*] 自动回复模式 ... 关闭');
}

$pf = pcntl_fork();
if ($pf){ //父进程负责监听消息
if(extension_loaded("pcntl")){
$pf = pcntl_fork();
if ($pf){ //父进程负责监听消息
$this->listenMsgMode();
exit();
}
}elseif(extension_loaded("pthreads")){
return true;
}else{
$this->_echo('[*] 缺少扩展,暂时只能获取监听消息,不能发送消息');
$this->_echo('[*] 如果要发消息,请安装pcntl或者pthreads扩展');
$this->listenMsgMode();
exit();
}

sleep(2);
$this->readRun();
return false;
}

public function readRun(){
$this->help();
while(true){
$text = raw_input('');
if($text == 'quit'){
//listenProcess.terminate()
$this->_echo('[*] 退出微信');
$this->_echo('[*] 退出微信');
exit();
}elseif($text == 'help'){
$this->help();
Expand All @@ -1009,8 +1021,9 @@ public function start(){
list($name, $file_name) = explode(':',substr($text,3));
$this->sendEmotion($name, $file_name);
}
}
}
}
}

public function help(){
$help = '
==============================================================
Expand Down Expand Up @@ -1362,6 +1375,43 @@ public function juhecurl($url,$params=false,$ispost=0){
return $response;
}
}
if(!extension_loaded('pthreads')){
class Thread {
public function start(){

}
}
}
class ListenMsg extends Thread {
private $weixin;
public function __construct(WebWeiXin $weixin){
# code...
$this->weixin = $weixin;
}
public function run(){
if($this->weixin){
$this->weixin->_echo("[*] 进入消息监听模式 ......ListenMsg...run");
$this->weixin->listenMsgMode();
}
}
}
class ListenWrite extends Thread {
public function __construct(WebWeiXin $weixin){
$this->weixin = $weixin;
}
public function run(){
if($this->weixin){
if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'r'));
if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'w'));
if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'w'));
$this->weixin->_echo("[*] 进入命令行等待输入模式 ......ListenWrite...run");
$this->weixin->readRun();
}
}
}




$weixin = new WebWeiXin();
//var_dump($weixin);
Expand All @@ -1370,9 +1420,13 @@ public function juhecurl($url,$params=false,$ispost=0){
'autoReplyMode'=>true,
//'DEBUG'=>true
]);
$weixin->start();

// var_dump($weixin->_xiaodoubi("你好"));
if($weixin->start()){
$msg = new ListenMsg($weixin);
$write = new ListenWrite($weixin);
$msg->start();
sleep(2);
$write->start();
}



Expand Down

0 comments on commit 219fba8

Please sign in to comment.