使用Node.js,Socket.io,Redis的私人聊天消息在PHP中
我正在为我的PHP应用程序实时的私人消息系统工作。 我的代码正在为所有用户一起工作。 但我需要私人消息系统作为一对一的消息。
安装节点和Redis后,我可以得到我需要的消息数据:这里是代码::
前端:: 1.我使用一个表单 – 用户名,消息和发送button
和通知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应用程序中很好地工作。 现在我想设置私人或一对一的信息系统。
我需要为用户添加用户名或电子邮件或唯一套接字ID。 我没有更多的私人信息的想法。 我试图在网上find,但失败了。
**如何设置私人消息到我的PHP应用程序? **
variables的基本初始化: –
主要是使mapOfSocketIdToSocket的 MAP从前端发送你要发送消息的特定用户的用户标识。 在服务器中find与userid映射的套接字obeject ,并在该套接字中发送消息。 这是一个想法的例子(而不是完整的代码)
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); } } })
要么你可能想要私人的一对多或一对一(如果只有两个成员)的空间概念http://williammora.com/nodejs-tutorial-building-chatroom-with/
我build议你使用socketIO命名空间,它允许你发送/发送特定通信“通道”的事件。
这里是关于房间和命名空间的socketIO文档的链接
http://socket.io/docs/rooms-and-namespaces/
干杯
一种解决scheme可能是发送消息给具有特定套接字ID的人。 由于您已经在使用redis,因此您可以在用户join时将用户的详细信息和套接字标识存储在redis中,然后通过从redis中获取套接字标识来向用户发送消息。 呼叫类似的事件
socket.emit('发送私人')从前端
并在后端处理
socket.on('发送私人'){/ /这里做redis的东西}
使用推杆 。 它提供频道使用,使私人聊天可能没有任何额外的代码