Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
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
wallet
token
Commits
a33c4fc6
Commit
a33c4fc6
authored
Aug 23, 2019
by
shajiaiming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test
parent
fa3d1e88
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
2 deletions
+29
-2
WorkermanWebSocketController.php
console/controllers/WorkermanWebSocketController.php
+29
-2
No files found.
console/controllers/WorkermanWebSocketController.php
View file @
a33c4fc6
...
@@ -57,6 +57,8 @@ class WorkermanWebSocketController extends Controller
...
@@ -57,6 +57,8 @@ class WorkermanWebSocketController extends Controller
{
{
$ip
=
isset
(
$this
->
config
[
'ip'
])
?
$this
->
config
[
'ip'
]
:
$this
->
ip
;
$ip
=
isset
(
$this
->
config
[
'ip'
])
?
$this
->
config
[
'ip'
]
:
$this
->
ip
;
$port
=
isset
(
$this
->
config
[
'port'
])
?
$this
->
config
[
'port'
]
:
$this
->
port
;
$port
=
isset
(
$this
->
config
[
'port'
])
?
$this
->
config
[
'port'
]
:
$this
->
port
;
define
(
'HEARTBEAT_TIME'
,
5
);
$wsWorker
=
new
Worker
(
"websocket://
{
$ip
}
:
{
$port
}
"
);
$wsWorker
=
new
Worker
(
"websocket://
{
$ip
}
:
{
$port
}
"
);
// 4 processes
// 4 processes
...
@@ -68,9 +70,34 @@ class WorkermanWebSocketController extends Controller
...
@@ -68,9 +70,34 @@ class WorkermanWebSocketController extends Controller
echo
"New connection
\n
"
;
echo
"New connection
\n
"
;
};
};
// $wsWorker->onConnect = function($connection) {
// // 给链接对象临时赋值一个lastTime属性记录上次接收消息的时间
// $connection->lastTime = time();
// };
// 进程启动后设置一个每秒运行一次的定时器
$wsWorker
->
onWorkerStart
=
function
(
$worker
)
{
\Workerman\Lib\Timer
::
add
(
1
,
function
()
use
(
$worker
){
$time_now
=
time
();
foreach
(
$worker
->
connections
as
$connection
)
{
// 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间
if
(
empty
(
$connection
->
lastMessageTime
))
{
$connection
->
lastMessageTime
=
$time_now
;
continue
;
}
// 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接
if
(
$time_now
-
$connection
->
lastMessageTime
>
HEARTBEAT_TIME
)
{
$connection
->
close
();
}
}
});
};
// Emitted when data received
// Emitted when data received
$wsWorker
->
onMessage
=
function
(
$connection
,
$data
)
use
(
$wsWorker
,
$uids
)
{
$wsWorker
->
onMessage
=
function
(
$connection
,
$data
)
use
(
$wsWorker
,
$uids
)
{
if
(
!
isset
(
$connection
->
uid
))
{
if
(
!
isset
(
$connection
->
uid
))
{
$connection
->
lastMessageTime
=
time
();
$connection
->
uid
=
$data
;
$connection
->
uid
=
$data
;
$wsWorker
->
uidConnections
[
$connection
->
uid
]
=
$connection
;
$wsWorker
->
uidConnections
[
$connection
->
uid
]
=
$connection
;
echo
$connection
->
uid
.
' 一共 '
.
count
(
$wsWorker
->
uidConnections
[
$connection
->
uid
])
.
PHP_EOL
;
echo
$connection
->
uid
.
' 一共 '
.
count
(
$wsWorker
->
uidConnections
[
$connection
->
uid
])
.
PHP_EOL
;
...
@@ -100,7 +127,7 @@ class WorkermanWebSocketController extends Controller
...
@@ -100,7 +127,7 @@ class WorkermanWebSocketController extends Controller
}
}
}
}
}
}
$connection
->
send
(
'binance : '
.
json_encode
(
$ticker
)
,
$connection
->
id
);
$connection
->
send
(
'binance : '
.
json_encode
(
$ticker
));
};
};
$con
->
connect
();
$con
->
connect
();
}
elseif
(
'huobi'
==
$data
)
{
}
elseif
(
'huobi'
==
$data
)
{
...
@@ -145,7 +172,7 @@ class WorkermanWebSocketController extends Controller
...
@@ -145,7 +172,7 @@ class WorkermanWebSocketController extends Controller
}
}
}
}
}
}
$connection
->
send
(
'huobi : '
.
json_encode
(
$ticker
)
,
$connection
->
id
);
$connection
->
send
(
'huobi : '
.
json_encode
(
$ticker
));
}
else
{
}
else
{
}
}
};
};
...
...
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