我正在尝试在客户端“Web浏览器”和在Windows 2008 R2上运行的Apache 2.4 / PHP 5.6服务器之间实现WebSocket通信。
我的应用程序假设在此URL“HTTPS”上运行
https://myApllication.example.com
我不确定在此之前是否需要在服务器上安装某些内容。
到目前为止我的代码看起来像这样
$(function(){
if(!("WebSocket" in window)){
console.log('your browser does not support WebSocket');
} else {
connect();
$('#disconnect').click(function(){
socket.close();
});
}
function connect(){
var socket;
var host = "ws://myApplication.example.com:8000/dev/icws/poll2.php";
try{
var socket = new WebSocket(host, {
protocolVersion: 13,
origin: 'https://www.example.com'
});
message('Socket Status: '+socket.readyState);
socket.onopen = function(){
message('Socket Status: '+socket.readyState+' (open)');
}
socket.onmessage = function(msg){
message('Received: ' + msg.data);
}
socket.onclose = function(){
message('Socket Status: '+ socket.readyState + ' (Closed)');
}
} catch(exception){
message(exception);
}
} //End connect()
function message(msg){
console.log(msg);
}//End message()
});
此代码会在控制台中显示此错误。
DOMException [SecurityError: "The operation is insecure."
code: 18
nsresult: 0x80530012
以下是我的poll2.php脚本的样子
<?php
header("Content-Type: text/event-stream" . PHP_EOL);
header("Cache-Control: no-cache" . PHP_EOL);
echo "data: {mykey: 'myvalue', eventName: 'eventName'}" . PHP_EOL . PHP_EOL;
?>
**已编辑** 从
更改主机后var host = "ws://myApplication.example.com:8000/dev/icws/poll2.php";
到
var host = "wss://myApplication.example.com:8000/dev/icws/poll2.php";
我不再收到安全错误,但我在控制台中得到了这个错误。
Socket Status: 0 (ready)
Socket Status: 3 (Closed)
Firefox can't establish a connection to the server at wss://myApplication.example.com:8000/dev/icws/poll2.php
什么可能导致连接无法进行? 我是否必须在服务器上打开端口8000?