rockstarcode/streamline

实时应用程序的免费、开源替代品

1.0 2016-09-30 20:18 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:42:02 UTC


README

Build Status

RockstarCode StreamLine(之前为82rules/streamline)

第三方推送服务和实时库的简单替代品。

要求

* PHP ^5.6
* Redis
* Apache/Nginx

功能

此PHP库通过Redis Pub/Sub作为分发引擎,为创建实时应用程序提供了一个简单的接口。

它允许用户之间的通信流 -> 用户和服务器 -> 用户(们),支持以下功能

* Message Notifications
* Chat
* Status Notifications
* Dynamic Content Push
* Dead simple integration
* Long Polling Javascript included
* Auto reconnects on dropped connections
* No need for external services

快速开始

更多详细信息请参阅Wiki

composer require rockstarcode/streamline

// http://www.mysite.com/subscribe.php?channel=test

<?PHP
require('vendor/autoload.php');
use RockstarCode\Streamline\Subscribe;
$sub = new Subscribe($_GET['channel]);
$sub->stream();

// [POST] http://www.mysite.com/publish.php?channel=test
<?PHP
require('vendor/autoload.php');
use RockstarCode\Streamline\Publish;
$pub = new Publish($_GET['channel]);
$pub->send($_POST['message']);


// http://www.mysite.com/chat
<html>
<head>
    <script src="http://www.mysite.com/streamline.js"></script>
    <script>

         window.onload = function(){
             window.StreamLine.subscribe("http://www.mysite.com/subscribe.php?channel=test", function(data){
                    // var data = JSON.parse(json); if data was json
                    document.getElementById("chat").innerHTML += data;
                 }
             });
         };
        function sendMessage(event, item){
            if (event.keyCode == 13) {
                var data = [];
                data.push('message='+item.value);
                StreamLine.publish('http://www.mysite.com/publish.php?channel=test',data.join("&"))
                item.value = '';
            }
        }
    </script>
</head>
<body>
    <input type="text" style="width:100%" placeholder="Send Message [press enter]" onkeyup="sendMessage(event, this)">
    <p>Messages: </p>
    <div id="chat"></div>
</body>
</html>

Laravel

// routes file

Route::get('/subscribe/{channel}',function($channel){
    use RockstarCode\StreamLine\Subscribe;

    $subscribe = new Subscribe($channel, function(){
        /// optional custom connection

        $client = new Redis();
        $client->connect('127.0.0.1');
        return $client;
    });

    $subscribe->stream(function($message){
        /// function that handles each message as it's received in the channel
        echo json_encode(['status'=>true,'message'=>$message]);
    });
});

Route::post('/subscribe/{channel}',function($channel, Request $request){
    use RockstarCode\StreamLine\Publish;

    $pub = new Publish($channel);

    $subscribe->send($request->only('message'));
});

包含示例

在此存储库中,您可以在 ./example 中找到一个非常简单的聊天示例。

    ./example
        - .htaccess
        - frontend.php
        - index.php

示例假设您使用apache或nginx,并配置一个虚拟主机,其documentroot指向示例路径。

    <VirtualHost *:80>
            ServerName myexample.app (or whichever domain you choose)
            DocumentRoot /path/to/streamline/example
    </VirtualHost>

在两个不同的浏览器标签页中简单地导航到 http:///,您应该在每个标签页中都能输入一个名称,并在各个窗口之间发送消息。

常见问题

阻塞请求

如果您使用Laravel的Valet或PHP内置的web服务器,它们不能很好地处理并发连接并阻塞打开的流。测试此应用程序的唯一方法是使用能够进行线程化PHP执行的web服务器。

问题/评论/提交

这是一个持续进行的项目,我欢迎反馈和贡献。如果您有任何问题,请随时通过rulian.estivalletti@gmail.com给我发邮件。