簡體   English   中英

無法使用新的 websocket 協議發送消息

[英]Can't send a message with the new websocket protocol

我正在嘗試在 php 中開發 websocket 服務器。

這是我到目前為止所做的:(服務器部分)

$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, "localhost", 10000);
socket_listen($server);

$client = socket_accept($server);

$message = socket_read($client, 5000);

$matches = array();

preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $message, $matches);

$new_key = new_key($matches[1]);

$new_message = "HTTP/1.1 101 Switching Protocols\r\n";
$new_message .= "Upgrade: websocket\r\n";
$new_message .= "Connection: Upgrade\r\n";
$new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";

socket_write($client, $new_message, strlen($new_message));

$new_message = "Test message !";

socket_write($client, $new_message, strlen($new_message));

function new_key($key)
{
    $key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    $key = sha1($key);
    $new_key = '';

    for ($i = 0; $i < strlen($key); $i+=2)
    {
        $new_key .= chr(intval($key[$i] . $key[$i+1], 16));
    }

    $new_key = base64_encode($new_key);

    return $new_key;
}

/* End of file server.php */

(客戶端部分)

window.onload = function() {
    var ws = new WebSocket('ws://localhost:10000');

    ws.onopen = function() {
        alert('Connection open !');

        setInterval(function() {
            ws.send('lol');
        }, 1000);
    }

    ws.onclose = function() {
        alert('WebSocket close !');
    }

    ws.onmessage = function(e) {
        alert(e.data);
    }

    ws.onerror = function(e) {
        alert('Error');
    }
};

第一個socket_write完美運行(用於握手)並且正確觸發了open事件。 但是第二個 socket_write 不起作用。 我嘗試在服務器部分使用第二個socket_readopen事件執行ws.send ,但它也不起作用。

謝謝你,如果你有一個想法!

我有一個類似的問題。 After checking out this project over here https://github.com/srchea/PHP-Push-WebSocket I noticed a function called "encode" in this file https://github.com/srchea/PHP-Push-WebSocket/blob /master/lib/Server.class.php在發送消息之前。

所以試着改變這個

socket_write($client, $new_message, strlen($new_message));
$new_message = "Test message !";
socket_write($client, $new_message, strlen($new_message));

對此

socket_write($client, $new_message, strlen($new_message));
$new_message = "Test message !";
socket_write($client, chr(129) . chr(strlen($new_message)) ."". $new_message);

我認為這是一個 INVALID_STATE_ERR: DOM Exception 11 意味着同步問題。 在你的客戶端試試這個:

var test = function(){
    var myWebSocket = new WebSocket('ws://localhost:10000');
    myWebSocket.onopen = function(e){
        alert('connection open');
        this.send('test1');
        this.send('test2');
    }
    myWebSocket.onmessage = function(e){ alert('message ' + e.data) };
    myWebSocket.onclose = function(e){ alert('connection closed'); this.close(); };
    myWebSocket.send('test3');
};

test3 將失敗,因為它是異步 websocket。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM