WebSockets - send json data via php

前端 未结 4 836
生来不讨喜
生来不讨喜 2021-01-05 07:05

I\'m trying to send a fire-and-forget request from PHP to my websocket aws api gateway.

I\'ve set up an action called \"sendmessage\".

This is the code I\'m

4条回答
  •  没有蜡笔的小新
    2021-01-05 07:32

    Since you didn't provide a handpoint link, here is some notes, following own tests!

    I guess the issue comes from the wss part, php needs to retrieve the certificate first, so it can encrypt the data.

    Your code should work just fine on a ws:// stream.

    To connect to a regular ws:// stream, one can simply use fsockopen().

    \n";
    } else {
        fwrite($fp, "\n");
        echo "Connected!";
        echo fread($fp, 26);
        fclose($fp);
    }
    

    But to connect to a wss:// secure websocket stream, using php, without libraries, we need to create a tunnel first, by querying the public key with stream_socket_client.

    This is a handshake mechanism. This can be done as follow.

    Notice the first ssl:// call. This is the TLS 1.0 protocol.

    The output should looks like:

    string(44) "HTTP/1.1 101 Web Socket Protocol Handshake"
    string(21) "Connection: Upgrade"
    string(37) "Date: Thu, 12 Dec 2019 04:06:27 GMT"
    string(52) "Sec-WebSocket-Accept: fTYwcEa6D9kJBtghptkz1e9CtBI="
    string(25) "Server: Kaazing Gateway"
    string(20) "Upgrade: websocket"
    

    Same base code, another example, pulling data from Binance wss:// stream.

    We can also use TLS 1.2, with a tls:// handshake instead. Works on most servers.


    Here is a way to retrieve only the ssl RSA public key of a remote handpoint, from php. Can be used to speed up later connections.

     true,
      "capture_peer_cert_chain" => true
    ];
    $a = stream_context_create(["ssl"=>$opt]);
    $b = stream_socket_client("ssl://stream.binance.com:9443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $a);
    $cont = stream_context_get_params($b);
    $key = openssl_pkey_get_public($cont["options"]["ssl"]["peer_certificate"]);
    $c = openssl_pkey_get_details($key);
    var_dump($c["key"]);
    

    Output something like:

    -----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhki(...)7aEsFtUNkwM5R5b1mpqzAwqHyvdamJx20bT6SS6
    PYXSr/dv8ak1d4e2Q0nIa1O7l3w0bZZ4wnp5B8Z+tjPd1W8uaZoRO2iVkPMh2yPl
    j0mmtUw1YlfDyutH/t4FlRCDiD4JjdREQGs381/+jbkdjl2SIb1IyNiCdAXA6zsq
    xwIDAQAB
    -----END PUBLIC KEY-----
    

    There is possibly other quircks, to be sure, we need the main handpoint^. Would be glad to test that. Otherwise good luck, there is a big lack of documentation on the subject.

    This is still a new born protocol (2011!). Best details are in the RFC specification:

    The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011

    About the handshake, it must be initiated by a GET request.

    The client will send a pretty standard HTTP request with headers that looks like this (the HTTP version must be 1.1 or greater, and the method must be GET)

    Writing_WebSocket_servers#Client_handshake_request


    In short:

    If unencrypted WebSocket traffic flows through an explicit or a transparent proxy server without WebSockets support, the connection will likely fail.

    WebSocket#Proxy_traversal

    Transport_Layer_Security#Digital_certificates

提交回复
热议问题