php start.php start无法启动, 报错Address already in use

superchangme
[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

求救,我调整了缓冲区大小
$gateway-$sendToClientBufferSize = 10241024100;

已经尝试过重启nginx,重启php-fpm都不行,在线求助。

8201 3 1
3个回答

phpcreeper

1、8282 端口被占用了,干掉占用这个端口的程序或者换一个端口试试;
2、workerman是基于PHP-CLI的,和PHP-FPM没有任何关系;

  • superchangme 2019-11-20

    [root@izm5e3gye2rcpk730gsan1z GatewayWorker]# lsof -i :8282
    [root@izm5e3gye2rcpk730gsan1z GatewayWorker]# netstat -tunlp|grep 8282
    [root@izm5e3gye2rcpk730gsan1z GatewayWorker]#
    大哥,我找不到端口被占用。

  • superchangme 2019-11-20

    有几个kworker的进程不知道是什么

  • phpcreeper 2019-11-20

    没上下文就把启动脚本代码完整贴下

  • superchangme 2019-11-20

    blog大哥 你看我最新截图那里我换了端口8283可以了但是8282还是在进程里残留着

  • superchangme 2019-11-20

    我是中途把127.0.0.1的ip直接改成了0.0.0.0 然后又restart了是不是这样会导致进程残留呢

superchangme
<?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://www.workerman.net/
 * @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协议,可以用telnet测试
$gateway = new Gateway("Websocket://0.0.0.0:8282");
// gateway名称,status方便查看
$gateway->name = 'YourAppGateway';
// gateway进程数
$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;
// 心跳数据
// $gateway->pingData = '{"type":"ping"}';

$gateway->pingNotResponseLimit = 1;

$gateway->pingData = '';
// 采用二进制传输数据
$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']标识来自哪个站点的页面发起的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();
}
  • 暂无评论
xiuwang

你启动了2个相同端口的gateway进程,所以端口冲突了。
你看下是不是有多个 start_gateway_xxx.php 文件。只保留一个,不然会一起启动。

  • superchangme 2019-11-20

    是只有一个start_gateway文件,但是我中间改了ip 从127.0.0.1改成了0.0.0.0 重启之后就不行了

  • superchangme 2019-11-20

    [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

  • superchangme 2019-11-20

    谢谢大哥确实有两个了。。。

年代过于久远,无法发表回答
×
🔝