我客戶(hù)端開(kāi)始能鏈接上ws服務(wù),但過(guò)幾十秒樣子,又?jǐn)嚅_(kāi)了顯示鏈接失敗。應(yīng)該是心跳沒(méi)接成功。現(xiàn)在問(wèn)題是我服務(wù)端已經(jīng)加上了 心跳,怎么沒(méi)起作用吶?
<?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.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;
// 自動(dòng)加載類(lèi)
require_once __DIR__ . '/../../vendor/autoload.php';
// gateway 進(jìn)程,這里使用Text協(xié)議,可以用telnet測(cè)試
$gateway = new Gateway("websocket://0.0.0.0:8285");
// gateway名稱(chēng),status方便查看
$gateway->name = 'AppGateway';
// gateway進(jìn)程數(shù)
$gateway->count = 4;
// 本機(jī)ip,分布式部署時(shí)使用內(nèi)網(wǎng)ip
$gateway->lanIp = '127.0.0.1';
// 內(nèi)部通訊起始端口,假如$gateway->count=4,起始端口為4000
// 則一般會(huì)使用4000 4001 4002 4003 4個(gè)端口作為內(nèi)部通訊端口
$gateway->startPort = 4900;
// 服務(wù)注冊(cè)地址
$gateway->registerAddress = '127.0.0.1:1238';
$gateway->pingInterval = 55;
$gateway->pingNotResponseLimit = 1;
// 服務(wù)端定時(shí)向客戶(hù)端發(fā)送的數(shù)據(jù)
// $gateway->pingData = '{"type":"ping"}';
$gateway->pingData = '{"type":"hello"}';
// 當(dāng)客戶(hù)端連接上來(lái)時(shí),設(shè)置連接的onWebSocketConnect,即在websocket握手時(shí)的回調(diào)
$gateway->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
// 可以在這里判斷連接來(lái)源是否合法,不合法就關(guān)掉連接
// $_SERVER['HTTP_ORIGIN']標(biāo)識(shí)來(lái)自哪個(gè)站點(diǎn)的頁(yè)面發(fā)起的websocket鏈接
if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
{
$connection->close();
}
// onWebSocketConnect 里面$_GET $_SERVER是可用的
var_dump($_GET, $_SERVER);
};
};
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
linux
執(zhí)行過(guò) 這個(gè)命令重啟了
