热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在laravel5.8中集成swoole组件----用协程实现的服务端和客户端(nginx配置篇章)

laravel项目中的配置原文出处:https:laravelacademy.orgpost19700.html,感谢原文作者让laravel这款可爱的php框架,进入了高并发的殿堂
  • laravel项目中的配置  原文出处:https://laravelacademy.org/post/19700.html,感谢原文作者让laravel这款可爱的php框架,进入了高并发的殿堂
如果你已经成功安装了laravelS组件,并用他接管了你的laravel项目,那么接下来就是实现一个websocket服务器的类,
类声明在项目app目录下的Services(没有就自己建一个)目录,

也许这些命名空间已经多的把你吓了一跳,但其实他们都源自你安装的laravelS组件,类的实现部分你也许并不感到陌生,没错,
他们就是按照swoole websocket原生类进行了封装
Hhxsv5类空间的源码可以参照 项目空间下的vendor/hhxsv5/laravel-s/src/Swoole/Coroutine

namespace App\Services;

use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
use Illuminate\Support\Facades\Log;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
class mywebsocket implements WebSocketHandlerInterface
{

public function __construct()
{
Log::info('launching laravel-swoole websocket');
}

public function onOpen(Server $server, Request $request)
{
Log::info('launching swoole websocket');
$server->push($request->fd,'欢迎使用,基于swoole的laravel websocket');
}

public function onMessage(Server $server, Frame $frame)
{
$server->push($frame->fd,date('Y-m-dH:i:s').'服务器响应你的请求');
}

public function onClose(Server $server, $fd, $reactorId)
{
Log::info('websocket服务器关闭');
}
}

然后,你就要到config目录下找到laravels.php,进行一番配置。

找到下列关联数组的键名,并作配置

'websocket'                => [ 'enable' => true, 'handler'=>\App\Services\mywebsocket::class,#这个就是上述声明的类 //'handler' => XxxWebSocketHandler::class,
    ],
'swoole' => [ ... // 每隔 60s 检测一次所有连接,如果某个连接在 600s 内都没有发送任何数据,则关闭该连接
    'heartbeat_idle_time'      => 600, 'heartbeat_check_interval' => 60, ... ],
  • nginx配置部分(分站点配置)
如果你对nginx多站点配置还感到陌生,可以随便搜罗一篇博文来看,这里的配置是在分站点目录vhosts下的,针对目标站点的 
.conf文件进行配置的,如果你急于上机,不妨全部拷贝,把域名
更改过来
map $http_upgrade $connection_upgrade #这行代码的目的是把http协议 “提升”到websocket协议的标准
{
default upgrade; '' close; } upstream laravels {#server后面是需要监听的ip地址,如果你使用了docker容器部署你的项目,那server后面跟的是容器名 # Connect IP:Port server yinti.com:5200 weight=5 max_fails=3 fail_timeout=30s; keepalive 16; } server { listen 88;#端口可以根据项目实际需要进行调整 server_name yinti.com; root /dingshub2/yinti/public; index index.html index.htm index.php; location / { # WordPress固定链接URL重写 try_files $uri @laravels; } # PHP配置 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #下面这段是websocket服务器的具体部署实现 location =/ws { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout: Nginx will close the connection if
the proxied server does not send data to Nginx in 60 seconds; # At the same time, this close behavior is also affected by heartbeat setting of Swoole. # proxy_read_timeout 60s; proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header Scheme $scheme; proxy_set_header Server-Protocol $server_protocol; proxy_set_header Server-Name $server_name; proxy_set_header Server-Addr $server_addr; proxy_set_header Server-Port $server_port; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass http://laravels; }    #我只知道下面的配置必须有 location @laravels { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout 60s; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header Scheme $scheme; proxy_set_header Server-Protocol $server_protocol; proxy_set_header Server-Name $server_name; proxy_set_header Server-Addr $server_addr; proxy_set_header Server-Port $server_port; proxy_pass http://laravels; } #我部署的静态资源服务器,你的项目可有可无 location ~ .*\.(gif|jpg|jpeg|png|js|css)$ { expires 24h; root /dingshub2/yinti/public/static; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /dingshub2/yinti/public/static;#图片访问路径 proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; if ( !-e $request_filename) { proxy_pass http://127.0.0.1;#默认80端口 } } }
  • html实现部分(blade模版,继承了母版页)
@extends('masterpage.chatroom') @section('passage') 
class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">

class="display-4">帅哥聊天室

class="lead">四海之内皆兄弟,请文明交流

@endsection @section('pad')
class="card-deck mb-3">
class="card mb-10 box-shadow">
class="card-header">

class="my-0 font-weight-normal text-center">交流板

class="card-body">

class="card-title pricing-card-title"> class="text-muted">

"margin-left: 2px" class="my-0 font-weight-normal">消息输入框


@endsection @section('tailscript') @endsection
  • js具体实现----点击按钮即实现websocket服务器的连接
$("#joinin").click(function() { var mysocket = new WebSocket('ws://yinti.com:5200/ws'); mysocket.onopen=function (event) { $("#chatpad").val(""); $("#chatpad").val("大家好!音提来了\n"); }; });

 

(未完待续)

 


推荐阅读
author-avatar
我还没公主
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有