2017-06-24 8 views
0

私はプライベートチャットを作成したいと思います。PHP REDIS NODE.JS複数のパブリッシャ/サブスクライバ

説明:

あなたは[等...ユーザ_2、USER_3]ユーザーのリストを開き_1ある想像し、すべての要素がある特定のボタンをクリックした後、私は特定のでchatwindowを開始したいボタンがありますユーザー。 (サーバー側のLaravelプロジェクトポート:8000)を使用してWebソケットを有効に使用しています。私はnode.jsサーバーも実行しています(ポート3000)。 、私はRedisのパブ/サブを使用していNode.jsのために、PHPから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); 
    }); 
}); 

私はUser1がbutton2をクリックしているので、User_2とチャットを開いて、すべての情報を取得します:websocket_room(私はIOroomと呼んでいます)、mongodb_collectionなど...)

今私はふりをしますUser_3(他のブラウザを使用)とボタン4をクリックする(user_4とチャットする)私はwebsocket_roomに参加していて、まだ(彼がクリックされたボタンから)_1からL情報(websocket_room、mongodbcollection、等...)USER_3としてCONSOLE.LOG結果:

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

私は何が欠けていますか?私は何を忘れてしまったのですか?

答えて

0

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. は、どのように、フロントエンドは、新しいメッセージが送信されますか? Node.jsを使用したLaravelまたはWebSocketのREST APIを使用しますか?
  2. WSへのアップグレード接続中にフロントエンドはどのように認証されますか? Node.jsはLaravelまたはNode.jsへのリクエストを作成してデータベースにリクエストしますか?
  3. チャットの会話を任意のストレージファイルシステムまたはデータベースに保存する必要がありますか?
  4. Node.jsとLaravelアプリケーションにルーティングするためのNginxやNginxのようなものはありますか?
+0

こんにちは@galkin、chatアプリケーションはlaravel-projectの外部アプリケーションとして設計されています 1. Node.jsを持つWSフロントエンドのindex.html: 'socket.emit ( 'new message、{ data ...})' 2. Node.jsはMongodbと連携しています: 'mongo.connect( 'mongodb://127.0.0.1/chat'、function(err、db){ socker.on( 'new message'、... )内のcol.insert({name:name、message:message}、function(){data ....} 'var col = db.collection(colName) } 3.答えは? 4.ローカルホストのApacheで使用していますが、Nginxで作業する方法をお伝えしています:) 私はあなたの考えに感謝しています。コード。 – booky

+0

私は問題が複数の加入者/出版社の中にあると思ったので、私はすべてのコードを掲載していませんか、間違っていますか?はい、expressのルーティングはredis加入者機能の外にあります。 :) – booky

0

理論的解決策: AMQP-server、node.jsサーバーにセッションを追加する。今私はチャットサーバーをセットアップする方法を知っています。私は自分でそれを理解しました...

関連する問題