TcpConnection如何做客户端主动连Workerman?

wuchuguang

Workerman的Worker做服务端,
现在想利用TcpConnection做客户端来连接Worker服务器。

require_once './Autoloader.php';

$conn = new \Workerman\Connection\AsyncTcpConnection("Websocket://localhost:8081");
$conn->connect();

php start.php

报:PHP Fatal error: Call to a member function add() on null in /opt/webpopled/WebsocketServer/Connection/AsyncTcpConnection.php on line 86

4954 1 0
1个回答

walkor 打赏

手册有
http://doc.workerman.net/appendices/about-ws.html

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/Workerman/Autoloader.php';
$worker = new Worker();
// 进程启动时
$worker->onWorkerStart = function()
{
       // 以websocket协议连接远程websocket服务器
       $ws_connection = new AsyncTcpConnection("ws://echo.websocket.org:80");
       // 连上后发送hello字符串
       $ws_connection->onConnect = function($connection){
           $connection->send('hello');
       };
       // 远程websocket服务器发来消息时
       $ws_connection->onMessage = function($connection, $data){
           echo "recv: $data\n";
       };
       // 连接上发生错误时,一般是连接远程websocket服务器失败错误
       $ws_connection->onError = function($connection, $code, $msg){
           echo "error: $msg\n";
       };
       // 当连接远程websocket服务器的连接断开时
       $ws_connection->onClose = function($connection){
           echo "connection closed\n";
       };
       // 设置好以上各种回调后,执行连接操作
       $ws_connection->connect();
};
Worker::runAll();
年代过于久远,无法发表回答
×
🔝