繁体   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