Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kw_wechat_robot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
kw_wechat_robot
Commits
17192060
Commit
17192060
authored
Jul 17, 2018
by
夏彬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改vbot 支持重启退出以及请求重启或者退出
parent
96f71272
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
143 additions
and
5 deletions
+143
-5
restart.php
src/restart.php
+18
-0
stop.php
src/stop.php
+18
-0
MessageHandler.php
vendor/hanson/vbot/src/Core/MessageHandler.php
+2
-0
Server.php
vendor/hanson/vbot/src/Core/Server.php
+29
-0
Swoole.php
vendor/hanson/vbot/src/Core/Swoole.php
+76
-5
No files found.
src/restart.php
0 → 100644
View file @
17192060
<?php
/**
* Created by PhpStorm.
* User: xiabin
* Date: 2018/5/23
* Time: 3:14 PM
*/
require_once
__DIR__
.
'/../vendor/autoload.php'
;
require_once
__DIR__
.
'/conf/main.php'
;
$vbot
=
new
Hanson\Vbot\Foundation\Vbot
(
$options
);
$vbot
->
messageHandler
->
setHandler
(
function
(
$message
)
use
(
$vbot
)
{
$vbot
->
console
->
log
(
"收到消息"
);
});
$vbot
->
server
->
restart
();
src/stop.php
0 → 100644
View file @
17192060
<?php
/**
* Created by PhpStorm.
* User: xiabin
* Date: 2018/5/23
* Time: 3:14 PM
*/
require_once
__DIR__
.
'/../vendor/autoload.php'
;
require_once
__DIR__
.
'/conf/main.php'
;
$vbot
=
new
Hanson\Vbot\Foundation\Vbot
(
$options
);
$vbot
->
messageHandler
->
setHandler
(
function
(
$message
)
use
(
$vbot
)
{
$vbot
->
console
->
log
(
"收到消息"
);
});
$vbot
->
server
->
stop
();
vendor/hanson/vbot/src/Core/MessageHandler.php
View file @
17192060
...
...
@@ -45,6 +45,7 @@ class MessageHandler
if
(
!
$this
->
handleCheckSync
(
$checkSync
[
0
],
$checkSync
[
1
]))
{
if
(
$server
)
{
$server
->
shutdown
();
}
else
{
break
;
...
...
@@ -89,6 +90,7 @@ class MessageHandler
{
if
(
in_array
(
$retCode
,
[
1100
,
1101
,
1102
,
1205
]))
{
// 微信客户端上登出或者其他设备登录
$this
->
vbot
->
log
->
error
(
'微信客户端上登出或者其他设备登录或者异常退出'
);
$this
->
vbot
->
console
->
log
(
'vbot exit normally.'
);
$this
->
vbot
->
cache
->
forget
(
'session.'
.
$this
->
vbot
->
config
[
'session'
]);
...
...
vendor/hanson/vbot/src/Core/Server.php
View file @
17192060
...
...
@@ -44,6 +44,35 @@ class Server
}
}
public
function
restart
()
{
if
(
$this
->
vbot
->
config
[
'swoole.status'
])
{
$this
->
vbot
->
swoole
->
shutdown
();
if
(
!
$this
->
tryLogin
())
{
$this
->
cleanCookies
();
$this
->
login
();
}
$this
->
init
();
if
(
$this
->
vbot
->
config
[
'swoole.status'
])
{
$this
->
vbot
->
swoole
->
run
();
}
else
{
$this
->
vbot
->
messageHandler
->
listen
();
}
}
else
{
$this
->
vbot
->
console
->
log
(
'swoole 未开启暂不支持'
);
}
}
public
function
stop
(){
$this
->
vbot
->
swoole
->
shutdown
();
}
/**
* 尝试登录.
*
...
...
vendor/hanson/vbot/src/Core/Swoole.php
View file @
17192060
...
...
@@ -13,11 +13,28 @@ class Swoole
*/
private
$vbot
;
/**
* 主进程id
*/
const
MASTER_PROCESS_ID_KEY
=
'master_process_id_key'
;
public
function
__construct
(
Vbot
$vbot
)
{
$this
->
vbot
=
$vbot
;
}
public
function
shutdown
()
{
$pid
=
$this
->
vbot
->
cache
->
get
(
self
::
MASTER_PROCESS_ID_KEY
);
$this
->
vbot
->
console
->
log
(
"shutdown pid :
$pid
"
);
if
(
$pid
!==
NULL
)
{
if
(
\swoole_process
::
kill
(
$pid
,
0
))
{
\swoole_process
::
kill
(
$pid
);
}
}
}
public
function
run
()
{
$server
=
new
SwooleServer
(
$this
->
vbot
->
config
->
get
(
'swoole.ip'
,
'127.0.0.1'
),
$this
->
vbot
->
config
->
get
(
'swoole.port'
,
8866
));
...
...
@@ -25,15 +42,69 @@ class Swoole
$handleProcess
=
new
Process
(
function
(
$worker
)
use
(
&
$server
)
{
$this
->
vbot
->
messageHandler
->
listen
(
$server
);
});
$handleProcess
->
start
();
$processId
=
$handleProcess
->
start
();
$this
->
vbot
->
console
->
log
(
"vbot process id:
$processId
"
);
$server
->
on
(
'receive'
,
function
(
SwooleServer
$server
,
$fd
,
$from_id
,
$data
)
{
$response
=
$this
->
vbot
->
api
->
handle
(
$data
);
$this
->
vbot
->
console
->
log
(
" receive data:"
.
$data
);
$request
=
explode
(
"
\r\n\r\n
"
,
$data
);
if
(
!
$request
[
1
])
{
$response
=
$this
->
vbot
->
api
->
handle
(
$data
);
}
else
{
$data
=
json_decode
(
$request
[
1
],
TRUE
);
if
(
!
isset
(
$data
[
'action'
])
||
!
isset
(
$data
[
'params'
]))
{
$response
=
$this
->
vbot
->
api
->
handle
(
$data
);
}
else
{
if
(
$data
[
'action'
]
===
'system'
)
{
switch
(
$data
[
'params'
])
{
case
'shutdown'
:
$this
->
vbot
->
console
->
log
(
" system shutdown "
);
$response
=
'system shutdown'
;
$server
->
shutdown
();
break
;
case
'reload'
:
$this
->
vbot
->
console
->
log
(
" system reload "
);
$response
=
'system reload'
;
$server
->
reload
();
break
;
default
:
$response
=
"system params error not in shutdown or reload"
;
}
}
else
{
$response
=
$this
->
vbot
->
api
->
handle
(
$data
);
}
}
}
$response
=
$this
->
makeResponse
(
$response
);
$server
->
send
(
$fd
,
$response
);
});
$server
->
on
(
'shutdown'
,
function
(
SwooleServer
$server
)
use
(
$processId
)
{
$this
->
vbot
->
console
->
log
(
"server shutdown trigger"
);
$this
->
vbot
->
cache
->
delete
(
self
::
MASTER_PROCESS_ID_KEY
);
\swoole_process
::
kill
(
$processId
);
});
$server
->
on
(
'start'
,
function
(
SwooleServer
$server
)
{
$this
->
vbot
->
console
->
log
(
"server start trigger"
);
$mid
=
$server
->
master_pid
;
$this
->
vbot
->
console
->
log
(
"master process id
$mid
"
);
$this
->
vbot
->
cache
->
set
(
self
::
MASTER_PROCESS_ID_KEY
,
$mid
,
1440
*
30
);
});
$server
->
start
();
exit
;
}
...
...
@@ -43,15 +114,15 @@ class Swoole
$data
=
json_encode
(
$data
);
$headers
=
[
'Server'
=>
'Swoole'
,
'Content-Type'
=>
'application/json'
,
'Server'
=>
'Swoole'
,
'Content-Type'
=>
'application/json'
,
'Content-Length'
=>
strlen
(
$data
),
];
$response
[]
=
'HTTP/1.1 200'
;
foreach
(
$headers
as
$key
=>
$val
)
{
$response
[]
=
$key
.
':'
.
$val
;
$response
[]
=
$key
.
':'
.
$val
;
}
$response
[]
=
''
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment