Commit d077e892 authored by 734642908@qq.com's avatar 734642908@qq.com

修改配置文件目录

parent 1c8b6dd6
......@@ -4,11 +4,11 @@ swoole框架websocket
##project 应用部署目录
* ├─application 应用目录(可设置)
* │ ├─conf 应用(公共)配置文件
* │ ├─socket 业务模块目录(可更改)
* │ │ ├─controller 控制器目录
* │ │ ├─model 模型目录
* │ ├─common.php 应用公共(函数)文件
* │ ├─config.php 应用(公共)配置文件
* ├─extend 扩展类库目录(可定义)
* ├─swoole swoole框架目录(可更改)
* ├─server.php 服务启动文件
......
......@@ -58,28 +58,108 @@ function post_curl($url,$data,$header = array(), $is_ssl = false) {
}
function aaa() {
return $GLOBALS['config']['img_path'];
function cache($config) {
$con = new Redis();
$con->connect($config['host'], $config['port']);
if(!empty($config['auth'])){
$this->auth($config['auth']);
}
return $con;
}
function db($config) {
//创建新连接
$con = new mysqli($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'],$config['DB_PORT']);
if($con){
$con->set_charset("utf8");
return $con;
//根据用户ID查用户的连接记录
function get_user_fd($uid){
if($GLOBALS['USER_LIST']->exist($uid)){
$tmp_val = $GLOBALS['USER_LIST']->get($uid);
return $tmp_val['fd'];
}else{
return false;
}
}
//根据用户的fd查用户的uid
function get_user_uid($fd){
if($GLOBALS['USER_LIST_FD']->exist($fd)){
$tmp_val = $GLOBALS['USER_LIST_FD']->get($fd);
return $tmp_val['uid'];
}else{
echo("connect database is error!!".$con->connect_error);
return false;
}
}
function cache($config) {
$con = new Redis();
$con->connect($config['host'], $config['port']);
if(!empty($config['auth'])){
$this->auth($config['auth']);
//设置用户在线
/*
$data = array(
"fd" => ,//"连接",
"uid" => ,//用户ID,
);
*/
function set_user_list($data){
//删掉这个uid之前绑定的连接
$tmp_uid = $GLOBALS['USER_LIST']->get($data['uid']);
if($tmp_uid && $tmp_uid['fd']>0){
$GLOBALS['USER_LIST_FD']->del($tmp_uid['fd']);
$GLOBALS['USER_LIST']->del($data['uid']);
}
return $con;
$GLOBALS['USER_LIST']->set($data['uid'],array('fd'=>$data['fd']));
$GLOBALS['USER_LIST_FD']->set($data['fd'],array('uid'=>$data['uid']));
return true;
}
//删除用户在线数据
function del_user_list($fd){
$GLOBALS['USER_LIST']->del(get_user_uid($fd));
$GLOBALS['USER_LIST_FD']->del($fd);
return true;
}
//给某个用户发信息
function send_user($server,$uid,$data,$opcode=1){
if(empty($uid)){
return false;
}
$fd = get_user_fd($uid);
//检测通道是否存在
if(!empty($fd) && !$server->exist($fd)){
echo_log("通道[ $fd ]的客户端已经断开,无法发送消息!\r\n",$data);
unset($data);
return false;
}else{
$server->push($fd,$data,$opcode,$finish);
unset($data);
return $finish;
}
}
//给指定用户发信息(群发)
function send_group($server,$arr,$data,$opcode=1){
if(!empty($arr)){
foreach($arr as $uid){
if(send_user($uid['uid'],$data,$opcode)){
echo_log('用户['. $uid .']发送消息成功!\r\n');
}else{
echo_log('用户['. $uid .']的客户端已经断开,无法发送消息!\r\n');
}
}
}else{
echo_log('没有指定要发信息的用户,无法发送消息!\r\n');
}
return true;
}
//给所有用户发信息
function send_all($server,$data,$opcode=1){
if(!empty($GLOBALS['USER_LIST'])){
foreach($GLOBALS['USER_LIST'] as $fd){
$server->push($fd['fd'],$data,$opcode,$finish);
if($finish){
echo_log('用户['. $fd['fd'] .']发送消息成功!\r\n');
}else{
echo_log('用户['. $fd['fd'] .']的客户端已经断开,无法发送消息!\r\n');
}
}
}else{
echo_log('没有指定要发信息的用户,无法发送消息!\r\n');
}
return true;
}
\ No newline at end of file
......@@ -17,6 +17,6 @@ $GLOBALS['config'] = array(
'DB_CHARSET' => 'utf8',
),
'socket_controller' => 'application/socket/controller/',
'socket_controller' => ROOT_PATH.'application/socket/controller/',
'img_path'=> '/9999/'
);
\ No newline at end of file
<?php
$GLOBALS['config'] = array(
'REDIS'=>array(
'type'=>'Redis',
'host'=>'127.0.0.1', // 119.23.249.188 (部署)
'port'=>'6379',
'auth'=>''
),
'MYSQL' => array(
'DB_TYPE' => 'mysql',
'DB_HOST' => '127.0.0.1',
'DB_NAME' => 'lewan_hezi',
'DB_USER' => 'root',
'DB_PWD' => '1QAZ2wsx',
'DB_PORT' => '3306',
'DB_PREFIX' => '',
'DB_CHARSET' => 'utf8',
),
'socket_controller' => ROOT_PATH.'application/socket/controller/',
'img_path'=> '/9999/'
);
\ No newline at end of file
<?php
$GLOBALS['config'] = array(
'REDIS'=>array(
'type'=>'Redis',
'host'=>'127.0.0.1', // 119.23.249.188 (部署)
'port'=>'6379',
'auth'=>''
),
'MYSQL' => array(
'DB_TYPE' => 'mysql',
'DB_HOST' => '127.0.0.1',
'DB_NAME' => 'lewan_hezi',
'DB_USER' => 'root',
'DB_PWD' => '1QAZ2wsx',
'DB_PORT' => '3306',
'DB_PREFIX' => '',
'DB_CHARSET' => 'utf8',
),
'socket_controller' => ROOT_PATH.'application/socket/controller/',
'img_path'=> '/9999/'
);
\ No newline at end of file
<?php
include_once("application/socket/model/UserModel.php");//uesrModel
include_once(ROOT_PATH."application/socket/model/UserModel.php");//uesrModel
class user extends base {
public function getData()
public function list()
{
$userModel = new \socket\model\UserModel();
$data = $userModel->test();
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('list'=>$data)));
$data = $userModel->getData();
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('info'=>$data)));
$this->server->push($this->fd, $reData);
}
public function info()
{
$userModel = new \socket\model\UserModel();
$data = $userModel->getOne();
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('info'=>$data)));
$this->server->push($this->fd, $reData);
}
public function onlineNum()
{
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('num'=>count($GLOBALS['USER_LIST']))));
$this->server->push($this->fd, $reData);
}
public function uid()
{
$uid = get_user_uid($this->fd);
$result = array(
'c'=>1
,'msg'=>'登录成功'
,'d'=>array('uid'=>$uid)
);
$this->server->push($this->fd, json_encode($result));
}
public function login() {
if(empty($this->reqData['uid'])) {
$result = array(
......@@ -28,16 +54,10 @@ class user extends base {
$this->server->push($this->fd, json_encode($result));
}
public function info() {
$data = post_curl('http://ylc.test.llewan.com/shop/goodsList',array('id'=>65421));
$this->server->push($this->fd, json_encode($data));
return 'success';
}
public function cache() {
$data = $GLOBALS['redisDB']->get('hezijp001');
//$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('list'=>$data)));
$this->server->push($this->fd, 9999);
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('list'=>json_decode($data,true))));
$this->server->push($this->fd, $reData);
}
public function msg() {
......@@ -47,14 +67,23 @@ class user extends base {
,'msg'=>'uid参数不存在'
);
} else {
$fd = $GLOBALS['USER_LIST']->get($this->reqData['uid'])['fd'];
$fd = get_user_fd($this->reqData['uid']);
if(!empty($fd)) {
$this->fd = $fd;
$result = array(
//消息发送者
$send1 = array(
'c'=>1
,'msg'=>'发送成功'
,'d'=>array('text'=>$this->reqData['text'])
);
$this->server->push($this->fd, json_encode($send1));
//消息结束者
$send2 = array(
'c'=>1
,'msg'=>'接收到新信息'
,'d'=>array('uid'=>get_user_uid($this->fd),'text'=>$this->reqData['text'])
);
$this->server->push($fd, json_encode($send2));
return 'success';
} else {
$result = array(
'c'=>2
......@@ -65,6 +94,7 @@ class user extends base {
}
$this->server->push($this->fd, json_encode($result));
return 'fail';
}
}
\ No newline at end of file
<?php
namespace socket\model;
include_once("application/socket/model/BaseModel.php");//uesrModel
include_once(ROOT_PATH."application/socket/model/BaseModel.php");//uesrModel
use socket\model\BaseModel;
class UserModel extends BaseModel {
public function test()
public function getData()
{
$data = $GLOBALS['mysqlDB']->select('*')->from('game_member_login_log')->limit(10)->query();
$data = $GLOBALS['mysqlDB']->select('*')->from('game_member')->orderBy(array("rand()"))->limit(10)->query();
return $data;
}
public function getOne() {
$data = $GLOBALS['mysqlDB']->select('*')->from('game_member')->orderBy(array("rand()"))->row();
return $data;
}
......
......@@ -16,6 +16,8 @@ $GLOBALS["TASK_LIST"] = new swoole_table(20480);
$GLOBALS['TASK_LIST']->column('task',swoole_table::TYPE_INT, 8);
$GLOBALS['TASK_LIST']->create();
// 定义根目录
define('ROOT_PATH',dirname(__FILE__).'/');
// 引入socketServer文件
require './swoole/socketServer.php';
$server = new socketServer("0.0.0.0","9501");
\ No newline at end of file
$server = new socketServer("0.0.0.0","9502");
\ No newline at end of file
......@@ -53,7 +53,7 @@ class socketServer
//此事件在Worker进程时发生(加载socket业务文件)
public function onWorkerStart( $server , $worker_id) {
require_once "./swoole/socketWork.php";
require ROOT_PATH."swoole/socketWork.php";
$this->handle = new socketWork($server);
}
......
<?php
include_once("application/config.php");//配置信息
include_once("application/common.php");//公共方法
include_once("extend/mysql/Connection.php");//mysql方法
require_once ROOT_PATH."application/conf/config.php";//配置信息
require_once ROOT_PATH."application/common.php";//公共方法
require_once ROOT_PATH."extend/mysql/Connection.php";//mysql方法
class socketWork {
......@@ -30,8 +30,9 @@ class socketWork {
public function onOpen($server, $frame) {
$uid = 'fd'.$frame->fd;
$GLOBALS['USER_LIST']->set($uid,array('fd'=>$frame->fd));
$server->push($frame->fd, "你好:$uid");
set_user_list(array('fd'=>$frame->fd,'uid'=>$uid));
$reData = json_encode(array('c'=>1,'msg'=>'websocket连接成功','d'=>array('uid'=>$uid)));
$server->push($frame->fd, $reData);
}
......@@ -72,7 +73,7 @@ class socketWork {
$act = new $data['m']();
//是否有这个方法在
if (!method_exists($act, $data['a'])) {
$server->push($frame->fd, "找不到对应的方法");
$server->push($data['fd'], "找不到对应的方法");
$this->server->finish($task_Id);
return false;
} else {
......@@ -95,94 +96,13 @@ class socketWork {
}
public function onClose($server, $fd) {
$GLOBALS['USER_LIST']->del('fd'.$fd);
del_user_list($fd);
echo 'client'.$fd.'下线';
//$this->redis->hdel($this->userTable,"fd{$fd}");
//$this->sendAll($server,"用户{$fd}下线了");
}
//根据用户ID查用户的连接记录
public function get_user_by_uid($uid){
echo_log(__CLASS__."->".__FUNCTION__."\r\n");
if($GLOBALS['USER_LIST']->exist($uid)){
$tmp_val = $GLOBALS['USER_LIST']->get($uid);
return $tmp_val;
}else{
return false;
}
}
//根据用户的fd查用户的uid
public function get_user_by_fd($fd){
echo_log(__CLASS__."->".__FUNCTION__."\r\n");
if($GLOBALS['USER_LIST_FD']->exist($fd)){
$tmp_val = $GLOBALS['USER_LIST_FD']->get($fd);
return $tmp_val;
}else{
return false;
}
}
//设置用户在线
/*
$data = array(
"fd" => ,//"连接",
"uid" => ,//用户ID,
);
*/
public function set_user_list($data){
//删掉这个uid之前绑定的连接
$tmp_uid = $GLOBALS['USER_LIST']->get($data['uid']);
if($tmp_uid && $tmp_uid['fd']>0){
$GLOBALS['USER_LIST_FD']->del($tmp_uid['fd']);
$GLOBALS['USER_LIST']->del($data['uid']);
}
$GLOBALS['USER_LIST']->set($data['uid'],array('fd'=>$data['fd']));
$GLOBALS['USER_LIST_FD']->set($data['fd'],array('uid'=>$data['uid']));
return true;
}
//给某个用户发信息
public function send_user($fd,$data,$opcode=1,$finish=true){
echo_log(__CLASS__."->".__FUNCTION__."\r\n");
if(empty($fd)){
return false;
}
if(is_array($data)){
$data = return_ajax($data);
echo $data;
}
//检测通道是否存在
if(!$this->server->exist($fd)){
echo_log("通道[ $fd ]的客户端已经断开,无法发送消息!\r\n",$data);
unset($data);
return false;
}else{
$this->server->push($fd,$data,$opcode,$finish);
unset($data);
return $finish;
}
}
//给指定用户发信息
public function send_all($arr,$data,$opcode=1,$finish=true){
echo_log(__CLASS__."->".__FUNCTION__."\r\n");
if(!empty($arr)){
foreach($arr as $uid){
if($GLOBALS['USER_LIST']->exist($uid)){
$tmp_fd = $GLOBALS['USER_LIST']->get($uid);
if($this->send_user($tmp_fd['fd'],$data,$opcode,$finish)){
echo_log('用户['. $uid .']发送消息成功!\r\n');
}else{
echo_log('用户['. $uid .']的客户端已经断开,无法发送消息!\r\n');
}
}
}
}else{
echo_log('没有指定要发信息的用户,无法发送消息!\r\n');
}
return true;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment