workerman作为客户端连接外部websocket服务的问题

linf

大概需求是这样,有几百家店铺,需要将workerman作为客户端同时与这几百家店铺的外部websocket地址建立连接,监听获取实时新的订单,请问有没有大佬能够提供一下思路,感激不尽

2807 3 0
3个回答

xiuwang

代码类似

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();
  • 暂无评论
linf

嗯嗯,感谢,就是如果要对这几百家店铺同时都保持外部的websocket连接要怎么做呢?

  • xiuwang 2020-12-30

    几百家是不同的url地址?

  • xiuwang 2020-12-31

    // 几百个地址列出来
    $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();
    }

  • linf 2020-12-31

    @1459:感谢感谢

linf

对的

  • 暂无评论
年代过于久远,无法发表回答
×
🔝