Commit 0dd97776 authored by shajiaiming's avatar shajiaiming

test

parent eb1c5912
<?php
namespace console\controllers;
use Workerman\Worker;
......@@ -67,22 +68,42 @@ class WorkermanWebSocketController extends Controller
// Emitted when data received
$wsWorker->onMessage = function ($connection, $data) {
// ssl需要访问443端口
$con = new \Workerman\Connection\AsyncTcpConnection("ws://stream.binance.com:9443/ws/!ticker@arr");
// 设置以ssl加密方式访问,使之成为wss
$con->transport = 'ssl';
// $con->onConnect = function($con) {
// $data = json_encode([
// 'sub' => 'market.btcusdt.kline.1min',
// 'id' => 'depth' . time()
// ]);
// $con->send($data);
// };
$con->onMessage = function($con, $data) use($connection) {
$connection->send(date("Y-m-d H:i:s") . ' : ' . $data);
};
$con->connect();
if ('binance' == $data) {
$con = new \Workerman\Connection\AsyncTcpConnection("ws://stream.binance.com:9443/ws/!ticker@arr");
$con->transport = 'ssl';
$con->onMessage = function($con, $data) use($connection) {
$base_coin = [
'ETH', 'BTC', 'USDT', 'BTC'
];
$result = json_decode($data, true);
$ticker = [];
foreach ($result as $val) {
go(function () use ($val, $base_coin) {
\Co::sleep(0.5);
foreach ($base_coin as $k => $coin) {
$explode_arr = explode($coin, $val['s']);
if (2 == count($explode_arr) && empty($explode_arr[1])) {
$ticker[$k]['symbol'] = $explode_arr[0] . '/' . $coin;
$ticker[$k]['close'] = (float)sprintf("%0.4f", $val['c']);
$ticker[$k]['change'] = (float)sprintf("%0.4f", $val['p'] * 100);
$ticker[$k]['high'] = (float)sprintf("%0.4f", $val['h']);
$ticker[$k]['low'] = (float)sprintf("%0.4f", $val['l']);
$ticker[$k]['vol'] = (float)sprintf("%0.4f", $val['v']);
}
}
});
}
$connection->send(date("Y-m-d H:i:s") . ' : ' . json_encode($ticker));
};
$con->connect();
} elseif ('huobi' == $data) {
$connection->send('huobi');
} elseif ('okex' == $data) {
$connection->send('okex');
} else {
$connection->send('other');
}
};
// Emitted when connection closed
......
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