【问题标题】:Socket.io - How to comment on post based on post id?Socket.io - 如何根据帖子 ID 评论帖子?
【发布时间】:2016-03-18 17:10:58
【问题描述】:

我正在使用 Laravel 5.0,并且我有一个视图,其中显示了多个帖子。我的 Post.php、Comment.php 和我的 User.php 之间存在模型关系

home.blade.php 视图:

@foreach($posts as $post)
<h1>{{ $post->title }}</h1>
<p>{{ $post->description }}</p>
<div id="comments">
    @foreach($post->comments as $comment)
    <h3>{{ $comment->user->username }}</h3>
    <p>{{ $comment->comment }}</p>
    <hr>
    @endforeach
</div>
@endforeach

使用 socket.io v1.3.5,我如何为每个帖子指定 cmets。例如,我有一个 ID 为 1 的帖子和另一个 ID 为 2 的帖子。我必须怎么做才能使帖子 1 中的评论保留在帖子 1 中,而帖子 2 中的评论保留在帖子 2 中。目前,一个在帖子 1 中发表的评论将出现在帖子 2 cmets 部分,反之亦然。

PostController.php

 $data = [
        'event' => 'UserComment',
        'data'  => [
            'username' => Auth::user()->getUsername(),
            'comment'  => Request::input('comment'),
            'post_id'   => $id
        ]
    ];

    Redis::publish('user-comment', json_encode($data));

server.js

var server = require('http').Server();

var io     = require('socket.io')(server);

var Redis  = require('ioredis');

var redis  = new Redis();

var usernames = {};



redis.subscribe('user-comment');

redis.on('message', function(channel, message){

message = JSON.parse(message);

var room = message.data.post_id;

console.log(room);

// io.emit(channel + ':' + message.event, message.data);

});

server.listen(3000);

home.blade.php - javascript

socket.on('user-comment:UserComment', function(data){
        $('#home_chat_content_div_body').append($('<h4>').text(data.username).css('color', '#00c5cd'));
        $('#home_chat_content_div_body').append($('<p>').text(data.comment));
        $('#home_chat_content_div_body').append($('<hr>'));

【问题讨论】:

    标签: php jquery socket.io laravel-5


    【解决方案1】:

    你有几个选择。但是,基本上你想将你的评论从 PHP 发送到 SocketIO。本质上,PHP 将充当 SocketIO 的客户端并发出消息。无论何时在代码中插入注释,您都希望使用以下方法之一将事件发送到 SocketIO:

    您可以使用 ElephantIO:

    use ElephantIO\Client as Elephant;
    
    $elephant = new Elephant('http://localhost:8000', 'socket.io', 1, false, true, true);
    
    $elephant->init();
    $elephant->send(
        ElephantIOClient::TYPE_EVENT,
        null,
        null,
        json_encode(array('name' => 'foo', 'args' => 'bar'))
    );
    $elephant->close();
    
    echo 'tryin to send `bar` to the event `foo`';
    

    来源:http://elephant.io/#usage

    或者,如果你在后台有 Redis,你可以使用 SocketIO Redis PHP 发射器:

    $redis = new \Redis(); // Using the Redis extension provided client
    $redis->connect('127.0.0.1', '6379');
    $emitter = new SocketIO\Emitter($redis);
    $emitter->emit('chat message', 'payload str');
    

    来源:https://github.com/rase-/socket.io-php-emitter

    此外,您将从 index.js 中删除此代码,因为不再需要它:

    socket.on('chat message', function(msg){
        io.emit('chat message', msg);
    });
    

    而且... SocketIO 的最新版本是 1.3.7。如果可能,绝对升级。


    SocketIO 使用房间的示例方法:

    $redis = new \Redis(); // Using the Redis extension provided client
    $redis->connect('127.0.0.1', '6379');
    $emitter = new SocketIO\Emitter($redis);
    $emitter->in('post:123'); // send this event only to clients who are in this room for 'new comment'
    $emitter->emit('new comment', $dataArray);
    

    然后在您的客户端中:

    socket.emit('subscribe-post', 'post:123'); // subscribe to events in the post:123 room
    socket.on('new comment', function(data){
        console.log( data );
    });
    

    然后在节点中:

    socket.on('subscribe-post', function(room) {
        socket.join(room); // join the post:123 room
    });
    

    【讨论】:

    • 所以真的没有办法纯粹用socket.io和express来做到这一点吗?
    • 您可以从节点而不是 php 进行插入,然后以这种方式发出。虽然我认为这不是一个好主意。
    • 因为可能存在注入漏洞?
    • 不,因为现在您正在从 Node 而不是 PHP 进行插入(奇怪的维护/存储数据方式的异常),使用我建议的方法解决问题是一种奇怪的方法。你完全理解我的建议吗?您只需在您正在执行 $comment->save(); 的地方添加一些行在你的 PHP 中。你能修改PHP吗?
    • 是的,您的建议既有用又值得赞赏。我唯一的问题是,对于 redis,我将不得不研究应用程序的 vps 托管,因为这最终将被推入生产环境,而 vps 托管并没有真正超出我的预算。我从来没有听说过大象.io,但它听起来正是我所需要的
    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2016-04-14
    • 2022-01-10
    • 2016-11-07
    • 2017-07-07
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多