我写了一个小的websockets聊天,php部分只有2个文件,server.php
和Chat.php
,它们都在bin
文件夹中,依赖于ratchet
以及我通过作曲家下载到laravel装置的其他一些库。
server.php
require __DIR__.'/../vendor/autoload.php';
require 'Chat.php';
use Ratchet\Server\IoServer;
use Ratchet\http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080);
$server->run();
Chat.php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
foreach ($this->clients as $client)
{
if ($client !== $conn ) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'the following error occured: ' . $e->getMessage();
$conn->close();
}
}
现在,我在laravel根目录中有bin
个文件夹,所以我能够启动服务器,因为server.php
正在寻找供应商一级的依赖关系,但是我想做什么使用这些文件中的所有laravel好东西,特别是在Chat.php
。
所以现在例如如果我在use DB
中写Chat.php
它会出错(我明白,它无法知道laravel),所以我的问题是如何包含这个bin文件夹和它的文件,以便我可以使用其中的所有laravel好东西?
答案 0 :(得分:0)
您无需手动加载vendor / autoload.php,因为laravel会为您执行此操作。
首先,您必须在YourLaravelRoot / app目录中创建文件夹(我们将其命名为Services)。然后将chat.php移动到其中,将其重命名为ChatService.php(将类名更改为ChatService)或任何适当的名称(以xxxxService结尾,以便更容易识别)并将其命名为from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def your_view(request):
if request.method == "POST":
# do something
return HttpResponse("Your response")
(假设为您的应用程序名称是App).NameSpacing正确非常重要,否则您必须通过composer.json手动加载它。然后创建一个artisan命令并将server.php的内容移动到handle方法内部命令(让我们将其命名为ServerCommand.php)。添加namespace App\Services;
。在app / console上的Kernal.php中注册命令就是这样。现在,您应该能够访问ChatService中的任何laravel外观
要点:
YourLaravelProject
-app
- 控制台
Kernal.php
use App\Services\ChatService as Chat;
---命令
---- ServerCommand.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\ServerCommand::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
}
- 服务
--- ChatService.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Services\ChatService as Chat;
class ServerCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'server:run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080);
$server->run();
}
}
执行命令<?php
namespace App\Services;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
/**
*
*/
class ChatService implements MessageComponentInterface {
{
protected $clients;
function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
foreach ($this->clients as $client)
{
if ($client !== $conn ) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'the following error occured: ' . $e->getMessage();
$conn->close();
}
}