WebsocketClient開(kāi)始可以正常連接,每三十秒發(fā)送數(shù)據(jù),可正常接收返回?cái)?shù)據(jù)。過(guò)幾小時(shí)到十幾小時(shí)不等,之后會(huì)onClose(不知道啥原因),我在里面運(yùn)行reConnect,有時(shí)可以重連成功,有時(shí)失敗。
從沒(méi)有觸發(fā)過(guò)onError。
報(bào)錯(cuò):Sec-WebSocket-Accpet not match.
而且一大串?dāng)?shù)據(jù)輸出之后還有error package. package_length=false
?
在網(wǎng)上搜了半天沒(méi)找到相關(guān)內(nèi)容,還請(qǐng)大佬指點(diǎn)
服務(wù)端用的GatewayWorker
下面是客戶端的啟動(dòng)文件
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
use \Workerman\Lib\Timer;
require_once __DIR__ . '/Workerman/Autoloader.php';
date_default_timezone_set("PRC");
$worker = new Worker();
$worker->onWorkerStart = function($worker){
$reConnentTimer = null;
$file = null;
$conn = new AsyncTcpConnection('ws://39.105.170.46:8282');
$conn->onConnect = function($conn) {
$conn->send('send onConnect');
Timer::add(30, function() use ($conn)
{
$conn->send(date("Y-m-d H:i:s"));
});
echo "run Timer::add( send message )\n";
global $file;
if (!isset($file)) {
$file = fopen('stdout.log','a');
}
fwrite($file, "run Timer::add( send message )"."\n");
};
$conn->onMessage = function($conn, $data) {
echo $data;
global $file;
if (!isset($file)) {
$file = fopen('stdout.log','a');
}
fwrite($file, $data);
global $reConnentTimer;
if ($reConnentTimer) {
Timer::del($reConnentTimer);
$reConnentTimer = null;
echo "run Timer::del( reConnect )\n";
global $file;
if (!isset($file)) {
$file = fopen('stdout.log','a');
}
fwrite($file, "run Timer::del( reConnect )"."\n");
}
};
$conn->onClose = function($conn) {
echo "run onClose()";
global $reConnentTimer;
global $file;
if ($reConnentTimer == null) {
$reConnentTimer = Timer::add(3, function() use ($conn,$file)
{
$conn->reConnect(0);
echo "run reConnect()\n";
if (!isset($file)) {
$file = fopen('stdout.log','a');
}
fwrite($file, "run reConnect()"."\n");
});
echo "run Timer::add( reConnect )\n";
global $file;
if (!isset($file)) {
$file = fopen('stdout.log','a');
}
fwrite($file, "run Timer::add( reConnect )"."\n");
}
};
$conn->onError = function($conn, $code, $msg) {
echo "error $code $msg\n";
global $file;
if (!isset($file)) {
$file = fopen('stdout.log','a');
}
fwrite($file, "error $code $msg"."\n");
};
$conn->connect();
};
Worker::runAll();
心跳這么用的,應(yīng)該可以吧,每三十秒發(fā)送一次
$conn->onConnect = function($conn) {
Timer::add(30, function() use ($conn)
{
$conn->send(date("Y-m-d H:i:s"));
});
onClose回調(diào)中的reConnect,我之前就是你說(shuō)的這樣寫的,運(yùn)行時(shí)間長(zhǎng)了出問(wèn)題,終端的輸出會(huì)停在最后一個(gè)reConnect(),看不到其他任何信息,之后我才改成現(xiàn)在的樣子,看到了Sec-WebSocket-Accpet not match.看到了截圖里的信息
@walkor:?
我更新完workerman,不再報(bào)錯(cuò)了Sec-WebSocket-Accpet not match.但是重連連不上的問(wèn)題仍然沒(méi)解決,客戶端這邊是下面截圖的樣子,服務(wù)端那邊也沒(méi)有錯(cuò)誤信息
[attach]1716[/attach]?
多次測(cè)試發(fā)現(xiàn),發(fā)生 onClose 并且 reConnect 連不上的情況都出現(xiàn)在早晨六點(diǎn)五十多的時(shí)候
而且這個(gè)客戶端連不上的時(shí)候,我再開(kāi)個(gè)新的客戶端去連服務(wù)器也連不上,把出問(wèn)題這個(gè)關(guān)掉之后,就可以再開(kāi)多個(gè)客戶端重新連接服務(wù)器了
?
還麻煩大佬給看看問(wèn)題在哪,或者指點(diǎn)我一下監(jiān)控進(jìn)程的思路,發(fā)現(xiàn)reConnect失敗之后如何關(guān)掉當(dāng)前進(jìn)程并重新開(kāi)一個(gè)新的