dongzaliang4492 2016-10-25 11: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 11: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.

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

报告相同问题?

悬赏问题

  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部