大概需求是这样,有几百家店铺,需要将workerman作为客户端同时与这几百家店铺的外部websocket地址建立连接,监听获取实时新的订单,请问有没有大佬能够提供一下思路,感激不尽
代码类似
use <?php use Workerman\Worker; use Workerman\Connection\AsyncTcpConnection; require_once __DIR__ . '/Workerman/Autoloader.php'; $worker = new Worker(); $worker->onWorkerStart = function($worker){ $con = new AsyncTcpConnection('ws://echo.websocket.org:80'); // 给接口发送数据 $con->onConnect = function($con) { $con->send('hello'); }; // 收到订单更新信息 $con->onMessage = function($con, $data) { echo $data; }; $con->connect(); }; Worker::runAll();
嗯嗯,感谢,就是如果要对这几百家店铺同时都保持外部的websocket连接要怎么做呢?
几百家是不同的url地址?
// 几百个地址列出来 $all_url = ['ws://a.com','ws://b.com', 'ws://c.com']; foreach ($all_url as $url) { $con = new AsyncTcpConnection($url); // 给接口发送数据 $con->onConnect = function($con) { $con->send('hello'); }; // 收到订单更新信息 $con->onMessage = function($con, $data) { echo $data; }; $con->connect(); }
@1459:感谢感谢
对的
代码类似
嗯嗯,感谢,就是如果要对这几百家店铺同时都保持外部的websocket连接要怎么做呢?
几百家是不同的url地址?
// 几百个地址列出来
$all_url = ['ws://a.com','ws://b.com', 'ws://c.com'];
foreach ($all_url as $url) {
$con = new AsyncTcpConnection($url);
// 给接口发送数据
$con->onConnect = function($con) {
$con->send('hello');
};
// 收到订单更新信息
$con->onMessage = function($con, $data) {
echo $data;
};
$con->connect();
}
@1459:感谢感谢
对的