douewei1665 2018-05-29 10:37
浏览 240

Web套接字在javascript中无法使用SSL和WSS

I have implemented web socket using below code,

try {
        console.log('wss://' + hostname + ':' + port + endpoint);
        webSocket = new WebSocket(webSocketURL);
        webSocket.onmessage = function (event) {
            //console.log('send message successfully.');
            var obj = JSON.parse(event.data);
            append_data(obj, market_pair);
        };
    } catch (exception) {
        console.log('Exception handle!');
        console.error(exception);
    }

My page is https supported so I am using wss protocol. but issue is it gives me error that "Firefox can’t establish a connection to the server at wss://domainname.com:4008/order.php" if I will load the page using simple http and use ws in websocket then it is working fine but with wss it shows above error. same way in google chrome it will also not able to connect. My ssl certificate is installed correctly in server and my hosting and domain provided in godaddy and they told me that certificate is installed correctly.

I am running server side socket in php and it is working fine just now able to connect to that socket port using wss://

I have found that I am not able to do handshak using my php code because my php file is running in background and the content I am getting by socket read is encrypted.

function doHandshake($received_header, $client_socket_resource, $host_name, $port) {
    echo $received_header;
    $headers = array();
    $lines = preg_split("/
/", $received_header);
    foreach ($lines as $line) {
        $line = chop($line);
        if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
            $headers[$matches[1]] = $matches[2];
        }
    }

    $secKey = $headers['Sec-WebSocket-Key'];
    echo $headers['Sec-WebSocket-Key']; 
    $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
    //$secAccept = base64_encode(sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'));
    $buffer = "HTTP/1.1 101 Web Socket Protocol Handshake
" .
            "Upgrade: websocket
" .
            "Connection: Upgrade
" .
            "WebSocket-Origin: $host_name
" .
            "WebSocket-Location: wss://$host_name:$port/websocket/btc_boon_pair.php
" .
            "Sec-WebSocket-Accept:$secAccept

";
    socket_write($client_socket_resource, $buffer, strlen($buffer));
}

this is my handshak function $headers['Sec-WebSocket-Key'] is undefined because $received_header is encrypted.

can anyone suggest the solution of this issue? I have to run php file as daemon and want to connect using wss protocol.

  • 写回答

1条回答 默认 最新

  • doujiao1949 2018-06-02 08:39
    关注

    Ok finally I solved the issue.

    due to the SSL encryption it is not able to read socket in my php daemon file. So I get solution from below answer https://serverfault.com/questions/804862/apache-mod-proxy-forward-secure-websocket-to-non-secure

    we can use proxy pass in apache so it will send all request with /websocket to the other ws://host:port/ after this it is working perfectly.

    Also make sure that domain name is not using proxy because due to that my above solution is not worked but After removing proxy from DNS settings it started working.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?