浅谈workerman安装与配置应用

ps:本次配置是在laravel5.4的基础上演示

1.首先安装workerman

composer require workerman/workerman

2.服务端启动

php artisan make:command Workerman

然后>/app/Console/Commands下面就会创建一个文件Workerman.php

我们改一改signature description 还有handle方法...↓

记得引入workerman

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Workerman\Worker;

class Workerman extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workerman:command {action} {-d}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $ws = new Worker("websocket://0.0.0.0:9011");

        $ws->count = 4;
//        $ws_connection = new Worker("websocket://0.0.0.0:9012");
//        $ws_connection->count = 1;
//        $ws->onConnect = function($connection)
//        {
//
//        };

        $ws->onMessage = function($connection, $data)
        {
            echo $data."\n";
            $connection->send($data);
        };

        $ws->onClose = function($connection)
        {
            echo "Connection closed\n";
        };

// 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题
        $ws->onWorkerStart = function($ws)
        {
            echo "Worker starting...\n";
            // 以websocket协议连接远程websocket服务器
            $ws->onConnect = function($connection){
                // 每10秒执行一次
                $time_interval = 1;
                // 给connection对象临时添加一个timer_id属性保存定时器id
                $connection->timer_id = Timer::add($time_interval, function()use($connection)
                {
                    $connect_time = time();
                    $connection->send($connect_time);
                });
            };
        };

        // Run worker
        Worker::runAll();
    }
}

3.修改/app/Console/Kernel.php

protected $commands = [
    Commands\Workerman::class,
];

4.启动workerman

php artisan workerman:command start d

注:如图为成功!

下面我创建了一个视图,该视图通过js连接 websocket://0.0.0.0:9011 ,然后后台接口是把当前时间时时传给前台页面

视图代码

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

    <!-- Styles -->
    <style>
        html, body {
            background-color: #fff;
            color: #636b6f;
            font-family: 'Raleway', sans-serif;
            font-weight: 100;
            height: 100vh;
            margin: 0;
        }

        .full-height {
            height: 100vh;
        }

        .flex-center {
            align-items: center;
            display: flex;
            justify-content: center;
        }

        .position-ref {
            position: relative;
        }

        .top-right {
            position: absolute;
            right: 10px;
            top: 18px;
        }

        .content {
            text-align: center;
        }

        .title {
            font-size: 84px;
        }

        .links > a {
            color: #636b6f;
            padding: 0 25px;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: .1rem;
            text-decoration: none;
            text-transform: uppercase;
        }

        .m-b-md {
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
<div class="flex-center position-ref full-height">
    <div class="content">
        <input type="text" class="xx">
        <button class="btn">连接sorket</button>
    </div>
</div>
</body>
</html>
<script>
    // $(document).ready(function(){
    //     var ws = new WebSocket("ws://127.0.0.1:9011");
    //
    //     ws.onopen = function(evt) {  //绑定连接事件
    //         // console.log(evt);
    //         ws.send("链接成功");
    //     };
    //
    //     ws.onmessage = function(evt) {//绑定收到消息事件
    //         console.log( "Received Message: " + evt.data);
    //     };
    //
    //     ws.onclose = function(evt) { //绑定关闭或断开连接事件
    //         console.log("Connection closed.");
    //     };
    // });

    $('.btn').click(function () {
        var ws = new WebSocket("ws://127.0.0.1:9011");

        ws.onopen = function(evt) {  //绑定连接事件
            // console.log(evt);
            ws.send("链接成功");
        };

        ws.onmessage = function(evt) {//绑定收到消息事件
            console.log( "Received Message: " + evt.data);
            $('.xx').val(evt.data);
        };

        ws.onclose = function(evt) { //绑定关闭或断开连接事件
            console.log("Connection closed.");
        };
    })

    // $('.hah').click(function () {
    //     var ws = new WebSocket("ws://127.0.0.1:9999");
    //     var text = $('.xx').val()
    //     ws.onopen = function (aa) {
    //         console.log('hkcflhkx')
    //     }
    // })
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值