dongzaliang4492 2016-10-25 19:15
浏览 148
已采纳

php / javascript中的SSL websocket

I am currently working on a real time notification service using websocket using TLS/SSL (wss://).
I have some problem for the handshake between the browser and the server. Everything works fine with a server and a client in php but when I use the JS's websocket to connect to the server, it fails because I don't know how to handle the handshake in server-side (from a browser).

So far my code for the server is :

$host = '127.0.0.1';
$port = '9000';
$null = NULL;

$context = stream_context_create();

// local_cert must be in PEM format
stream_context_set_option($context, 'ssl', 'local_cert', "cert.pem");
stream_context_set_option($context, 'ssl', 'local_pk', "key.pem");
// Pass Phrase (password) of private key
stream_context_set_option($context, 'ssl', 'passphrase', "test");
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);

// Create the server socket
$server = stream_socket_server('ssl://' . $host . ':' . $port, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);

if ($server == false) {
    die ("Could no create the server.");
}

//start endless loop

while (true) {
    $buffer = '';
    print "waiting...";
    $client = stream_socket_accept($server);
    var_dump($client);
    print "accepted " . stream_socket_get_name($client, true) . "
";
    if ($client) {
        stream_set_blocking($client, true); 
        // TODO : handshaking
        stream_set_blocking($client, false);

        // Respond to php client (test only)
        /*fwrite($client, "200 OK HTTP/1.1
"
            . "Connection: close
"
            . "Content-Type: text/html
"
            . "
"
            . "Hello World!");
        fclose($client);*/
    } else {
        print "error.
";
    }
}

Nothing is stated about the SSL handshake on the RFC WebSocket.
If anyone has some idea on how to implement a handshake, it would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongpinyao2203 2016-10-25 19:33
    关注

    Nothing is stated about the SSL handshake on the RFC WebSocket.

    wss:// is just ws:// inside a SSL connection, same as HTTPS is just HTTP inside a SSL connection. There is nothing special, i.e. you just need to speak the WebSocket protocol on the SSL stream after the successful SSL handshake.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!