【问题标题】:PHP REDIS NODE.JS multiple publisher/subscriberPHP REDIS NODE.JS 多个发布者/订阅者
【发布时间】:2017-11-27 23:34:38
【问题描述】:

我想创建一个私人聊天室。

解释:

假设你是 User_1 你打开一个用户列表[User_2, User_3 ...etc],每个元素在点击一个特定按钮后都有一个按钮我想与特定用户启动一个聊天窗口。 (服务器端 Laravel 项目端口:8000)有效地使用 Websockets 我也在运行一个 node.js 服务器(端口 3000)。要将数据从 php 传输到 node.js,我使用的是 redis Pub/Sub,ChatController.php:

class ChatController extends Controller
{

   public function toChat($ForeignUserId)
   {
    $fid = (int) $ForeignUserId;
    $uid = (int) Sentinel::getUser()->id;

    $CollectionName1 = $fid . "collection" . $uid;
    $CollectionName2 = $uid . "collection" . $fid;
    $roomName = $uid . "room" . $fid;

    $res = DB::table('chats')
              ->where('collection_name', '=', $CollectionName1)
              ->orWhere('collection_name', '=', $CollectionName2)
              ->take(1)
              ->get();

    if(count($res) == 1)
    {
      $redis = Redis::connection();
      //$redis->publish('chat-channel', json_encode([['idone' => $uid], ['idtwo' => $fid]]));
      $redis->publish('chat-channel', json_encode(['idone' => $uid, 'idtwo' => $fid, 'IOroom' => $res[0]->room_name, 'collection' => $res[0]->collection_name]));
    }
    else
    {
      $date = date('Y-m-d H:m:s');
      DB::table('chats')->insert(['collection_name' => $CollectionName2, 'room_name' => $roomName, 'sender' => $uid, 'receiver' => $fid, 'created_at' => $date]);

      //$client = new MongoDB\Client;
      $client = new Mongo;
      $companydb = $client->chat;
      $result1 = $companydb->createCollection($CollectionName2);

      $redis = Redis::connection();
      //$redis->publish('chat-channel', json_encode([['idone' => $uid], ['idtwo' => $fid]]));
      $redis->publish('chat-channel', json_encode(['idone' => $uid, 'idtwo' => $fid, 'IOroom' => $roomName, 'collection' => $CollectionName2]));
    }
    //return view('chat');
    $url = "http://localhost:3000";
    return Redirect::to($url);
   }
}

Node.js 文件(server.js 端口:3000)接收数据:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoClientInstance = require('mongodb').MongoClient;
var redis = require('redis');
var php_listener = redis.createClient();

   app.get('/', function(req, res){
        res.sendFile(__dirname + '/index.html');
   });


php_listener.subscribe('chat-channel');

php_listener.on('message', function(channel, message){
   var purchase_data = JSON.parse(message);
   let colName = purchase_data.collection;
   let roomName = purchase_data.IOroom;

   let data = [];
   data.push(purchase_data.idone);
   data.push(purchase_data.idtwo);
   data.push(purchase_data.collection);
   data.push(purchase_data.IOroom);

   http.listen(process.env.PORT || 3000);
   console.log('Server running...');

   io.on('connection', function(socket){
       socket.join(roomName);
       io.sockets.in(roomName).emit('roomChat', data);
   });
});

我的问题如下:我是 User_1 点击 button2(所以打开与 User_2 聊天,获取所有信息:websocket_room(我称之为 IOroom)、mongodb_collection 等...)

现在让我们假装我是 User_3(使用其他浏览器)并点击按钮 4(与 user_4 聊天)等...) Console.log 结果为 User_3:

 (4) [2, 1, "2collection1", "2room1"] 
 (4) [4, 3, "4collection3", "4room3"]

我错过了什么?我忘记了什么?

【问题讨论】:

    标签: php node.js redis


    【解决方案1】:

    您的 Node.js 应用程序有一段奇怪的代码。我认为您并不了解代码的每一行。例如:

    //this code will add express routeing rule on every message in Redis
    app.get('/', function(req, res){
      res.sendFile(__dirname + '/index.html');
    });
    

    在实施聊天之前,您应该做出以下决定:

    1. 前端如何发送新消息?通过 Laravel 的 REST API 或 Node.js 的 WebSocket?
    2. 在升级连接到 WS 期间,前端如何进行身份验证? Node.js 会向 Laravel 发出请求,还是 Node.js 会向数据库发出请求?
    3. 您应该将聊天对话存储到任何存储文件系统或数据库吗?
    4. 您会使用 Nginx 或任何类似 Nginx 的东西来路由到您的 Node.js 和 Laravel 应用程序吗?

    【讨论】:

    • 嗨@galkin,聊天应用程序被设计为来自 laravel 项目的外部应用程序 1. WS with Node.js Front-end index.html : socket.emit('new message, { data...}) 2. Node.js 是与 Mongodb 合作:mongo.connect('mongodb://127.0.0.1/chat', function(err, db){ var col = db.collection(colName); within the socker.on('new message', ... col.insert({name: name, message: message}, function(){data....} }) 3. 回答为 2.? 4. 我在我的 localhost apache 上使用,但我正在阅读如何使用 Nginx :) 感谢您的想法。 :) 我还没有发布所有代码。
    • 我没有发布所有代码,因为我认为问题出在使用 redis 的多个订阅者/发布者中,还是我错了?是的,express 的路由现在不在 redis 订阅者功能范围内。 :)
    【解决方案2】:

    理论解决方案: AMQP-Server,并在 node.js 服务器上添加会话。现在我知道如何设置聊天服务器了。我自己想出来的……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      相关资源
      最近更新 更多