【发布时间】:2020-02-02 14:19:30
【问题描述】:
我尝试运行我的 websocket 服务器,但由于某种我无法弄清楚的原因它无法运行。 我用过这个教程:http://socketo.me/docs/hello-world
我有这个主文件夹: enter image description here
composer.js:
{
"autoload": {
"psr-4": {
"dealspace_websocket\\": "websocket_src"
}
},
"require": {
"cboden/ratchet": "^0.4.2"
}
}
websocket_src文件夹内只有1个文件,Chat.php:
<?php
namespace dealspace_websocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once __DIR__ . "/../Model/DBConnection.php";
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
$JSonMsg = json_decode($msg);
if($JSonMsg['type'] === 'updownvote') {
$connection = new DBConnection();
$allowUpdate = false;
try {
$sql = 'UPDATE `deals`
SET `votes_counter` = :vc
WHERE `id` = :id';
$stmt = $connection->dbh->prepare($sql);
$allowUpdate = $stmt->execute(array(
'vc' => $JSonMsg['data']['votes'],
'id' => $JSonMsg['data']['dealid']
));
if($allowUpdate !== false) {
$dataOBJ->dealid = $JSonMsg['data']['dealid'];
$dataOBJ->votes = $JSonMsg['data']['votes'];
$returnMsg->type = 'updownvote';
$returnMsg->date = $dataOBJ;
foreach($this->clients as $client) {
$client->send(json_encode($returnMsg));
}
}
} catch(PDOException $e) {}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
最后一个文件,websocket_server.php:
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use dealspace_websocket\Chat;
require __DIR__ . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
?>
然后,当我在主文件夹中打开 CMD 时,我运行:php websocket_server.php
这是结果: enter image description here
这是为什么呢?
【问题讨论】: