我正在用php构建一个简单的聊天工具,而websocket是聊天应用程序的最佳方法之一
我完全是websocket的初学者,我为websocket连接编写了一个简单的代码,但出现了错误
WebSocket connection to 'ws://localhost:8080/htdocs/chat.php' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
这是我到目前为止编写的完整代码
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
function showMessage(messageHTML) {
$('#chat-box').append(messageHTML);
}
$(document).ready(function(){
var websocket = new WebSocket("ws://localhost:8080/htdocs/chat.php");
websocket.onopen = function(event) {
showMessage("<div class='chat-connection-ack'>Connection is established!</div>");
}
websocket.onmessage = function(event) {
var Data = JSON.parse(event.data);
showMessage("<div class='"+Data.message_type+"'>"+Data.message+"</div>");
$('#chat-message').val('');
};
websocket.onerror = function(event){
showMessage("<div class='error'>Problem due to some Error</div>");
console.log(event.data);
};
websocket.onclose = function(event){
showMessage("<div class='chat-connection-ack'>Connection Closed</div>");
};
});
</script>
</head>
<body>
<div id="chat-box">
</div>
</body>
</html>
我签入了php.ini文件,websocket没有被注释掉