Socket.io - 如何根据post id 评论post?

Socket.io - How to comment on post based on post id?

我正在使用 Laravel 5.0,并且我有一个视图,其中显示了多个 post。我的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,我将如何为每个 post 指定注释。例如,我有一个 ID 为 1 的 post 和另一个 ID 为 2 的 post。我必须怎么做才能使 Post 1 中的评论保留在 Post 中1 而在 Post 2 中发表的评论保留在 Post 2 中。目前,在 Post 1 中发表的评论将出现在 Post 2 评论部分,反之亦然。

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 发送到 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
});