WorkerMan实现即时通讯(准备工作)

获取项目基础信息

项目地址:GitHub - walkor/workerman-chat: Websocket chat room written in PHP based on workerman.

目录结构展示

目录结构
项目目录结构

目录结构说明:

  • db.php 数据库配置文件
  • Chat.php Redis数据缓存
  • Events.php 接收和发送消息

数据库配置文件db.php

<?php
define('HOST', '127.0.0.1');
define('PORT', '3306');
define('USERNAME', 'root');
define('PASSWORD', 'root');
define('DBNAME', 'root');
define('REDIS_KEY', 'uidConnectionMap');
define('CHAT_KEY', 'uidChatMap'); //在线用户数

建表语句chat.sql

CREATE TABLE `os_chat` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `room_id` char(4) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0' COMMENT '房间号',
  `from_client_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0' COMMENT '发送用户客户端ID',
  `to_client_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0' COMMENT '接收用户客户端ID',
  `content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '发送内容',
  PRIMARY KEY (`id`),
  KEY `os_chat_from_client_id_index` (`from_client_id`) USING BTREE,
  KEY `os_chat_to_client_id_index` (`to_client_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='聊天记录';

只需要修改 start_gateway.php 配置 https

<?php
// start.php

/**
 * run with command
 * php start.php start
 */

use Workerman\Worker;

// composer 的 autoload 文件
require_once  '../../vendor/autoload.php';

if (strpos(strtolower(PHP_OS), 'win') === 0) {
    exit("start.php not support windows, please use start_for_win.bat\n");
}

// 标记是全局启动  linux 启动时需要先启动laravel脚本 (App\Console\Commands\SyncOauthToUsers)  php artisan longer:sync_oauth 同步好友画像存在redis
define('GLOBAL_START', 1);
// 加载IO
foreach (glob(__DIR__.'/start*.php') as $start_file) {
    require_once "{$start_file}";
}
// 运行所有服务
Worker::runAll();
<?php
// start_businessworker.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 \GatewayWorker\BusinessWorker;

require_once   '../../vendor/autoload.php';

// bussinessWorker 进程
$worker = new BusinessWorker();
// worker名称
$worker->name = 'ChatBusinessWorker';
// bussinessWorker进程数量
$worker->count = 2;
// 服务注册地址
$worker->registerAddress = '127.0.0.1:1236';

// 如果不是在根目录启动,则运行runAll方法
if (!defined('GLOBAL_START')) {
    Worker::runAll();
}
<?php
// start_gateway.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 \GatewayWorker\Gateway;

require_once   '../../vendor/autoload.php';

// gateway 进程
if (in_array(PHP_OS, ['WINNT','Darwin'])) {
    $gateway = new Gateway("websocket://0.0.0.0:7272");
} else {
    $context = array(
        'ssl' => array(
            'local_cert'  => '/user/server/fullchain.pem',//你证书的pem文件
            'local_pk'    => '/user/server/privkey.pem',//你证书的key文件
            'verify_peer' => false,
        )
    );
    $gateway = new Gateway("websocket://0.0.0.0:7272", $context);
    // 开启SSL,websocket+SSL 即wss
    $gateway->transport = 'ssl';
}
// 设置名称,方便status时查看
$gateway->name = 'ChatGateway';
// 设置进程数,gateway进程数建议与cpu核数相同
$gateway->count = 2;
// 分布式部署时请设置成内网ip(非127.0.0.1)
$gateway->lanIp = '127.0.0.1';
// 内部通讯起始端口。假如$gateway->count=4,起始端口为2300
// 则一般会使用2300 2301 2302 2303 4个端口作为内部通讯端口
$gateway->startPort = 2300;
// 心跳间隔
$gateway->pingInterval = 1;
//如果pingNotResponseLimit = 1,则代表客户端必须定时发送心跳给服务端,否则pingNotResponseLimit*pingInterval=30秒内没有任何数据发来则关闭对应连接,并触发onClose。
//$gateway->pingNotResponseLimit = 1;
// 心跳数据
$gateway->pingData = '{"type":"ping"}';
// 服务注册地址
$gateway->registerAddress = '127.0.0.1:1236';

// 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
/*$gateway->onConnect = function($connection) {
    $connection->onWebSocketConnect = function($connection , $http_header) {
        global $host;
        // 可以在这里判断连接来源是否合法,不合法就关掉连接
        // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
        if($_SERVER['SERVER_NAME'] != $host) {
            $connection->close();
        }
        // onWebSocketConnect 里面$_GET $_SERVER是可用的
         var_dump($_GET, $_SERVER);
    };
};*/

// 如果不是在根目录启动,则运行runAll方法
if (!defined('GLOBAL_START')) {
    Worker::runAll();
}

<?php
// start_register.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 \GatewayWorker\Register;

require_once   '../../vendor/autoload.php';

// register 服务必须是text协议
$register = new Register('text://0.0.0.0:1236');

// 如果不是在根目录启动,则运行runAll方法
if (!defined('GLOBAL_START')) {
    Worker::runAll();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值