[root@izm5e3gye2rcpk730gsan1z GatewayWorker]# php start.php restart -d
Workerman[start.php] restart
Workerman[start.php] is stopping ...
Workerman[start.php] stop success
[root@izm5e3gye2rcpk730gsan1z GatewayWorker]# stream_socket_server(): unable to connect to tcp://0.0.0.0:8282 (Address already in use) in file /usr/local/www/GatewayWorker/vendor/workerman/workerman/Worker.php on line 2178
Fatal error: Uncaught exception 'Exception' with message 'Address already in use' in /usr/local/www/GatewayWorker/vendor/workerman/workerman/Worker.php:2180
Stack trace:
#0 /usr/local/www/GatewayWorker/vendor/workerman/workerman/Worker.php(619): Workerman\Worker->listen()
#1 /usr/local/www/GatewayWorker/vendor/workerman/workerman/Worker.php(503): Workerman\Worker::initWorkers()
#2 /usr/local/www/GatewayWorker/start.php(37): Workerman\Worker::runAll()
#3 {main}
thrown in /usr/local/www/GatewayWorker/vendor/workerman/workerman/Worker.php on line 2180
求救,我調整了緩沖區(qū)大小
$gateway-$sendToClientBufferSize = 10241024100;
已經嘗試過重啟nginx,重啟php-fpm都不行,在線求助。
1、8282 端口被占用了,干掉占用這個端口的程序或者換一個端口試試;
2、workerman是基于PHP-CLI的,和PHP-FPM沒有任何關系;
[root@izm5e3gye2rcpk730gsan1z GatewayWorker]# lsof -i :8282
[root@izm5e3gye2rcpk730gsan1z GatewayWorker]# netstat -tunlp|grep 8282
[root@izm5e3gye2rcpk730gsan1z GatewayWorker]#
大哥,我找不到端口被占用。
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
use \Workerman\Protocols\Websocket;
use \GatewayWorker\Lib\Gateway\Events;
use \GlobalData\Client;
// 自動加載類
require_once __DIR__ . '/../../vendor/autoload.php';
$GlobalClient = new Client('0.0.0.0:2207');
// gateway 進程,這里使用Text協(xié)議,可以用telnet測試
$gateway = new Gateway("Websocket://0.0.0.0:8282");
// gateway名稱,status方便查看
$gateway->name = 'YourAppGateway';
// gateway進程數(shù)
$gateway->count = 4;
// 本機ip,分布式部署時使用內網ip
$gateway->lanIp = '127.0.0.1';
// 內部通訊起始端口,假如$gateway->count=4,起始端口為4000
// 則一般會使用4000 4001 4002 4003 4個端口作為內部通訊端口
$gateway->startPort = 2900;
// 服務注冊地址
$gateway->registerAddress = '127.0.0.1:1238';
// 心跳間隔
$gateway->pingInterval = 55;
// 心跳數(shù)據(jù)
// $gateway->pingData = '{"type":"ping"}';
$gateway->pingNotResponseLimit = 1;
$gateway->pingData = '';
// 采用二進制傳輸數(shù)據(jù)
$gateway->onConnect = function($connection)
{
$connection->websocketType = Websocket::BINARY_TYPE_ARRAYBUFFER;
};
$gateway->onBufferFull = function($connection)
{
// string(111) "a:3:{s:9:"client_id";s:20:"7f0000010b5400000004";s:7:"room_id";s:7:"ROOMONE";s:11:"client_name";s:7:"xiaohei";}"
global $GlobalClient;
$session = unserialize($connection->session);
$client_id = $session['client_id'];
echo 'client_id---'.$client_id .' is buffer full';
$GlobalClient->add($client_id .'_cache',1);
$bufferCache = $GlobalClient->__get($client_id.'_cache_data');
if(empty($bufferCache)){
$GlobalClient->add($client_id .'_cache_data',array());
}
};
$gateway->onBufferDrain = function($connection)
{
// echo "buffer drain and continue send\n".$_SESSION['isBufferFull'];
// if(isset($_SESSION['bufferCache'])){
// print_r($_SESSION['bufferCache']);
// echo 'handler buffercache----cache len--'.count($_SESSION['bufferCache']);
// foreach ($cache as $_SESSION['bufferCache']) {
// //Events::onMessage($cache['client_id'],$cache['message']);
// $connection->send($cache['data']);
// // 等同于Event.php onMessage的 Gateway::sendToClient($json_info['to_client_id'], $message);
// }
// unset($_SESSION['isBufferFull']);
// unset($_SESSION['bufferCache']);
// }
global $GlobalClient;
$session = unserialize($connection->session);
$client_id = $session['client_id'];
// echo 'client_id---'.$client_id .' is buffer full'.$GlobalClient->{$client_id.'_cache'};
$GlobalClient->__unset($client_id.'_cache');
echo 'in buffer drain can send again---\n';
$bufferCache = $GlobalClient->__get($client_id.'_cache_data');
echo 'buffer count---'.count($bufferCache);
if(count($bufferCache)==0){
$GlobalClient->__unset($client_id.'_cache_data');
return;
}
foreach ($bufferCache as $i=>$cache) {
//Events::onMessage($cache['client_id'],$cache['message']);
if(empty($GlobalClient->__get($client_id .'_cache'))){
echo 'send --- '.$i;
$connection->send($cache);
unset($bufferCache[$i]);
}
// 等同于Event.php onMessage的 Gateway::sendToClient($json_info['to_client_id'], $message);
}
$GlobalClient->__set($client_id.'_cache_data',$bufferCache);
//
};
$gateway->onError = function($connection, $code, $msg)
{
echo "error $code $msg\n";
};
/*
// 當客戶端連接上來時,設置連接的onWebSocketConnect,即在websocket握手時的回調
$gateway->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
// 可以在這里判斷連接來源是否合法,不合法就關掉連接
// $_SERVER['HTTP_ORIGIN']標識來自哪個站點的頁面發(fā)起的websocket鏈接
if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
{
$connection->close();
}
// onWebSocketConnect 里面$_GET $_SERVER是可用的
// var_dump($_GET, $_SERVER);
};
};
*/
// 如果不是在根目錄啟動,則運行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
你啟動了2個相同端口的gateway進程,所以端口沖突了。
你看下是不是有多個 start_gateway_xxx.php 文件。只保留一個,不然會一起啟動。
[root@izm5e3gye2rcpk730gsan1z GatewayWorker]# find / -name 'start_gateway*.php'
/usr/local/www/GatewayWorker/Applications/chat/start_gateway.php
/usr/local/www/GatewayWorker/Applications/YourApp/start_gateway.php