作者:xuanchrist
项目:HC-2
<?php
require_once dirname(__DIR__) . '/redisData.php';
require_once dirname(dirname(dirname(dirname(__DIR__)))) . '/Workerman/Autoloader.php';
\Workerman\Autoloader::setRootPath(__DIR__ . '/../../');
use GatewayWorker\Lib\Db;
use Workerman\Protocols\Http;
HTTP::sessionStart();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title></title>
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- 字体样式css -->
<link rel="stylesheet" href="css/font-awesome.min.css">
<!-- 按键样式css -->
<link rel="stylesheet" href="css/buttons.css">
<!-- 开关样式css -->
<link rel="stylesheet" href="css/bootstrap-switch.css">
<link rel="stylesheet" href="css/intell_list.css">
<link rel="stylesheet" href="css/highlight.css">
<link rel="stylesheet" href="css/intell.css">
<link rel="stylesheet" href="css/messenger.css">
<link rel="stylesheet" href="css/messenger-theme-block.css">
</head>
<body style="margin: 0; padding: 0;">
作者:hongbo81
项目:webCha
* 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 GatewayWorker\Gateway;
use GatewayWorker\BusinessWorker;
use Workerman\Autoloader;
// 自动加载类
require_once __DIR__ . '/../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__);
// gateway 进程
$gateway = new Gateway("Websocket://0.0.0.0:7272");
// 设置名称,方便status时查看
$gateway->name = 'ChatGateway';
// 设置进程数,gateway进程数建议与cpu核数相同
$gateway->count = 4;
// 分布式部署时请设置成内网ip(非127.0.0.1)
$gateway->lanIp = '127.0.0.1';
// 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
// 则一般会使用4001 4002 4003 4004 4个端口作为内部通讯端口
$gateway->startPort = 3000;
// 心跳间隔
$gateway->pingInterval = 10;
// 心跳数据
$gateway->pingData = '{"type":"ping"}';
作者:AimuTra
项目:workerman-for-wi
/**
* 运行worker实例
*/
public function run()
{
// 设置自动加载根目录
Autoloader::setRootPath($this->_appInitPath);
// 则创建一个全局事件轮询
if (extension_loaded('libevent')) {
self::$globalEvent = new Libevent();
} else {
self::$globalEvent = new Select();
}
// 监听_mainSocket上的可读事件(客户端连接事件)
if ($this->_socketName) {
if ($this->transport !== 'udp') {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
} else {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
}
}
// 用全局事件轮询初始化定时器
Timer::init(self::$globalEvent);
// 如果有设置进程启动回调,则执行
if ($this->onWorkerStart) {
call_user_func($this->onWorkerStart, $this);
}
// 子进程主循环
self::$globalEvent->loop();
}
作者:TongJiankan
项目:Workerma
/**
* Run worker instance.
* @return void
*/
public function run()
{
//Update process state.
self::$_status = self::STATUS_RUNNING;
// Eegister shutdown function for checking errors.
register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
// Set autoload root path.
Autoloader::setRootPath($this->_autoloadRootPath);
// Create a global event loop.
if (!self::$globalEvent) {
$eventLoopClass = "\\Workerman\\Events\\" . ucfirst(self::getEventLoopName());
self::$globalEvent = new $eventLoopClass();
// Register a listener to be notified when server socket is ready to read.
if ($this->_socketName) {
if ($this->transport !== 'udp') {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
} else {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
}
}
}
// Reinstall signal.
self::reinstallSignal();
// Init Timer.
Timer::init(self::$globalEvent);
// Try to emit onWorkerStart callback.
if ($this->onWorkerStart) {
try {
call_user_func($this->onWorkerStart, $this);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
// Main loop.
self::$globalEvent->loop();
}
作者:Nathanyan
项目:web-msg-sende
/**
* 当Gateway启动的时候触发的回调函数
* @return void
*/
public function onWorkerStart()
{
// 分配一个内部通讯端口
$this->lanPort = function_exists('posix_getppid') ? $this->startPort - posix_getppid() + posix_getpid() : $this->startPort;
if ($this->lanPort < 0 || $this->lanPort >= 65535) {
$this->lanPort = rand($this->startPort, 65535);
}
// 如果有设置心跳,则定时执行
if ($this->pingInterval > 0) {
$timer_interval = $this->pingNotResponseLimit > 0 ? $this->pingInterval / 2 : $this->pingInterval;
Timer::add($timer_interval, array($this, 'ping'));
}
if (!class_exists('\\Protocols\\GatewayProtocol')) {
class_alias('\\GatewayWorker\\Protocols\\GatewayProtocol', 'Protocols\\GatewayProtocol');
}
// 初始化gateway内部的监听,用于监听worker的连接已经连接上发来的数据
$this->_innerTcpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}");
$this->_innerTcpWorker->listen();
$this->_innerUdpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}");
$this->_innerUdpWorker->transport = 'udp';
$this->_innerUdpWorker->listen();
// 重新设置自动加载根目录
Autoloader::setRootPath($this->_appInitPath);
// 设置内部监听的相关回调
$this->_innerTcpWorker->onMessage = array($this, 'onWorkerMessage');
$this->_innerUdpWorker->onMessage = array($this, 'onWorkerMessage');
$this->_innerTcpWorker->onConnect = array($this, 'onWorkerConnect');
$this->_innerTcpWorker->onClose = array($this, 'onWorkerClose');
// 注册gateway的内部通讯地址,worker去连这个地址,以便gateway与worker之间建立起TCP长连接
if (!$this->registerAddress()) {
$this->log('registerAddress fail and exit');
Worker::stopAll();
}
if ($this->_onWorkerStart) {
call_user_func($this->_onWorkerStart, $this);
}
}
作者:hongbo81
项目:webCha
/**
* 运行worker实例
*/
public function run()
{
// 注册进程退出回调,用来检查是否有错误
register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
// 设置自动加载根目录
Autoloader::setRootPath($this->_appInitPath);
// 如果没有全局事件轮询,则创建一个
if (!self::$globalEvent) {
if (extension_loaded('libevent')) {
self::$globalEvent = new Libevent();
} else {
self::$globalEvent = new Select();
}
// 监听_mainSocket上的可读事件(客户端连接事件)
if ($this->_socketName) {
if ($this->transport !== 'udp') {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
} else {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
}
}
}
// 重新安装事件处理函数,使用全局事件轮询监听信号事件
self::reinstallSignal();
// 用全局事件轮询初始化定时器
Timer::init(self::$globalEvent);
// 如果有设置进程启动回调,则执行
if ($this->onWorkerStart) {
call_user_func($this->onWorkerStart, $this);
}
// 子进程主循环
self::$globalEvent->loop();
}
作者:hongbo81
项目:webCha
<?php
//test文件,压入数据的时候可以参考
use Workerman\Autoloader;
require_once '../../../Workerman/Autoloader.php';
Autoloader::setRootPath("../../");
//消息队列,向队列中rpush
//require 'Redis.php';
//use \Vendors\RedisQuene\Redisq;
use Vendors\Redis\RedisModel;
//RedisModel::set('webChat', 'aaa', 'bbbbgggggg');
// RedisModel::hashSet('webChat', 'aaa', 'bbbbgggggg', 'dddd');
// RedisModel::hashSet('webChat', 'USER_ONLINE_LIST', 'bbbbggg', 'dddd');
// $res = RedisModel::hashGet('webChat', 'aaa');
// var_dump($res);die;
$res = RedisModel::hashDel('webChat', 'wangjx:unread:msg', 'bbb');
var_dump($res);
die;
if (null === $res) {
var_dump($res);
} else {
var_dump(2222222);
}
die;
$a = substr(str_shuffle("qwertyuiopasdfghjklzxcvbnm"), 0, 4);
$b = substr(str_shuffle("qwertyuiopasdfghjklzxcvbnm"), 0, 4);
$messageVal = array('fromuser' => 'cuihb', 'touser' => 'xieyx', 'message' => $a . $b, 'time' => time());
Redisq::rpush(array('serverName' => 'webChat', 'key' => 'chat:message:quene', 'value' => serialize($messageVal)));
$long = Redisq::getSize(array('serverName' => 'webChat', 'key' => 'chat:message:quene'));
var_dump($long);
$arr = Redisq::range(array('serverName' => 'webChat', 'key' => 'chat:message:quene', 'offset' => 0, 'len' => -1));
作者:dryyu
项目:Workerma
/**
* 运行worker实例
*/
public function run()
{
//更新 Worker 状态
self::$_status = self::STATUS_RUNNING;
// 注册进程退出回调,用来检查是否有错误
register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
// 设置自动加载根目录
Autoloader::setRootPath($this->_appInitPath);
// 如果没有全局事件轮询,则创建一个
if (!self::$globalEvent) {
$eventLoopClass = "\\Workerman\\Events\\" . ucfirst(self::getEventLoopName());
self::$globalEvent = new $eventLoopClass();
// 监听_mainSocket上的可读事件(客户端连接事件)
if ($this->_socketName) {
if ($this->transport !== 'udp') {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
} else {
self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
}
}
}
// 重新安装事件处理函数,使用全局事件轮询监听信号事件
self::reinstallSignal();
// 用全局事件轮询初始化定时器
Timer::init(self::$globalEvent);
// 如果有设置进程启动回调,则执行
if ($this->onWorkerStart) {
try {
call_user_func($this->onWorkerStart, $this);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
// 子进程主循环
self::$globalEvent->loop();
}
作者:hongbo81
项目:webCha
<?php
//自动加载类
use Workerman\Autoloader;
require '../../Workerman/Autoloader.php';
Autoloader::setRootPath(__DIR__ . "/..");
//class and method
$class = rtrim('Api\\Controler\\ ') . ucfirst($_REQUEST['c']);
$method = "do" . ucfirst($_REQUEST['a']);
WebchatApi::run($class, $method);
class WebchatApi
{
/**
* 执行API的方法
*/
public static function run($class, $method)
{
if ($method === "do" || !$class) {
return false;
}
if (!class_exists($class)) {
exit('class ' . $class . ' is not found!');
}
$obj = new $class();
if (!method_exists($obj, $method)) {
exit('class ' . $class . ' has not method ' . $method . ' ');
}
return $obj->{$method}();
}
}
作者:qyclou
项目:ant_util
/**
* 当Gateway启动的时候触发的回调函数
* @return void
*/
public function onWorkerStart()
{
// 分配一个内部通讯端口
$this->lanPort = $this->startPort + $this->id;
// 如果有设置心跳,则定时执行
if ($this->pingInterval > 0) {
$timer_interval = $this->pingNotResponseLimit > 0 ? $this->pingInterval / 2 : $this->pingInterval;
Timer::add($timer_interval, array($this, 'ping'));
}
// 如果BusinessWorker ip不是127.0.0.1,则需要加gateway到BusinessWorker的心跳
if ($this->lanIp !== '127.0.0.1') {
Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, array($this, 'pingBusinessWorker'));
}
// 如果Register服务器不在本地服务器,则需要保持心跳
if (strpos($this->registerAddress, '127.0.0.1') !== 0) {
Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, array($this, 'pingRegister'));
}
if (!class_exists('\\Protocols\\GatewayProtocol')) {
class_alias('\\Utils\\GatewayWorker\\Protocols\\GatewayProtocol', 'Protocols\\GatewayProtocol');
}
// 初始化gateway内部的监听,用于监听worker的连接已经连接上发来的数据
$this->_innerTcpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}");
$this->_innerTcpWorker->listen();
// 重新设置自动加载根目录
Autoloader::setRootPath($this->_autoloadRootPath);
// 设置内部监听的相关回调
$this->_innerTcpWorker->onMessage = array($this, 'onWorkerMessage');
$this->_innerTcpWorker->onConnect = array($this, 'onWorkerConnect');
$this->_innerTcpWorker->onClose = array($this, 'onWorkerClose');
// 注册gateway的内部通讯地址,worker去连这个地址,以便gateway与worker之间建立起TCP长连接
$this->registerAddress();
if ($this->_onWorkerStart) {
call_user_func($this->_onWorkerStart, $this);
}
}