Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat #1

Merged
merged 8 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/Chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Chat extends Model
{
protected $fillable = ['user', 'post'];
}
82 changes: 82 additions & 0 deletions app/Http/Controllers/Api/ChatController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers\Api;

use App\Chat;
use Carbon\Carbon;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ChatController extends Controller
{
/**
* チャット保存
*
* @param Request $request
* @return JsonResponse
*/
public function add(Request $request): JsonResponse
{
$chat = Chat::make([
'user' => $request->get('user'),
'post' => $request->get('post'),
]);
$chat->save();
Redis::set('latest_created_at', $chat->created_at->toDateTimeString());
return response()->json(['result' => 'ok']);
}

/**
* 入力中の人の情報を保存
*
* @param Request $request
* @return JsonResponse
*/
public function typing(Request $request): JsonResponse
{
Redis::sadd('typing_users', $request->get('user'));
return response()->json(['result' => 'ok']);
}

/**
* チャット一覧取得
*
* @return StreamedResponse
*/
public function event(): StreamedResponse
{
// 最近のチャット5件と、一番最近のcreated_atを取得
$chats = Chat::orderBy('created_at', 'desc')->limit(5)->get()->sortBy('created_at')->values();
$tmpLatestCreatedAt = optional($chats->last())->created_at ?? Carbon::minValue();
$response = new StreamedResponse(function() use ($chats, $tmpLatestCreatedAt) {
printf("data: %s\n\n", json_encode(['posts' => $chats]));
ob_flush();
flush();
while(true) {
$latestCreatedAt = is_null(Redis::get('latest_created_at')) ? Carbon::minValue() : Carbon::parse(Redis::get('latest_created_at'));
$typingUsers = Redis::smembers('typing_users');
if ($latestCreatedAt->gt($tmpLatestCreatedAt)) {
// チャットに更新があった場合はテーブルから取得
$latestChats = Chat::where('created_at', '>', $tmpLatestCreatedAt)->orderBy('created_at', 'asc')->get();
$tmpLatestCreatedAt = $latestCreatedAt;
}
echo 'data: ' . json_encode(['posts' => $latestChats ?? [], 'typing_users' => $typingUsers]) . "\n\n";
ob_flush();
flush();

$latestChats = null;
Redis::del('typing_users');

sleep(1);
}
});
$response->headers->set('Content-Type', 'text/event-stream');
$response->headers->set('X-Accel-Buffering', 'no');
$response->headers->set('Cach-Control', 'no-cache');
return $response;
}
}
19 changes: 19 additions & 0 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use Illuminate\View\View;
use Illuminate\Http\Request;

class ChatController extends Controller
{
/**
* チャット画面表示
*
* @return View
*/
public function index(): View
{
return view('chat');
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"laravel/ui": "^1.1",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
Expand Down
59 changes: 58 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions database/migrations/2019_12_14_131034_create_chat_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateChatTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('chats', function (Blueprint $table) {
$table->increments('id');
$table->string('user');
$table->string('post');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('chats');
}
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
},
"devDependencies": {
"axios": "^0.19",
"bootstrap": "^4.0.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.13",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0"
"sass": "^1.20.1",
"sass-loader": "7.*",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
}
}
Loading