Laravel和Gateway Worker的使用+SSL WSS访问 推送消息

功能要求:数据有变化时,更新所有访问者的页面数据。针对每个登录用户,返回不同的用户数据
不同数据:每个用户不同的竞买号,当前登录用户是否交了保证金,是否已经出价,是否可以优先购买,未登录用户不显示相关数据
文档 https://www.workerman.net/doc

已有laravel 配置Gateway Worker

去这个地址把demo下载下来 http://doc2.workerman.net/ 本站使用linux环境
我是先下载放到项目用git提交到服务器的
Applications下的YourApp我直接复制到:laravel项目文件\app下
start.php 复制到laravel项目文件下
如图:
这个是放置位置,大家可以按照自己心意放
该按照自己的实际需求配置信息了
记得服务器的端口是否打开
防火墙是否限制了此端口
都会影响对接

开始配置

首先配置位置:app\YourApp\start_gateway.php
在这里插入图片描述

在这里插入图片描述
到这里 代码的修改就暂停一下

开始配置依赖的扩展

执行
composer require workerman/gateway-worker OR(或者)
composer update workerman/gateway-worker

因为我的用到了推送消息,所以需要gatewayclient (往下看会说到)
composer require workerman/gatewayclient OR(或者)
composer update workerman/gatewayclient

如果你还需要用mysql
composer require workerman/mysql OR(或者)
执行 composer update workerman/mysql

redis建议可以不用workerman/redis 用的话可以 composer update workerman/redis

(json 没有用require 已经有了用 update)
配置完成的composer.json

需要的扩展配置完成

完成我的业务逻辑 发送消息

打开这个文件:app\YourApp\Events.php
在这里插入图片描述
app\YourApp\Events.php 当客户端发来消息时触发onMessage
(相关文字写在图上了 有想法可留言)
在这里插入图片描述

app\YourApp\start_gateway.php里定义了常量用来存储redis

const WEB_GID_REDIS = 'gid_redis_'; //用作存所有访问产品id(gid)的$client_id redis集合
const WEB_DEL_CLIENT = 'del_client_'; //用来存 $client_id 对应的git redis字符串
const WEB_TOKEN_CLIENT = 'token_client_';//用来存每个$client_id 对应的token

app\YourApp\Events.php 存对应关系和发送消息

//$message 前端传来的数据
public static function onMessage($client_id, $message)
{
	$arr = json_decode($message,true);
       $id = intval($arr['id']);//产品id 从$message里获取
       $tokenl = trim($arr['token']);//会员信息
       if(empty($tokenl)){
           $tokenl = '';
       }

	$goodinfo = self::getgoodinfo($id);//自己写的方法 获取产品公共的产品信息


	//存redis
	self::$man_redis->set(WEB_DEL_CLIENT.$client_id,$id);//用作删除或查找gid
	  if(!empty($tokenl)){
		self::$man_redis->set(WEB_TOKEN_CLIENT.$client_id,$tokenl);//用作查找token
	   }
	self::$man_redis->sAdd(WEB_GID_REDIS.$id,$client_id);//用作发送消息 存集合


	//获取redis信息发送消息
	$client_ids = self::$man_redis->sMembers(WEB_GID_REDIS.$id);
	foreach ($client_ids as $cl_id){
	   $token = self::$man_redis->get(WEB_TOKEN_CLIENT.$cl_id);//获取到$client_id对应的token(会员信息)
	   $jsonstr = self::user_goodinfo($goodinfo,$token);//自己写的方法 用来查找会员相关的产品信息
	   Gateway::sendToClient($cl_id,$jsonstr);//给每个$client_id 发消息
	}

}

	/**
    * 当用户断开连接时触发 删除redis
    * @param int $client_id 连接id
    */
   public static function onClose($client_id)
   {
       //Gateway::sendToClient($client_id, "Connection closed"."\r\n");
       $gid = self::$man_redis->get(WEB_DEL_CLIENT.$client_id);//用作删除 查找gid
       self::$man_redis->sRem(WEB_GID_REDIS.$gid,$client_id);
       self::$man_redis->del(WEB_TOKEN_CLIENT.$client_id);//删除存储的token
       self::$man_redis->del(WEB_DEL_CLIENT.$client_id);//删除存储的gid
   }

以上是前端访问,服务器给前端发送消息

以下是服务器给建立连接的前端发送消息

新建一个公共文件 我的写在app\Models\Gatewaysend.php
引用/gatewayclient/Gateway.php

namespace App\Models;
use GatewayClient\Gateway;
require_once __DIR__ . '/../../vendor/workerman/gatewayclient/Gateway.php';
Gateway::$registerAddress = '127.0.0.1:1238';

class Gatewaysend extends Model
{
    public function sendToClient($cl_id,$jsonstr){
        Gateway::sendToClient($cl_id,$jsonstr);//给每个$client_id发送消息
    }
    //获取产品信息
    public function send_bid_goodinfo($id)
    {
        $arr = Goods::find($id);
        return $arr;
    }
    //获取会员和产品相关信息
    public function send_user_goodinfo($arr,$token='')
    {
    	$result = $arr;
    	//补充要查找的会员相关信息 省略
        return json_encode($result);
    }
 

}

打开需要触发服务器推送的方法(即执行这个方法 和服务器建立连接的前端收到消息)
加入以下代码
记得use App\Models\Gatewaysend;

			$gid = '产品id';
			
			//workman通知出价
            $rv = new Gatewaysend();
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379);
            $client_ids = $redis->sMembers('gid_redis_'.$gid);
            $arr = $rv->send_bid_goodinfo($gid);
            foreach ($client_ids as $cl_id){
                $token = $redis->get('token_client_'.$cl_id);
                $jsonstr = $rv->send_user_goodinfo($arr,$token);
                $rv->sendToClient($cl_id,$jsonstr);
            }

到此代码逻辑写完了 该开启服务和调用了
到项目目录执行
php start.php start
守护进程可以加 -d : php start.php start -d
关闭 php start.php stop

前端调用代码

有证书用wss
没有证书用ws

<script type="text/javascript">
    WebSocketTest();
    function WebSocketTest()
    {
        if ("WebSocket" in window)
        {
            var osn = '{"id":56,"token":"547acd5608555551113d63a006dc79a"}';
            var ws = new WebSocket("wss://你的ip或域名:8282");
            ws.onopen = function()
            {
                ws.send(osn);
            };
            ws.onerror = function(event){
                console.log(event);
            };
            ws.onmessage = function (evt)
            {
                console.log(evt.data);
                /*setTimeout(function(){
                    ws.send(osn);
                },5000);*/
            };
        }
        else
        {
            alert("您的浏览器不支持 WebSocket!请支付后手动查看订单页面");
        }
    }
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值