Commit a33c4fc6 authored by shajiaiming's avatar shajiaiming

test

parent fa3d1e88
......@@ -57,6 +57,8 @@ class WorkermanWebSocketController extends Controller
{
$ip = isset($this->config['ip']) ? $this->config['ip'] : $this->ip;
$port = isset($this->config['port']) ? $this->config['port'] : $this->port;
define('HEARTBEAT_TIME', 5);
$wsWorker = new Worker("websocket://{$ip}:{$port}");
// 4 processes
......@@ -68,9 +70,34 @@ class WorkermanWebSocketController extends Controller
echo "New connection\n";
};
// $wsWorker->onConnect = function($connection) {
// // 给链接对象临时赋值一个lastTime属性记录上次接收消息的时间
// $connection->lastTime = time();
// };
// 进程启动后设置一个每秒运行一次的定时器
$wsWorker->onWorkerStart = function($worker) {
\Workerman\Lib\Timer::add(1, function()use($worker){
$time_now = time();
foreach($worker->connections as $connection) {
// 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间
if (empty($connection->lastMessageTime)) {
$connection->lastMessageTime = $time_now;
continue;
}
// 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接
if ($time_now - $connection->lastMessageTime > HEARTBEAT_TIME) {
$connection->close();
}
}
});
};
// Emitted when data received
$wsWorker->onMessage = function ($connection, $data) use ($wsWorker, $uids) {
if (!isset($connection->uid)) {
$connection->lastMessageTime = time();
$connection->uid = $data;
$wsWorker->uidConnections[$connection->uid] = $connection;
echo $connection->uid . ' 一共 ' . count($wsWorker->uidConnections[$connection->uid]) . PHP_EOL;
......@@ -100,7 +127,7 @@ class WorkermanWebSocketController extends Controller
}
}
}
$connection->send('binance : ' . json_encode($ticker), $connection->id);
$connection->send('binance : ' . json_encode($ticker));
};
$con->connect();
} elseif ('huobi' == $data) {
......@@ -145,7 +172,7 @@ class WorkermanWebSocketController extends Controller
}
}
}
$connection->send('huobi : ' . json_encode($ticker), $connection->id);
$connection->send('huobi : ' . json_encode($ticker));
} else {
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment