刚开始使用棘轮。当我第一次测试它时,它可以正常使用websockets,获得请求等。但是现在我和我一样做了,但是请求(onOpen)甚至没有得到请求,我得到了这个JS错误:
WebSocket connection to 'ws://localhost:8080/' failed: Error during WebSocket handshake: Unexpected response code: 404
jscode:
var conn = new WebSocket("ws://localhost:8080");
conn.onopen = function(e) {
console.log("Connected!");
conn.send("hello server");
};
conn.onmessage = function (e) {
console.log(e.data);
};
我的源代码:
主档案:
require __DIR__ . "../../library/loader.php";
require __DIR__ . "../../vendor/autoload.php";
error_reporting(E_ALL);
use Ratchet\WebSocket\WsServer,
Ratchet\Http\HttpServer,
Ratchet\Server\IoServer;
use bin\library\AsynChatServer;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new AsynChatServer()
)), 8080);
$server->run();
服务器类:
namespace bin\library;
use Ratchet\MessageComponentInterface,
Ratchet\ConnectionInterface,
Ratchet\Server\IoConnection;
use bin\library\database\Db;
use bin\library\model\Client,
bin\library\model\PacketManager;
class AsynChatServer implements MessageComponentInterface {
private $clients;
/** @var Db */
private $database;
/** @var PacketManager */
private $packets;
function __construct()
{
$this->clients = array();
$this->database = new Db();
$this->packets = new PacketManager($this);
AsynChatServer::println("Server has started up.");
}
/**
* When a new connection is opened it will be passed to this method
* @param ConnectionInterface $conn The socket/connection that just connected to your application
* @throws \Exception
*/
function onOpen(ConnectionInterface $conn)
{
echo "request";
/** @var $client Client */
$client = new Client($conn);
$this->clients[$conn->resourceId] = $client;
AsynChatServer::println("New client accepted: " + $client->getClient()->getRemoteAddress());
}
/**
* This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed.
* @param ConnectionInterface $conn The socket/connection that is closing/closed
* @throws \Exception
*/
function onClose(ConnectionInterface $conn)
{
// TODO: Implement onClose() method.
}
/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
* the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
* @param ConnectionInterface $conn
* @param \Exception $e
* @throws \Exception
*/
function onError(ConnectionInterface $conn, \Exception $e)
{
// TODO: Implement onError() method.
}
/**
* Triggered when a client sends data through the socket
* @param \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
* @param string $msg The message received
* @throws \Exception
*/
function onMessage(ConnectionInterface $from, $msg)
{
/** @var $client Client */
$client = $this->clients[$from->resourceId];
AsynChatServer::println("Paccket received from: " . $client->getClient()->getRemoteAddress());
$this->packets->processPacket($client, $msg);
}
public static function println($message) {
echo $message . "\n";
}
public function getDB() {
return $this->database;
}
}
有什么问题吗?库可能已损坏,所以我需要重新设置它吗? 这是我跑步时得到的输出:
c:\xampp1\php>php c:/xampp1/htdocs/AsynChat/bin/AsynChat.php
Server has started up.
连接时没有别的,没有信号错误。
有什么问题?
NETSTAT -A:
TCP 127.0.0.1:8080 Ben-╧╩:0 LISTENING
TCP 127.0.0.1:8080 Ben-╧╩:0 LISTENING
我的App.php构造函数:
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) {