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

抽离配置文件

parent d077e892
...@@ -67,7 +67,39 @@ function cache($config) { ...@@ -67,7 +67,39 @@ function cache($config) {
return $con; return $con;
} }
//解码(主要针对特殊符号和emoji表情)
function emoji_decode($str){
if(empty($str)){
return $str;
}
$str = preg_replace_callback('/@E(.{6}==)/', function ($r) {
return base64_decode($r[1]);
}, $str);
return $str;
}
function createLoginToken($data)
{
$arr = json_encode($arr);
return base64_encode(uc_authcode($arr, 'ENCODE'));
}
function decryptToken($token)
{
if($token){
$val = uc_authcode(base64_decode($token),'DECODE');
if(!empty($val)){
$val = json_decode($val,true);
}else{
$val = false;
}
return $val;
}else{
return false;
}
}
/***********************************socket公用方法start********************************************************/
//根据用户ID查用户的连接记录 //根据用户ID查用户的连接记录
function get_user_fd($uid){ function get_user_fd($uid){
if($GLOBALS['USER_LIST']->exist($uid)){ if($GLOBALS['USER_LIST']->exist($uid)){
...@@ -114,7 +146,7 @@ function del_user_list($fd){ ...@@ -114,7 +146,7 @@ function del_user_list($fd){
} }
//给某个用户发信息 //给某个用户发信息
function send_user($server,$uid,$data,$opcode=1){ function send_user($server,$uid,$data,$opcode=1,$finish=true){
if(empty($uid)){ if(empty($uid)){
return false; return false;
} }
...@@ -135,7 +167,7 @@ function send_user($server,$uid,$data,$opcode=1){ ...@@ -135,7 +167,7 @@ function send_user($server,$uid,$data,$opcode=1){
function send_group($server,$arr,$data,$opcode=1){ function send_group($server,$arr,$data,$opcode=1){
if(!empty($arr)){ if(!empty($arr)){
foreach($arr as $uid){ foreach($arr as $uid){
if(send_user($uid['uid'],$data,$opcode)){ if(send_user($server,$uid,$data,$opcode)){
echo_log('用户['. $uid .']发送消息成功!\r\n'); echo_log('用户['. $uid .']发送消息成功!\r\n');
}else{ }else{
echo_log('用户['. $uid .']的客户端已经断开,无法发送消息!\r\n'); echo_log('用户['. $uid .']的客户端已经断开,无法发送消息!\r\n');
...@@ -148,7 +180,7 @@ function send_group($server,$arr,$data,$opcode=1){ ...@@ -148,7 +180,7 @@ function send_group($server,$arr,$data,$opcode=1){
} }
//给所有用户发信息 //给所有用户发信息
function send_all($server,$data,$opcode=1){ function send_all($server,$data,$opcode=1,$finish=true){
if(!empty($GLOBALS['USER_LIST'])){ if(!empty($GLOBALS['USER_LIST'])){
foreach($GLOBALS['USER_LIST'] as $fd){ foreach($GLOBALS['USER_LIST'] as $fd){
$server->push($fd['fd'],$data,$opcode,$finish); $server->push($fd['fd'],$data,$opcode,$finish);
...@@ -162,4 +194,5 @@ function send_all($server,$data,$opcode=1){ ...@@ -162,4 +194,5 @@ function send_all($server,$data,$opcode=1){
echo_log('没有指定要发信息的用户,无法发送消息!\r\n'); echo_log('没有指定要发信息的用户,无法发送消息!\r\n');
} }
return true; return true;
} }
\ No newline at end of file /***********************************socket公用方法end********************************************************/
\ No newline at end of file
<?php <?php
$GLOBALS['config'] = array( $GLOBALS['config'] = array(
'SERVER_NAME'=>"test",
'MAIN_SERVER'=>[
'HOST'=>'0.0.0.0',
'PORT'=>9502,
'SETTING'=>[
'open_tcp_nodelay' => 1,
'open_cpu_affinity' => 1,
'task_worker_num' =>8,
'dispatch_mode'=>2,
'daemonize' => 0,//守护进程化。设置daemonize => 1时,程序将转入后台作为守护进程运行。长时间运行的服务器端程序必须启用此项。
'reactor_num' => 2,//2个处理线程的数量
'worker_num' => 4,//4个线程
'max_request' => 0,//最大10万个连接请求
// 'heartbeat_idle_time' => 60,//如果客户端超过600秒未向服务器发消息
// 'heartbeat_check_interval' =>61,//每60秒检查一次所有的连接
'log_file' => ROOT_PATH.'swoole.log'
]
],
'REDIS'=>array( 'REDIS'=>array(
'type'=>'Redis', 'type'=>'Redis',
'host'=>'127.0.0.1', // 119.23.249.188 (部署) 'host'=>'127.0.0.1', // 119.23.249.188 (部署)
......
...@@ -8,10 +8,10 @@ class base { ...@@ -8,10 +8,10 @@ class base {
public function setData($data) public function setData($data)
{ {
if(!empty($data['server']) && !empty($data['fd']) && !empty($data['d'])) { if(!empty($data['server'])) {
$this->server = $data['server']; $this->server = $data['server'];
$this->fd = $data['fd']; $this->fd = $data['fd'] ?? '';
$this->reqData = $data['d']; $this->reqData = $data['d'] ?? '';
return true; return true;
} else { } else {
return false; return false;
......
...@@ -3,24 +3,22 @@ include_once(ROOT_PATH."application/socket/model/UserModel.php");//uesrModel ...@@ -3,24 +3,22 @@ include_once(ROOT_PATH."application/socket/model/UserModel.php");//uesrModel
class user extends base { class user extends base {
public function list() public function mysqlData()
{ {
$userModel = new \socket\model\UserModel(); $userModel = new \socket\model\UserModel();
$data = $userModel->getData(); $data = $userModel->getOne();
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('info'=>$data))); $reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('info'=>$data)));
$this->server->push($this->fd, $reData); $this->server->push($this->fd, $reData);
} }
public function info() public function redisData() {
{ $data = $GLOBALS['redisDB']->get('hezijp001');
$userModel = new \socket\model\UserModel(); $reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('list'=>json_decode($data,true))));
$data = $userModel->getOne();
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('info'=>$data)));
$this->server->push($this->fd, $reData); $this->server->push($this->fd, $reData);
} }
public function onlineNum() public function online_num()
{ {
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('num'=>count($GLOBALS['USER_LIST'])))); $reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('num'=>count($GLOBALS['USER_LIST']))));
$this->server->push($this->fd, $reData); $this->server->push($this->fd, $reData);
...@@ -31,70 +29,105 @@ class user extends base { ...@@ -31,70 +29,105 @@ class user extends base {
$uid = get_user_uid($this->fd); $uid = get_user_uid($this->fd);
$result = array( $result = array(
'c'=>1 'c'=>1
,'msg'=>'登录成功' ,'msg'=>'获取成功'
,'d'=>array('uid'=>$uid) ,'d'=>array('uid'=>$uid)
); );
$this->server->push($this->fd, json_encode($result)); $this->server->push($this->fd, json_encode($result));
} }
public function login() { public function set_info() {
if(empty($this->reqData['uid'])) { $token = decryptToken($this->reqData['token']);
if($token == false) {
$result = array( $result = array(
'c'=>2 'c'=>2
,'msg'=>$this->reqData['uid'] ,'msg'=>'token不正确'
); );
} else { } else {
$GLOBALS['USER_LIST']->set($this->reqData['uid'],array('fd'=>$this->fd)); set_user_list(array('fd'=>$frame->fd,'uid'=>$token['uid']));
$result = array( $result = array(
'c'=>1 'c'=>1
,'msg'=>'登录成功' ,'msg'=>'保存成功'
); );
} }
$this->server->push($this->fd, json_encode($result)); $this->server->push($this->fd, json_encode($result));
} }
public function cache() {
$data = $GLOBALS['redisDB']->get('hezijp001'); //广播(给所有在线用户发送消息)
$reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('list'=>json_decode($data,true)))); public function broadcast() {
$this->server->push($this->fd, $reData); if(empty($this->reqData['content'])) {
$result = array(
'c'=>2
,'msg'=>'content参数不存在'
);
} else {
//发送消息给接收者
$consignee = array(
'c'=>1
,'msg'=>'接收到广播信息'
,'d'=>array('sender'=>get_user_uid($this->fd),'content'=>$this->reqData['content'])
);
send_all($this->server,json_encode($consignee));
//发送成功,通知发送者
$sender = array(
'c'=>1
,'msg'=>'信息发送成功'
);
$this->server->push($this->fd, json_encode($sender));
return 'success';
}
$this->server->push($this->fd, json_encode($result));
return 'fail';
} }
public function msg() { //给指定用户发送消息
if(empty($this->reqData['uid'])) { public function send_msg() {
if(empty($this->reqData['to_uid'])) {
$result = array( $result = array(
'c'=>2 'c'=>2
,'msg'=>'uid参数不存在' ,'msg'=>'to_uid参数不存在'
);
} else if(empty($this->reqData['content'])) {
$result = array(
'c'=>2
,'msg'=>'content参数不存在'
); );
} else { } else {
$fd = get_user_fd($this->reqData['uid']);
if(!empty($fd)) { //发送消息给接收者
//消息发送者 $consignee = array(
$send1 = array( 'c'=>1
'c'=>1 ,'msg'=>'接收到新信息'
,'msg'=>'发送成功' ,'d'=>array('sender'=>$this->reqData['to_uid'],'content'=>$this->reqData['content'])
); );
$this->server->push($this->fd, json_encode($send1));
if(is_array($this->reqData['to_uid'])) {
//消息结束者 $re1 = send_group($this->server,$this->reqData['to_uid'],json_encode($consignee));
$send2 = array( } else {
$re1 = send_user($this->server,$this->reqData['to_uid'],json_encode($consignee));
}
if($re1) {
//发送成功,通知发送者
$sender = array(
'c'=>1 'c'=>1
,'msg'=>'接收到新信息' ,'msg'=>'信息发送成功'
,'d'=>array('uid'=>get_user_uid($this->fd),'text'=>$this->reqData['text'])
); );
$this->server->push($fd, json_encode($send2)); if(!empty($this->fd)) {
return 'success'; $this->server->push($this->fd, json_encode($sender));
}
return json_encode($sender);
} else { } else {
$result = array( $result = array(
'c'=>2 'c'=>2
,'msg'=>'uid参数错误' ,'msg'=>'信息发送失败'
); );
} }
} }
$this->server->push($this->fd, json_encode($result)); $this->server->push($this->fd, json_encode($result));
return 'fail'; return json_encode($sender);
} }
} }
\ No newline at end of file
...@@ -20,4 +20,4 @@ $GLOBALS['TASK_LIST']->create(); ...@@ -20,4 +20,4 @@ $GLOBALS['TASK_LIST']->create();
define('ROOT_PATH',dirname(__FILE__).'/'); define('ROOT_PATH',dirname(__FILE__).'/');
// 引入socketServer文件 // 引入socketServer文件
require './swoole/socketServer.php'; require './swoole/socketServer.php';
$server = new socketServer("0.0.0.0","9502"); $server = new socketServer($GLOBALS['config']['MAIN_SERVER']['HOST'],$GLOBALS['config']['MAIN_SERVER']['PORT']);
\ No newline at end of file \ No newline at end of file
<?php <?php
require_once ROOT_PATH."application/conf/config.php";//配置信息
class socketServer class socketServer
{ {
private $server; private $server;
...@@ -7,20 +7,7 @@ class socketServer ...@@ -7,20 +7,7 @@ class socketServer
public function __construct($host,$port) { public function __construct($host,$port) {
$this->server = new swoole_websocket_server($host, $port); $this->server = new swoole_websocket_server($host, $port);
$this->server->set(array( $this->server->set($GLOBALS['config']['MAIN_SERVER']['SETTING']);
'open_tcp_nodelay' => 1,
'open_cpu_affinity' => 1,
'task_worker_num' =>8,
'dispatch_mode'=>2,
'daemonize' => 0,//守护进程化。设置daemonize => 1时,程序将转入后台作为守护进程运行。长时间运行的服务器端程序必须启用此项。
'reactor_num' => 2,//2个处理线程的数量
'worker_num' => 4,//4个线程
'max_request' => 0,//最大10万个连接请求
// 'heartbeat_idle_time' => 60,//如果客户端超过600秒未向服务器发消息
// 'heartbeat_check_interval' =>61,//每60秒检查一次所有的连接
'log_file' => './swoole.log'
));
$this->server->on('start', array($this, 'onStart')); $this->server->on('start', array($this, 'onStart'));
$this->server->on('shutdown', array($this, 'onShutdown')); $this->server->on('shutdown', array($this, 'onShutdown'));
......
<?php <?php
require_once ROOT_PATH."application/conf/config.php";//配置信息
require_once ROOT_PATH."application/common.php";//公共方法 require_once ROOT_PATH."application/common.php";//公共方法
require_once ROOT_PATH."extend/mysql/Connection.php";//mysql方法 require_once ROOT_PATH."extend/mysql/Connection.php";//mysql方法
...@@ -48,7 +47,7 @@ class socketWork { ...@@ -48,7 +47,7 @@ class socketWork {
$tmp_task_id = $this->server->task($data); $tmp_task_id = $this->server->task($data);
echo_log("进行task任务:".$tmp_task_id); echo_log("进行task任务:".$tmp_task_id);
}else{ }else{
$server->push($frame->fd, "找不到对应的模块"); $this->server->push($frame->fd, json_encode(array('c'=>2,'msg'=>'找不到对应的模块')));
} }
} }
...@@ -57,13 +56,20 @@ class socketWork { ...@@ -57,13 +56,20 @@ class socketWork {
if($event == 'reload') { if($event == 'reload') {
$server->reload(); $server->reload();
$response->write('reload success'); $response->write('reload success');
}else if($event == 'sendAll') { } else if($event == 'send_msg') {
$this->sendAll($server,'大家好'); //通过uid给用户发送消息
$response->write($GLOBALS['USER_LIST']->get('fd3')); $act = new user();
$data = array(
'server'=>$server
,'d'=>$request->post
);
$act->setData($data);
$result = $act->send_msg();
$response->write($result);
} else { } else {
// $data = $this->server->db->select('sysid as id,name')->from('yl_goods')->where('sysid=1')->row(); //默认返回
// $reData = json_encode(array('c'=>1,'msg'=>'success','d'=>array('list'=>$data))); $response->write('websocket response');
$response->write('websocket server respone');
} }
} }
...@@ -73,7 +79,7 @@ class socketWork { ...@@ -73,7 +79,7 @@ class socketWork {
$act = new $data['m'](); $act = new $data['m']();
//是否有这个方法在 //是否有这个方法在
if (!method_exists($act, $data['a'])) { if (!method_exists($act, $data['a'])) {
$server->push($data['fd'], "找不到对应的方法"); $this->server->push($data['fd'], json_encode(array('c'=>2,'msg'=>'找不到对应的方法')));
$this->server->finish($task_Id); $this->server->finish($task_Id);
return false; return false;
} else { } else {
...@@ -97,9 +103,7 @@ class socketWork { ...@@ -97,9 +103,7 @@ class socketWork {
public function onClose($server, $fd) { public function onClose($server, $fd) {
del_user_list($fd); del_user_list($fd);
echo 'client'.$fd.'下线'; echo_log('client'.$fd.'下线');
//$this->redis->hdel($this->userTable,"fd{$fd}");
//$this->sendAll($server,"用户{$fd}下线了");
} }
......
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