如何在php socket中建立websocket安全连接

时间:2016-01-27 14:46:11

标签: php ssl websocket

我正在使用WebSockets创建一个聊天室,当我使用未加密的连接时它正常工作,但在我使用证书之后它不再起作用了。

在JavaScript中,我正在select STR_TO_DATE('10/01/2016 00:00', '%d/%m/%Y %h:%i'); 打开与WebSocket服务器的连接,如下所示:

serverr.php

现在我知道我必须使用var wsUri = "ws://mydomain.com:9002/chat/serverr.php"; websocket1 = new WebSocket(wsUri); 代替wss://所以现在我正在使用:

ws://

但是我收到以下错误:

  

与'wss://mydomain.com:9002 / chat / serverr.php'的WebSocket连接失败:连接建立错误:net :: ERR_SSL_PROTOCOL_ERROR

但我不知道如何建立var wsUri = "wss://mydomain.com:9002/chat/serverr.php"; websocket1 = new WebSocket(wsUri); 连接。我尝试使用以下部分修改我的代码:

wss://

set_time_limit(0); require_once($_SERVER['DOCUMENT_ROOT']."../../../home/username/public_html/config/config.php"); $host = 'ssl://0.0.0.0'; //host $port = '9002'; //port $null = NULL; //null var // Generate certificate $privkey = openssl_pkey_new(); $cert = openssl_csr_new($dn, $privkey); $cert = openssl_csr_sign($cert, null, $privkey, 365); // Generate PEM file # Optionally change the passphrase from 'comet' to whatever you want, or leave it empty for no passphrase $pem_passphrase = 'comet'; $pem = array(); openssl_x509_export($cert, $pem[0]); openssl_pkey_export($privkey, $pem[1], $pem_passphrase); $pem = implode($pem); // Save PEM file $pemfile = 'server.pem'; file_put_contents($pemfile, $pem); //Create TCP/IP sream socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //reuseable port socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); socket_set_option($socket, SOL_SOCKET, 'ssl', 'local_cert', $pemfile); socket_set_option($socket, SOL_SOCKET, 'ssl', 'passphrase', $pem_passphrase); socket_set_option($socket, SOL_SOCKET, 'ssl', 'allow_self_signed', true); socket_set_option($socket, SOL_SOCKET, 'ssl', 'verify_peer', false); //bind socket to specified host socket_bind($socket, 0, $port); //the rest of code is still unmodified is like in the page serverr.php

中的代码
serverr.php

0 个答案:

没有答案