在Laravel项目中集成PHP套接字

时间:2019-08-06 07:08:15

标签: php laravel websocket socket.io

我正在开发一个类似于instagram故事部分的项目模块。 其他用户将上载故事(文本/视频/图像),并且登录的用户可以观看其他用户的故事。用户可以发布多个故事。如果用户已经观看了故事并且用户上传了一些新的故事,那么对于用户已经观看过的故事,则为了更新故事的可见状态,用户应该观看那些已经看过的故事,并且应该看到新的故事。为了发送实时故事可见状态,我想使用套接字发送故事状态。

有人可以告诉我如何在我的Laravel项目代码中集成PHP套接字。我尝试按照this tutorial中给出的所有步骤来执行此操作,但是当我运行在视图中编写的服务器脚本时,页面仍继续加载。

我也见过php manual

我只需要按照要求使用php套接字来做到这一点!

routes.api.php

Route::get('server', 'API\NotificationController@storySeen');
Route::post('server-post', 'API\NotificationController@serverPost')->name('server.post');
Route::get('client', 'API\NotificationController@client');

API \ UserProfileController.php

public function storySeen()
{
    return view('server');
}

public function serverPost(Request $request)
{
    $serverReply = $request->input('reply');
    $host = "localhost";
    $port = 8189;

    // No Timeout
    set_time_limit(10);
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or dd("Could not create socket\n");
    $result = socket_bind($socket, $host, $port) or dd("Could not bind to socket\n");
    $result = socket_listen($socket, 3) or dd("Could not set up socket listener\n");
    $spawn = socket_accept($socket) or dd("Could not accept incoming connection\n");
    $input = socket_read($spawn, 1024) or dd("Could not read input\n");

    $output = strrev($input) . "\n";
    $mg = trim($output);
    $client = "Client says : \t" . $mg;
    socket_write($spawn, $client, strlen($client)) or dd("Could not write output\n");
    socket_close($spawn);
    socket_close($socket);

    return view('server', ['client' => $client]);
}

public function client()
{
    return view('test');
}

resources \ views \ server.blade.php

<div class="flex-center position-ref full-height">
    <div class="content">
        <form method="POST" action="{{route('server.post')}}" enctype="multipart/form-data">
            {{csrf_field()}}
            <table>
                <tr>
                    <label>Enter message:</label>
                    <input type="text" name="reply">
                    <input type="submit" name="btnSend" value="Send">
                </tr>
                <?php
                //                if (isset($_GET['btnSend']))
                //                {
                //                    $host ="192.168.1.109";
                //                    $port = 20205;
                //
                //                    $msg  = $_REQUEST['txtMessage'];
                //                    $sock  = socket_create(AF_INET, SOCK_STREAM,0)  or die("Could not create");
                //                    socket_connect($sock, $host, $port)  or die("Could not Connect");
                //                    socket_write($sock, $msg, strlen($msg))  or die("Could not write");
                //                    $reply = socket_read($sock,1024);
                //                    $reply = trim($reply);
                //                        $reply= "Server says : \t" . $reply;
                //                }
                //                ?>
                <tr>
                    <td>
                        <textarea rows="10" col="30"> {{isset($client)?$client:''}}</textarea>
                    </td>
                </tr>
            </table>
        </form>

        <div class="links">

        </div>
    </div>
</div>

resources \ views \ test.blade.php

<form method="GET" enctype="multipart/form-data">
    {{csrf_field()}}
    <table>
        <tr>
            <label>Enter message:</label>
            <input type="text" name="txtClient">
            <input type="submit" name="btnSend" value="Send">
        </tr>
        <?php
        if (isset($_GET['btnSend'])) {
            $host = "localhost";
            $port = 8189;

            $msg = $_REQUEST['txtClient'];
            $sock = socket_create(AF_INET, SOCK_STREAM, 0) or dd("Could not create");
            socket_bind($sock, $host, $port) or dd("Could not Connect");
            //socket_connect($sock, $host, $port)  or dd("Could not Connect");
            socket_write($sock, $msg, strlen($msg)) or dd("Could not write");
            $reply = socket_read($sock, 1024);
            $reply = trim($reply);
            $reply = "Server says : \t" . $reply;
            socket_close($sock);

        }
        ?>
        <tr>
            <td>
                <textarea rows="10" col="30"> {{isset($reply)?$reply:''}}</textarea>
            </td>
        </tr>
    </table>
</form>

运行服务器脚本时,我的页面继续加载。并在重试时显示。

  

由于端口已在使用中而无法绑定。

当我运行客户端脚本时,出现错误消息:

  

断管

0 个答案:

没有答案