我們公司項(xiàng)目一直使用的 Laravel,部分項(xiàng)目也可能會用到 laravels(swoole),所以我對于 workerman 的關(guān)注少之又少。我平時(shí)本來就比較習(xí)慣逛 LearnKu,也是在今年上半年看到了一篇關(guān)于介紹 webman 的帖子,這才知道 walkor 老大居然出了一個(gè)基于 workerman 的web框架,一下子就來了興趣,各種翻論壇看相關(guān)帖子,哈哈(就喜歡研究新玩意兒??)。但是由于工作方面原因,項(xiàng)目周期也比較緊,也就不了了之。直到上周來著,一時(shí)興起,尋思寫個(gè)小聊天室玩玩,說干就干...
一開始準(zhǔn)備花一晚上,隨便擼一個(gè)零樣式的。后來...原本做后端的,對前端布局、CSS都不在行,折騰不少時(shí)間才基于 Bootstrap5 拼出來了下面的兩個(gè)頁面(見笑了??)。
準(zhǔn)備用 Mysql數(shù)據(jù)庫 記錄用戶信息、以及聊天記錄,然后聊天記錄通過Redis存儲再掃到數(shù)據(jù)庫。但是我沒想到更好的更新讀取方案,暫時(shí)存的Redis沒做同步方案。對于這塊問題我產(chǎn)生的以下不完善想法:
AplineJS
for 遍歷渲染出來的,如果一小時(shí)聊天記錄幾千上萬條瀏覽器肯定會卡,想聽聽大家的優(yōu)化方案。我在參考老大的代碼,使用workerman 起一個(gè)進(jìn)程對聊天服務(wù)做壓測時(shí)出現(xiàn)報(bào)錯(cuò),下面是代碼
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Connection\AsyncTcpConnection;
$worker = new Worker();
$worker->onWorkerStart = 'connect';
function connect(){
static $count = 0;
// 2000個(gè)鏈接
if ($count++ >= 500) return;
// 建立異步鏈接
$con = new AsyncTcpConnection('ws://127.0.0.1:8282');
$con->onConnect = function($con) {
$con->send('hello world');
// 遞歸調(diào)用connect
connect();
};
$con->onMessage = function($con, $msg) {
echo "recv $msg\n";
};
$con->onClose = function($con) {
echo "con close\n";
};
// 當(dāng)前鏈接每55秒發(fā)個(gè)心跳包
Timer::add(55, function()use($con){
$con->send("ping");
});
$con->connect();
echo $count, " connections complete\n";
}
Worker::runAll();
報(bào)錯(cuò)內(nèi)容
ErrorException: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /Users/aaa/www/Liao/vendor/workerman/gateway-worker/src/Protocols/GatewayProtocol.php:179
Stack trace:
#0 [internal function]: {closure}(8192, 'strlen(): Passi...', '/Users/aaa/ww...', 179)
#1 /Users/aaa/www/Liao/vendor/workerman/gateway-worker/src/Protocols/GatewayProtocol.php(179): strlen(NULL)
#2 /Users/aaa/www/Liao/vendor/workerman/gateway-worker/src/Lib/Gateway.php(714): GatewayWorker\Protocols\GatewayProtocol::encode(Array)
#3 /Users/aaa/www/Liao/vendor/workerman/gateway-worker/src/Lib/Gateway.php(394): GatewayWorker\Lib\Gateway::getBufferFromAllGateway(Array)
#4 /Users/aaa/www/Liao/plugin/webman/gateway/Events.php(26): GatewayWorker\Lib\Gateway::getClientIdByUid(NULL)
#5 [internal function]: plugin\webman\gateway\Events::onWebSocketConnect('7f0000010900000...', Array)
#6 /Users/aaa/www/Liao/vendor/workerman/gateway-worker/src/BusinessWorker.php(419): call_user_func('plugin\\webman\\g...', '7f0000010900000...', Array)
#7 /Users/aaa/www/Liao/vendor/workerman/workerman/Connection/TcpConnection.php(638): GatewayWorker\BusinessWorker->onGatewayMessage(Object(Workerman\Connection\AsyncTcpConnection), Array)
#8 /Users/aaa/www/Liao/vendor/workerman/workerman/Events/Select.php(295): Workerman\Connection\TcpConnection->baseRead(Resource id #235)
#9 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(2465): Workerman\Events\Select->loop()
#10 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(1574): Workerman\Worker->run()
#11 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(1416): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#12 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(1390): Workerman\Worker::forkWorkersForLinux()
#13 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(1722): Workerman\Worker::forkWorkers()
#14 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(1660): Workerman\Worker::monitorWorkersForLinux()
#15 /Users/aaa/www/Liao/vendor/workerman/workerman/Worker.php(559): Workerman\Worker::monitorWorkers()
#16 /Users/aaa/www/Liao/start.php(117): Workerman\Worker::runAll()
#17 {main}
SendBufferToWorker fail. The connections between Gateway and BusinessWorker are not ready. See http://doc2.workerman.net/send-buffer-to-worker-fail.html
沒找到報(bào)錯(cuò)原因在哪
這個(gè)報(bào)錯(cuò)提示解決方案:http://doc2.workerman.net/send-buffer-to-worker-fail.html
很棒,學(xué)習(xí)了!