From b374309c1f321ee288040da246822ca042760cf4 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Wed, 17 Jun 2020 20:25:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=E7=9B=B4?= =?UTF-8?q?=E6=92=ADwebsocket=E8=AE=A8=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Web/Controllers/IndexController.php | 8 + app/Http/Web/Controllers/LiveController.php | 90 ++-- .../Web/Controllers/MessengerController.php | 27 ++ app/Http/Web/Controllers/PublicController.php | 6 +- app/Http/Web/Services/Live.php | 67 +++ app/Http/Web/Views/chapter/show_live.volt | 159 +------ app/Http/Web/Views/index/im.volt | 99 +++++ app/Http/Web/Views/index/player.volt | 338 --------------- app/Services/Frontend/Chapter/Learning.php | 4 +- app/Traits/Auth.php | 1 + composer.json | 1 + composer.lock | 409 ++++++++++-------- public/static/web/css/common.css | 5 + public/static/web/js/live.im.js | 96 ++++ public/static/web/js/live.player.js | 104 +++++ websocket/Events.php | 22 +- 16 files changed, 722 insertions(+), 714 deletions(-) create mode 100644 app/Http/Web/Controllers/MessengerController.php create mode 100644 app/Http/Web/Services/Live.php create mode 100644 app/Http/Web/Views/index/im.volt delete mode 100644 app/Http/Web/Views/index/player.volt create mode 100644 public/static/web/js/live.im.js create mode 100644 public/static/web/js/live.player.js diff --git a/app/Http/Web/Controllers/IndexController.php b/app/Http/Web/Controllers/IndexController.php index e3ebb7b3..ab02d7cd 100644 --- a/app/Http/Web/Controllers/IndexController.php +++ b/app/Http/Web/Controllers/IndexController.php @@ -24,4 +24,12 @@ class IndexController extends Controller $this->view->setVar('vip_courses', $indexService->getVipCourses()); } + /** + * @Get("/im", name="web.im") + */ + public function imAction() + { + + } + } diff --git a/app/Http/Web/Controllers/LiveController.php b/app/Http/Web/Controllers/LiveController.php index 18a7fd7a..2c3b8816 100644 --- a/app/Http/Web/Controllers/LiveController.php +++ b/app/Http/Web/Controllers/LiveController.php @@ -2,47 +2,81 @@ namespace App\Http\Web\Controllers; -class LiveController extends Controller +use App\Http\Web\Services\Live as LiveService; +use App\Traits\Response as ResponseTrait; + +/** + * @RoutePrefix("/live") + */ +class LiveController extends \Phalcon\Mvc\Controller { + use ResponseTrait; + /** - * @Get("/stats", name="web.live.stats") + * @Get("/{id:[0-9]+}/members", name="web.live.members") */ - public function statsAction() + public function membersAction($id) + { + $list = [ + [ + 'username' => '直飞机', + 'avatar' => 'http://tp1.sinaimg.cn/5619439268/180/40030060651/1', + 'status' => 'online', + 'sign' => '高舍炮打的准', + 'id' => 1, + ], + [ + 'username' => '直飞机2', + 'avatar' => 'http://tp1.sinaimg.cn/5619439268/180/40030060651/1', + 'status' => 'online', + 'sign' => '高舍炮打的准', + 'id' => 2, + ], + [ + 'username' => '直飞机3', + 'avatar' => 'http://tp1.sinaimg.cn/5619439268/180/40030060651/1', + 'status' => 'online', + 'sign' => '高舍炮打的准', + 'id' => 3, + ], + ]; + + $content = ['data' => ['list' => $list]]; + + return $this->jsonSuccess($content); + } + + /** + * @Post("/{id:[0-9]+}/bind", name="web.live.bind") + */ + public function bindAction($id) + { + $service = new LiveService(); + + $service->bindUser($id); + + return $this->jsonSuccess(); + } + + /** + * @Post("/{id:[0-9]+}/unbind", name="web.live.unbind") + */ + public function unbindAction($id) { } /** - * @Get("/users", name="web.live.users") + * @Post("/{id:[0-9]+}/message", name="web.live.message") */ - public function usersAction() + public function messageAction($id) { + $service = new LiveService(); - } - - /** - * @Post("/login", name="web.live.login") - */ - public function loginAction() - { - - } - - /** - * @Post("/logout", name="web.live.logout") - */ - public function logoutAction() - { - - } - - /** - * @Post("/message", name="web.live.message") - */ - public function messageAction() - { + $service->sendMessage($id); + return $this->jsonSuccess(); } } diff --git a/app/Http/Web/Controllers/MessengerController.php b/app/Http/Web/Controllers/MessengerController.php new file mode 100644 index 00000000..940df23a --- /dev/null +++ b/app/Http/Web/Controllers/MessengerController.php @@ -0,0 +1,27 @@ +siteSeo->setKeywords($this->siteSettings['keywords']); + $this->siteSeo->setDescription($this->siteSettings['description']); + + $indexService = new IndexService(); + + $this->view->setVar('slides', $indexService->getSlides()); + $this->view->setVar('lives', $indexService->getLives()); + $this->view->setVar('new_courses', $indexService->getNewCourses()); + $this->view->setVar('free_courses', $indexService->getFreeCourses()); + $this->view->setVar('vip_courses', $indexService->getVipCourses()); + } + +} diff --git a/app/Http/Web/Controllers/PublicController.php b/app/Http/Web/Controllers/PublicController.php index b02b14f6..435d120d 100644 --- a/app/Http/Web/Controllers/PublicController.php +++ b/app/Http/Web/Controllers/PublicController.php @@ -53,13 +53,13 @@ class PublicController extends \Phalcon\Mvc\Controller } /** - * @Post("/learning", name="web.learning") + * @Post("/{id:[0-9]+}/learning", name="web.learning") */ - public function learningAction() + public function learningAction($id) { $service = new LearningService(); - $service->handle(); + $service->handle($id); return $this->jsonSuccess(); } diff --git a/app/Http/Web/Services/Live.php b/app/Http/Web/Services/Live.php new file mode 100644 index 00000000..a3373c34 --- /dev/null +++ b/app/Http/Web/Services/Live.php @@ -0,0 +1,67 @@ +checkChapterCache($id); + + $user = $this->getCurrentUser(); + + $userId = $user->id > 0 ?: $this->session->getId(); + + $clientId = $this->request->getPost('client_id'); + + $groupName = $this->getGroupName($chapter->id); + + Gateway::$registerAddress = '127.0.0.1:1238'; + + Gateway::bindUid($clientId, $userId); + + Gateway::joinGroup($clientId, $groupName); + } + + public function sendMessage($id) + { + $chapter = $this->checkChapterCache($id); + + $from = $this->request->getPost('from'); + $to = $this->request->getPost('to'); + + $content = [ + 'username' => $from['username'], + 'avatar' => $from['avatar'], + 'content' => $from['content'], + 'fromid' => $from['id'], + 'id' => $to['id'], + 'type' => $to['type'], + 'timestamp' => 1000 * time(), + 'mine' => false, + ]; + + $message = json_encode([ + 'type' => 'show_message', + 'content' => $content, + ]); + + $groupName = $this->getGroupName($chapter->id); + + Gateway::$registerAddress = '127.0.0.1:1238'; + + Gateway::sendToGroup($groupName, $message); + } + + protected function getGroupName($groupId) + { + return "chapter_{$groupId}"; + } + +} diff --git a/app/Http/Web/Views/chapter/show_live.volt b/app/Http/Web/Views/chapter/show_live.volt index 06fea4d5..575f2150 100644 --- a/app/Http/Web/Views/chapter/show_live.volt +++ b/app/Http/Web/Views/chapter/show_live.volt @@ -2,32 +2,28 @@ {% block content %} - {% set course_url = url({'for':'web.course.show','id':chapter.course.id}) %} - -