0
我目前正在使用websocket使用TLS/SSL(wss://)实时通知服务。
我对浏览器和服务器之间的握手有一些问题。一切工作正常与服务器和客户端在PHP中,但当我使用JS的WebSocket连接到服务器,它失败,因为我不知道如何处理服务器端(从浏览器)握手。
到目前为止,我的服务器代码:SSL websocket在php/javascript
$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) . "\n";
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\r\n"
. "Connection: close\r\n"
. "Content-Type: text/html\r\n"
. "\r\n"
. "Hello World!");
fclose($client);*/
} else {
print "error.\n";
}
}
没有什么是陈述有关的RFC WebSocket SSL握手。
如果有人对如何实现握手有一些想法,将不胜感激。
我明白了,谢谢澄清。然而从维基百科,服务器返回'HTTP/1.1 101交换协议 升级:网页套接字 连接:升级 仲丁基的WebSocket - 接受:HSmrc0sMlYUkAGmm5OPpG2HaGWk = 仲丁基网页套接字协议:chat' 此为'WS://'插座,我不应该为'wss://'websocket返回不同的东西吗? –
@AlexandreChambet:不,请求发送的升级是相同的,响应也是一样的。唯一的区别是通过普通的TCP连接进行通信,而通过SSL流进行通信。与HTTP与HTTPS完全相同。 –
谢谢,我会考虑一下。 –