我正在使用棘轮系统尝试为应用设置Websocket服务器。 每次出现此错误时,我的连接都出现问题:
与'ws://:8080'的WebSocket连接失败:在执行期间出错 WebSocket握手:意外的响应代码:403
这是共享主机的文件夹中托管的代码。我将脚本命名为index.php
是否可以解决此问题?也许我需要检查我的php脚本?
<?php
require __DIR__.'/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
//store the new connection
$this->clients->attach($conn);
echo "someone connected\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
//send the message to all the other clients except the one who sent.
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "someone has disconnected";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>chatapp</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"></script>
</head>
<body>
<style>
.hidden {
display: none;
}
#wrapper {
width: 800px;
margin: 0 auto;
}
#leave-room {
margin-bottom: 10px;
float: right;
}
#user-container {
width: 500px;
margin: 0 auto;
text-align: center;
}
#main-container {
width: 500px;
margin: 0 auto;
}
#messages {
height: 300px;
width: 500px;
border: 1px solid #ccc;
padding: 20px;
text-align: left;
overflow-y: scroll;
}
#msg-container {
padding: 20px;
}
#msg {
width: 400px;
}
.user {
font-weight: bold;
}
.msg {
margin-bottom: 10px;
overflow: hidden;
}
.time {
float: right;
color: #939393;
font-size: 13px;
}
.details {
margin-top: 20px;
}
</style>
<div id="wrapper">
<div id="user-container">
<label for="user">What's your name?</label>
<input type="text" id="user" name="user">
<button type="button" id="join-chat">Join Chat</button>
</div>
<div id="main-container" class="hidden">
<button type="button" id="leave-room">Leave</button>
<div id="messages">
</div>
<div id="msg-container">
<input type="text" id="msg" name="msg">
<button type="button" id="send-msg">Send</button>
</div>
</div>
</div>
<script id="messages-template" type="text/x-handlebars-template">
{{#each messages}}
<div class="msg">
<div class="time">{{time}}</div>
<div class="details">
<span class="user">{{user}}</span>: <span class="text">{{text}}</span>
</div>
</div>
{{/each}}
</script>
<script>
(function(){
var user;
var messages = [];
var messages_template = Handlebars.compile($('#messages-template').html());
function updateMessages(msg){
messages.push(msg);
var messages_html = messages_template({'messages': messages});
$('#messages').html(messages_html);
$("#messages").animate({ scrollTop: $('#messages')[0].scrollHeight}, 1000);
}
var conn = new WebSocket('ws://<myserverip>:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
var msg = JSON.parse(e.data);
updateMessages(msg);
};
$('#join-chat').click(function(){
user = $('#user').val();
$('#user-container').addClass('hidden');
$('#main-container').removeClass('hidden');
var msg = {
'user': user,
'text': user + ' entered the room',
'time': moment().format('hh:mm a')
};
updateMessages(msg);
conn.send(JSON.stringify(msg));
$('#user').val('');
});
$('#send-msg').click(function(){
var text = $('#msg').val();
var msg = {
'user': user,
'text': text,
'time': moment().format('hh:mm a')
};
updateMessages(msg);
conn.send(JSON.stringify(msg));
$('#msg').val('');
});
$('#leave-room').click(function(){
var msg = {
'user': user,
'text': user + ' has left the room',
'time': moment().format('hh:mm a')
};
updateMessages(msg);
conn.send(JSON.stringify(msg));
$('#messages').html('');
messages = [];
$('#main-container').addClass('hidden');
$('#user-container').removeClass('hidden');
conn.close();
});
})(jQuery);
</script>
</body>
</html>