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

  • 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
<?php
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:Portserver 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')<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center"><h1 class="display-4">帅哥聊天室</h1><p class="lead">四海之内皆兄弟,请文明交流</p></div>
@endsection@section('pad')<div class="card-deck mb-3"><div class="card mb-10 box-shadow"><div class="card-header"><h4 class="my-0 font-weight-normal  text-center">交流板</h4></div><div class="card-body"><h1 class="card-title pricing-card-title"> <small class="text-muted"></small></h1><textarea id="chatpad" class='form-control' cols="90" rows="20"></textarea><p style="margin-left: 2px" class="my-0 font-weight-normal">消息输入框</p><textarea class='form-control'  cols="90" rows="8"></textarea><br><button id="joinin" type="button" class="btn btn-lg btn-block btn-outline-primary">进入聊天室</button></div></div></div>
@endsection@section('tailscript')<script src="{{asset(config('mystatic._js_').'/jquery-3.3.1.min.js')}}" ></script><script src="{{asset(config('mystatic._myjs_').'/joinintalk.js')}}"></script>
@endsection
  • js具体实现----点击按钮即实现websocket服务器的连接
$("#joinin").click(function()
{var mysocket = new WebSocket('ws://yinti.com:5200/ws');mysocket.onopen=function (event) {$("#chatpad").val("");$("#chatpad").val("大家好!音提来了\n");};});

 

(未完待续)

 

转载于:https://www.cnblogs.com/saintdingspage/p/11272418.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/410350.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[html] 如何判断用户正在操作页面?当页面一个小时没有操作时跳转到指定页面如何做?

[html] 如何判断用户正在操作页面&#xff1f;当页面一个小时没有操作时跳转到指定页面如何做&#xff1f; 监听mousemove事件。当一段时间没有触发mousemove时&#xff0c;即认定用户没有操作页面个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&am…

Html5---可编辑的Contenteditable

Html5的新元素&#xff0c;使得表格的每个文本都是在网页中可编辑的&#xff0c;效果很好&#xff0c;很实用&#xff01; Chinese Name:moumouBirthday:1931.01.13English Name:momoCareer:SingerOther Name:testRepresentative works:Mr.Nationality:KoreaTeam:KARANation:Ko…

imagex使用方法_Microsoft OneDrive 的使用心得,真香

一、OneDrive 账号1. 免费用户只有5GB&#xff0c;这显然是不够的&#xff1b;2. 可以通过购买 Office 365 获得 1TB 空间&#xff1b;3. 可以通过教育邮箱获得免费 3-5TB 空间&#xff0c;这个是真香的方法&#xff0c;上大学期间现在学校基本都给学生edu邮箱&#xff0c;可以…

Refactoring

What Refacing&#xff1a; 重构&#xff08;Refactoring&#xff09;就是在不改变软件现有功能的基础上&#xff0c;通过调整程序代码改善软件的质量、性能&#xff0c;使其程序的设计模式和架构更趋合理&#xff0c;提高软件的扩展性和维护性。 经典的Refacing Definition&am…

编程之美-2.17-数组循环移位

1. 简述 设计一个算法&#xff0c;把一个含有N个元素的数组循环右移K位&#xff0c;要求时间复杂度为O(N)&#xff0c;且只允许使用两个附加变量。 2. 思路 其实看到题目中只允许两个附加变量应该也能想到&#xff0c;就是两个变量倒来倒去就行了。如果熟悉希尔排序的话&#x…

[html] 如何给页面添加追加右键菜单(原右键菜单功能保持不变)

[html] 如何给页面添加追加右键菜单&#xff08;原右键菜单功能保持不变&#xff09; 监听oncontextmenu&#xff0c;在系统右键菜单下追加自定义菜单个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 …

python写入excel特定区域_Python读取Excel中符合特定条件的数据,并写入新的表格中...

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循CC 4.0 BY-SA版权协议&#xff0c;转载请附上原文出处链接和本声明。 原始表格 代码#!/usr/bin/env python # -*- coding: utf-8 -*- # Time : 2019/3/20 21:24 # Author : cunyu # Site : cunyu1943.github.io # File …

swoole 协程channel乱测

channel和数组差不多&#xff0c;可以被用作队列&#xff0c;属性capacity是设置容量&#xff0c;isEmpty() isFull() 用来判断队列是空还是满&#xff0c;push()加入队列 pop()弹出队列 interface pusher {function push($data); } #require redisconn.php; class mypusher im…

Rails 3:提高Ajax应用速度

Rails 3&#xff1a;提高Ajax应用速度 http://developer.51cto.com 2011-05-18 09:45 Stefan Siebel 51CTO 我要评论(0)我建立了一个列表网站&#xff0c;ListKungFo&#xff0c;其中大量使用了Ajax&#xff0c;目前为止网站运行非常良好。而过去两周里&#xff0c;我一直在…

[html] websocket和socket有什么区别?

[html] websocket和socket有什么区别&#xff1f; 1.Socket 是传输控制层的接口。用户可以通过 Socket 来操作底层 TCP/IP 协议族通信。 2.WebSocket 是一个完整应用层协议。 3.Socket 更灵活&#xff0c;WebSocket 更易用。 4.两者都能做即时通讯个人简介 我是歌谣&#xff…

opencv支持python3吗_Python3.4+opencv3

1、安装Python 3.4 for Windows 好的这好像没有什么可以说的 2、下载OpenCV 3和Numpy(OpenCV依赖Numpy库) 大家在这里就出了问题。如果使用直接使用pip install pyopencv安装一定会出错。这里使用了Python界活雷锋封装的安装包&#xff0c;大家根据自己的系统下载&#xff1a; …

Python之浅谈运算符

目录 格式化输出的三种方式基本运算符流程控制格式化输出的三种方式 运算符的语义取决于其操作数的类型 第一种格式化方式&#xff08;3.0&#xff09; name Tim height 170 weight 120 print(My name is:%s,my height is:%s,my weight is:%s%{name,height,weight})第二种格…

[html] websocket和http有什么区别?

[html] websocket和http有什么区别&#xff1f; WebSocket是双向的&#xff0c;在客户端-服务器通信的场景中使用的全双工协议&#xff0c;与HTTP不同&#xff0c;它以ws://或wss://开头。 HTTP是单向的&#xff0c;客户端发送请求&#xff0c;服务器发送响应。个人简介 我是…

Socketpair 简介

socketpair - a slight generalization of pipes used for two-way stream communication. 以下内容来自&#xff1a;匠意雕码 http://my.oschina.net/zengsai/blog/12583 今天用了一个从没玩过的socket函数socketpair()&#xff0c;它的作用是在进程内创建一对sock连接。乍一看…

cacti安装配置详解_MySQL实战001:8.0免安装版服务配置详解

首先我们需要先下载MySQL的安装包&#xff0c;MYSQL官方下载地址&#xff1a;https://dev.mysql.com/downloads/mysql/&#xff0c;这里我下载的是MySQL Community Server 8.0.15版本&#xff0c;以前MySQL还有安装版&#xff0c;现在都是压缩版&#xff0c;解压之后将环境配置…

HttpServletRequest简述

HttpServletRequest简述 HttpServletRequest httpServletRequest (HttpServletRequest) request;/*getRequestURL方法返回客户端发出请求时的完整URL。getRequestURI方法返回请求行中的资源名部分。getQueryString 方法返回请求行中的参数部分。getRemoteAddr方法返回发出请求…

[html] 图片上传时实现本地预览功能的原理是什么?

[html] 图片上传时实现本地预览功能的原理是什么&#xff1f; 通过HTML5 File API读取用户上传的图片&#xff0c;生成一个image对象显示到页面个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录…

推荐两个检索和分类小工具Carrot2 OSS

Carrot2 Carrot2是一个开源搜索结果分类引擎。它能够自动把搜索结果组织成一些专题分类。Carrot2提供的一个架构能够从各种搜索引擎&#xff08;YahooAPI、GoogleAPI、MSN Search API、eTools Meta Search、Alexa Web Search、PubMed、OpenSearch、Lucene index、SOLR&#xff…

Eclipse最常用快捷键

Alt← 前一个编辑的页面 Alt→ 下一个编辑的页面 AltEnter 显示当前选择文件的属性 CtrlF11运行Run As CtrlM 当前窗口最大化 CtrlF7 视窗口切换 CtrlF8 模式切换 CtrlQ 定位到最后编辑的地方 CtrlL 定位在具体某行 CtrlK 选中的Word快速定位到下一个 ctrl.及ctrl1 当某行出错时…

[html] input如何在各个浏览器下保持UI统一?

[html] input如何在各个浏览器下保持UI统一&#xff1f; .ibutton { padding: 3px 15px; *padding: 0 15px; *height: 24px; font-size: 12px; text-align: center; text-shadow: #CF510B 0 1px 0; border:1px solid #ec5c0d; border-radius: 2px; background: #FC750A; backg…