2016-05-16 156 views
6

我正在为我的php应用程序实时的私人消息系统工作。我的代码一起为所有用户工作。但我需要私人消息系统作为一对一的消息。在PHP中使用Node.js,Socket.io,Redis的私人聊天消息

后设置节点和Redis的我能得到的消息数据我需要什么:这里是代码::

前端:: 1.我使用表单 - 用户名,邮件和发送按钮

和通知JS:

$(document).ready(function() { 

    var socket = io.connect('http://192.168.2.111:8890'); 

    socket.on('notification', function (data) { 
     var message = JSON.parse(data); 
     $("#notifications").prepend("<p> <strong> " + message.user_name + "</strong>: " + message.message + "</p>"); 

    }); 

}); 

服务器端JS:

var app = require('express')(); 
var server = require('http').Server(app); 
var io = require('socket.io')(server); 
var redis = require('redis'); 

server.listen(8890); 

var users = {}; 
var sockets = {}; 

io.on('connection', function (socket) { 

    console.log(" New User Connected "); 
    // instance of Redis Client 
    var redisClient = redis.createClient(); 
    redisClient.subscribe('notification'); 

    socket.on('set nickname', function (name) { 
     socket.set('nickname', name, function() { 
      socket.emit('ready'); 
     }); 
    }); 

    socket.on('msg', function() { 
    socket.get('nickname', function (err, name) { 
     console.log('Chat message by ', name); 
    }); 
    }); 


    redisClient.on("message", function(channel, message) 
    { 
    // to view into terminal for monitoring 
    console.log("Message from: " + message + ". In channel: " + channel + ". Socket ID "+ socket.id); 

    //send to socket 
    socket.emit(channel, message); 
    }); 

    redisClient.on('update_chatter_count', function(data) 
    { 
     socket.emit('count_chatters', data); 
    }); 

    //close redis 
    socket.on('disconnect', function() 
    { 
     redisClient.quit(); 
    }); 

}); 

HTML ::

<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script> 
<form .....> 
<input ....... > 
</form> 
<div id="notifications" ></div> 

过所有输出:

John : Hello 
Kate : Hi 
Others: ..... 

上面的代码在我的PHP应用程序很好地工作。现在我想设置私人或一对一的信息系统。

我需要为用户添加用户名或电子邮件或唯一套接字标识的方式。我没有更多关于私人消息的想法。我试图在网上找到,但失败了。

**如何在我的php应用程序中设置私人信息? **

回答

5

变量的基本初始化: -

晴使MAPmapOfSocketIdToSocket然后从前端发送特定用户的用户ID您要为之发送的消息。在服务器中找到与用户标识映射的套接字对象,并在该套接字中发送消息。以下是你可能需要去房概念私人一个一对多或一对一一个(如果只有两个成员)http://williammora.com/nodejs-tutorial-building-chatroom-with/

想法的样本(而不是完整的代码)

var io = socketio.listen(server); 
    var connectedCount = 0; 
    var clients = []; 
    var socketList = []; 
    var socketInfo = {}; 
    var mapOfSocketIdToSocket={}; 

    socket.on('connectionInitiation', function (user) { 
     io.sockets.sockets['socketID'] = socket.id; 
     socketInfo = {}; 
     socketInfo['userId']=user.userId; 
     socketInfo['connectTime'] = new Date(); 
     socketInfo['socketId'] = socket.id; 
     socketList.push(socketInfo); 

     socket.nickname = user.name; 
     socket.userId= user.userId; 

     loggjs.debug("<"+ user.name + "> is just connected!!"); 

     clients.push(user.userId); 
     mapOfSocketIdToSocket[socket.id]=socket; 
    } 
    socket.on('messageFromClient', function (cMessageObj, callback) { 
    for(var i=0; i<socketList.length;i++){ 
     if(socketList[i]['userId']==cMessageObj['messageToUserID']){ // if user is online 
     mapOfSocketIdToSocket[socketList[i]['socketId']].emit('clientToClientMessage', {sMessageObj: cMessageObj}); 
     loggjs.debug(cMessageObj); 
     } 
    } 
    }) 

无论是

+0

示例应用程序:http://justchatnow.herokuapp.com/和源代码https://github.com/codebazz/justchatnow –

+0

::它不在私人消息传递。我想我错过了一些东西。请您引导我多一点作为服务器端和客户端使用Redis客户端? –

+0

@Md。 Sariful islam似乎开放的聊天框,所有用户都可以在一个位置发送和接收消息。但它似乎是一对一的信息系统。 – naf4me

0

使用Pusher。它提供了通道使用,使私人聊天可能没有任何额外的代码

1

一个解决方案可能是发送消息给具有特定套接字ID的人。由于您已经在使用redis,因此您可以在用户加入时将用户的详细信息和套接字标识存储在redis中,并且只要您想向他发送消息,就可以通过从redis获取套接字标识来向用户发送消息。像

socket.emit呼叫事件( '发私')从前端

和后端处理

socket.on( '发私'){// 做Redis的东西,这里面}