Socket.send()不发送

时间:2017-02-28 11:53:35

标签: javascript php sockets

我正在使用PHP套接字并尝试使用js连接它。

我的JS代码是:

        var socket = new WebSocket("ws://localhost:9090/");


        setTimeout(
                socket.onopen = function()
                {
                    try {
                        socket.send('\n');
                    }catch (e){
                        alert(e);
                    }
                    try {
                        socket.send('{"link":"1"}');
                    }catch (e){
                        alert(e);
                    }
                    alert("Connected");
                    // Web Socket is connected, send data using send()
                }
        , 5000);

PHP代码是:

$host = '127.0.0.1';
$port = '9090';
$null = NULL;
include_once ("TransmissionRPC.php");
$rpc = new TransmissionRPC();
$rpc->return_as_array = true;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);
socket_listen($socket);
socket_set_block($socket);
$clients = array($socket);
while(true){
    $changed = $clients;
    socket_select($changed, $null, $null, 0, 10);
    if (in_array($socket, $changed)){
        $socket_new = socket_accept($socket);
        $clients[] = $socket_new;
        print_r($clients);

        $header = socket_read($socket_new, 1024);
        perform_handshaking($header, $socket_new, $host, $port);

        socket_getpeername($socket_new, $ip);

        //make room for new socket
        $found_socket = array_search($socket, $changed);
        unset($changed[$found_socket]);
    }
    foreach($changed as $changed_socket){
        while($input = socket_read($changed_socket, 1024)){
            print_r($input);
            $received_text = trim($input);
            $recived = json_decode($received_text);
            if (array_key_exists('session', $recived)){
                $session_id = $recived->{'session'};
                $info = $rpc->get($session_id, array());
                send_message(json_encode($info), 1);
            } else if(array_key_exists('link', $recived)) {
                $link = $recived->{'link'};
                $result = $rpc->sstats();
                $test_torrent = "http://www.slackware.com/torrents/slackware64-13.1-install-dvd.torrent";
                $result = $rpc->add( $test_torrent, '/home/ftpacc/domains/ali.com/public_html/' );
                send_message(json_encode($result), 1);
            }
            break 2;
        }
        $buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
        if ($buf === false) {
            $found_socket = array_search($changed_socket, $clients);
            socket_getpeername($changed_socket, $ip);
            unset($clients[$found_socket]);
            $response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' disconnected \n')));
            echo "DC";
        }
    }
}
function perform_handshaking($receved_header, $client_conn, $host, $port)
{
    $headers = array();
    $lines = preg_split("/\r\n/", $receved_header);
    foreach ($lines as $line){
        $line = chop($line);
        if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
            $headers[$matches[1]] = $matches[2];
        }
    }
}
function mask($text)
{
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);

    if ($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif ($length > 125 && $length < 65536)
        $header = pack('CCn', $b1, 126, $length);
    elseif ($length >= 65536)
        $header = pack('CCNN', $b1, 127, $length);
    return $header . $text;
}
function send_message($msg, $reciver_id)
{
    global $clients;
    @socket_send($clients[$reciver_id] , $msg , strlen($msg) , 0);
    var_dump($clients);
    return true;
}

当我使用socket.send('{“link”:“1”}');不工作。

它给我这个错误:

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

我该如何解决这个问题?

注意:当我通过TELNET连接到套接字时它正常工作。

0 个答案:

没有答案