RT,我用Gatewayworker 實現(xiàn)了幾個接口,都是服務端。
我有一個接口是作為socket客戶端,我試了用workerman,用AsyncTcpConnection,可以連接對方服務器,但我想把AsyncTcpConnection集成到原來的幾個接口一起,代碼怎么寫,各位前輩給指導一下,集成的方法,最好是詳細一點的,我之前沒寫過代碼,接觸PHP和workerman才兩個星期不到。謝謝各位。
1、既然是剛接觸,建議老老實實看手冊,使用workerman進行業(yè)務開發(fā)需要一定的功底,否則很容易自己給自己埋很多不是坑的坑;
2、至于使用 workerman 或者 gatewayworker 作為客戶端,手冊上就說的很詳細了,DEMO代碼也有,怎么整合到自己的上下文也不是什么難事,難的還是你沒認真看手冊:
http://doc.workerman.net/faq/as-wss-client.html
每天都在看手冊,沒寫過代碼,所以不是很懂,但是我想把AsyncTcpConnection 發(fā)出的連接也當作一個client用,這樣發(fā)消在Events中調用,通迅就比較方便。
我定議了一個協(xié)議 Xml
用的AsyncTcpConnection
新建一個Gateway,運行就報錯
這個是gateway
$internal_gateway = new Gateway("Xml://192.168.3.8:7273");
$internal_gateway->name='sis_client';
// #### 不要與原來start_gateway.php的一樣####
// #### 比原來跨度大一些,比如在原有startPort基礎上+1000 ####
$internal_gateway->startPort = 4300;
// #### 這里設置成與原start_gateway.php 一樣 ####
$internal_gateway->registerAddress = '127.0.0.1:1238';
這個是定義的協(xié)議,
public static function input($recv_buffer,AsyncTcpConnection $connection )
{
if(strlen($recv_buffer) < 1)
{
// 不夠10字節(jié),返回0繼續(xù)等待數據
return 0;
}
// 返回包長,包長包含 頭部數據長度+包體長度
$total_len = base_convert(substr($recv_buffer, 0, 10), 10, 10);
return $total_len;
}
public static function decode($recv_buffer)
{
// 請求包體
$body = $recv_buffer;
return simplexml_load_string($body);
}
public static function encode($xml_string)
{
// 包體+包頭的長度
//$total_length = strlen($xml_string)+10;
// 長度部分湊足10字節(jié),位數不夠補0
//$total_length_str = str_pad($total_length, 10, '0', STR_PAD_LEFT);
// 返回數據
//return $total_length_str . $xml_string;
return $xml_string;
}
}
stream_socket_server(): unable to connect to tcp://192.168.3.8:7273 (Cannot assign requested address) in file /var/www/html/GatewayWorker/vendor/workerman/workerman/Worker.php on line 2206
Fatal error: Uncaught exception 'Exception' with message 'Cannot assign requested address' in /var/www/html/GatewayWorker/vendor/workerman/workerman/Worker.php on line 2208
Exception: Cannot assign requested address in /var/www/html/GatewayWorker/vendor/workerman/workerman/Worker.php on line 2208
Call Stack:
0.0002 231320 1. {main}() /var/www/html/GatewayWorker/start.php:0
0.0114 1362368 2. Workerman\Worker::runAll() /var/www/html/GatewayWorker/start.php:37
0.0137 1398216 3. Workerman\Worker::initWorkers() /var/www/html/GatewayWorker/vendor/workerman/workerman/Worker.php:504
0.0143 1404712 4. Workerman\Worker->listen() /var/www/html/GatewayWorker/vendor/workerman/workerman/Worker.php:646
1、沒有讓你監(jiān)聽端口,是你的代碼報錯顯示程序有在監(jiān)聽端口的行為,為什么監(jiān)聽了,你得查自己的代碼;
2、AsyncTcpConnection原本就是一個基于workerman的客戶端類庫;
應該是建立Gateway 不支持這樣連接遠程,但我不明白怎么樣才能連接遠程地址,讓Gateway 作為一個客戶端。
Fatal error: Uncaught exception 'Exception' with message 'class \Protocols\Async TcpConnection not exist' in /var/www/html/GatewayWorker/vendor/workerman/workerm an/Worker.php on line 2272
Exception: class \Protocols\AsyncTcpConnection not exist in /var/www/html/Gatewa yWorker/vendor/workerman/workerman/Worker.php on line 2272
Call Stack:
0.0002 231320 1. {main}() /var/www/html/GatewayWorker/start.php:0
0.0201 1346776 2. require_once('/var/www/html/GatewayWorker/Application s/YourApp/start_xml_gateway.php') /var/www/html/GatewayWorker/start.php:34
0.0201 1347464 3. GatewayWorker\Gateway->__construct() /var/www/html/Ga tewayWorker/Applications/YourApp/start_xml_gateway.php:10
0.0201 1347600 4. Workerman\Worker->__construct() /var/www/html/Gateway Worker/vendor/workerman/gateway-worker/src/Gateway.php:239
0.0201 1358672 5. Workerman\Worker->parseSocketAddress() /var/www/html/ GatewayWorker/vendor/workerman/workerman/Worker.php:2169