前端刷新后无法使用

ACM_1821
<script>
ws = new WebSocket("ws://12.12.12.12:2346");
ws.onopen = function() {
    console.log("connect");
    ws.send('tom');
};
ws.onmessage = function(e) {
    console.log(e.data);
   // document.write(e.data)
};

</script>
<?php
require_once __DIR__ . '/Autoloader.php';

use Workerman\Worker;
use Workerman\Lib\Timer;

define('HEARTBEAT_TIME', 25);
// 创建一个Worker监听2346端口,使用websocket协议通讯
$ws_worker = new Worker("websocket://0.0.0.0:2346");

Worker::$logFile = '/home/workerman.log';

// 启动4个进程对外提供服务
$ws_worker->count = 4;

$ws_worker->onConnect = function($connection)
{
    echo 1;
    // 设置当前连接的应用层发送缓冲区大小为102400字节
    $connection->maxSendBufferSize = 5024000;
};

$ws_worker->onWorkerStart = function($worker) {
    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();
            }
        }
    });
};

$ws_worker->onClose = function($connection)
{
    echo 2;
    $connection->send("connection closed");
};

$ws_worker->onError = function($connection, $code, $msg)
{
    echo 3;
    $connection->send("error $code $msg\n");
};

// 当收到客户端发来的数据后返回hello $data给客户端
$ws_worker->onMessage = function($connection, $data)
{
    $connection->lastMessageTime = time();

    while(1){
        echo 4;
        $con = mysql_connect('localhost','root','123456') 
    or die('Could not connect to the server!');

mysql_select_db('laohu') 
    or die('Could not select a database.');

        $sql1 = "SELECT * FROM laohu_okcoin WHERE TYPE='bit'  order by create_time desc LIMIT 1";   

        $result1 = mysql_query($sql1);

        $row1 = mysql_fetch_assoc($result1);

        $sql2 = "SELECT * FROM laohu_okcoin WHERE TYPE='ltc'  order by create_time desc LIMIT 1";   

        $result2 = mysql_query($sql2);

        $row2 = mysql_fetch_assoc($result2);

        $return = $row1;
        $return = $row2;

        //mysql_close($con);
        $connection->send(json_encode($return));

        sleep(1);       
    }

};

// 运行worker
Worker::runAll();
2411 1 0
1个回答

walkor 打赏

onMessage里面while死循环了...

  • 暂无评论
年代过于久远,无法发表回答
×
🔝