Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
swooleframe
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
石振业
swooleframe
Commits
d077e892
Commit
d077e892
authored
Oct 17, 2018
by
734642908@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改配置文件目录
parent
1c8b6dd6
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
215 additions
and
128 deletions
+215
-128
README.md
README.md
+1
-1
common.php
application/common.php
+96
-15
config.php
application/conf/config.php
+2
-1
config.php
application/conf/prod/config.php
+23
-0
config.php
application/conf/test/config.php
+23
-0
user.php
application/socket/controller/user.php
+47
-16
UserModel.php
application/socket/model/UserModel.php
+8
-3
server.php
server.php
+4
-2
socketServer.php
swoole/socketServer.php
+1
-1
socketWork.php
swoole/socketWork.php
+10
-89
No files found.
README.md
View file @
d077e892
...
...
@@ -4,11 +4,11 @@ swoole框架websocket
##project 应用部署目录
*
├─application 应用目录(可设置)
*
│ ├─conf 应用(公共)配置文件
*
│ ├─socket 业务模块目录(可更改)
*
│ │ ├─controller 控制器目录
*
│ │ ├─model 模型目录
*
│ ├─common.php 应用公共(函数)文件
*
│ ├─config.php 应用(公共)配置文件
*
├─extend 扩展类库目录(可定义)
*
├─swoole swoole框架目录(可更改)
*
├─server.php 服务启动文件
...
...
application/common.php
View file @
d077e892
...
...
@@ -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
application/config.php
→
application/conf
/conf
ig.php
View file @
d077e892
...
...
@@ -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
application/conf/prod/config.php
0 → 100644
View file @
d077e892
<?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
application/conf/test/config.php
0 → 100644
View file @
d077e892
<?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
application/socket/controller/user.php
View file @
d077e892
<?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
application/socket/model/UserModel.php
View file @
d077e892
<?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
;
}
...
...
server.php
View file @
d077e892
...
...
@@ -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
swoole/socketServer.php
View file @
d077e892
...
...
@@ -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
);
}
...
...
swoole/socketWork.php
View file @
d077e892
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment