如何保持PHP套接字客户端连接打开,直到用户使用socket_closed()

时间:2016-02-17 13:31:42

标签: php websocket

我有一个按钮" connect" ! 当我按下该按钮时,我想打开一个到ip:端口的php套接字连接,并且连接应该保持打开状态以发送和接收数据消息,直到用户点击; s断开连接。我怎样才能做到这一点?我有以下代码:

if (isset($_POST['cmd_connect'])) {
    // select in baza de date nodes pentru randul selectat de user
    $selectOption = $_POST['cmb_select_node'];
    $sqlquery = mysql_query("SELECT * FROM `nodes` WHERE node_mac='{$selectOption}'");
    if ($sqlquery === FALSE) {
        die(mysql_error());
    }
    while ($row = mysql_fetch_array($sqlquery)) {
        $host = $row['node_ip'];
        $port = 11000;
    };
    // don't timeout!
    set_time_limit(0);
        // Creaza si se connecteaza la socket internet IP

        $socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        $s_connect=socket_connect($socket, $host, $port);
        $timeout= array('sec'=>30,'usec'=>500000);
        socket_set_option($socket, SOL_SOCKET , SO_KEEPALIVE, 1);
        socket_set_option($socket, SOL_SOCKET , SO_RCVTIMEO,$timeout);


        // Conditie creare si connectare socket
       if (! $socket) {
           ?> <script>alert('Could not create the socket to client');</script>
            <?php
        } 
             elseif($s_connect) {
             ?> <script>alert('Your Are now connected');</script>
            <?php
        }

    }


?>

感谢

0 个答案:

没有答案