私人聊天消息使用Node.js,Socket.io,Redis in PHP

前端之家收集整理的这篇文章主要介绍了私人聊天消息使用Node.js,Socket.io,Redis in PHP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为我的PHP应用程序中的一个实时私人消息系统工作.我的代码一起为所有用户服务.但我需要私人消息系统作为一对一的消息.

在设置节点和redis后,我可以得到我需要的消息数据:这里是代码::@H_404_3@

前端 ::
1.我使用表格 – 用户名,信息和发送按钮@H_404_3@

通知JS:@H_404_3@

  1. $( document ).ready(function() {
  2.  
  3. var socket = io.connect('http://192.168.2.111:8890');
  4.  
  5. socket.on('notification',function (data) {
  6. var message = JSON.parse(data);
  7. $( "#notifications" ).prepend("<p> <strong> " + message.user_name + "</strong>: " + message.message + "</p>" );
  8.  
  9. });
  10.  
  11. });

服务器端js:@H_404_3@

  1. var app = require('express')();
  2. var server = require('http').Server(app);
  3. var io = require('socket.io')(server);
  4. var redis = require('redis');
  5.  
  6. server.listen(8890);
  7.  
  8. var users = {};
  9. var sockets = {};
  10.  
  11. io.on('connection',function (socket) {
  12.  
  13. console.log(" New User Connected ");
  14. // instance of Redis Client
  15. var redisClient = redis.createClient();
  16. redisClient.subscribe('notification');
  17.  
  18. socket.on('set nickname',function (name) {
  19. socket.set('nickname',name,function () {
  20. socket.emit('ready');
  21. });
  22. });
  23.  
  24. socket.on('msg',function () {
  25. socket.get('nickname',function (err,name) {
  26. console.log('Chat message by ',name);
  27. });
  28. });
  29.  
  30.  
  31. redisClient.on("message",function(channel,message)
  32. {
  33. // to view into terminal for monitoring
  34. console.log("Message from: " + message + ". In channel: " + channel + ". Socket ID "+ socket.id );
  35.  
  36. //send to socket
  37. socket.emit(channel,message);
  38. });
  39.  
  40. redisClient.on('update_chatter_count',function(data)
  41. {
  42. socket.emit('count_chatters',data);
  43. });
  44.  
  45. //close redis
  46. socket.on('disconnect',function()
  47. {
  48. redisClient.quit();
  49. });
  50.  
  51. });

HTML ::@H_404_3@

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

总体输出:@H_404_3@

  1. John : Hello
  2. Kate : Hi
  3. Others: .....

上面的代码在我的PHP应用程序中运行良好.现在我想建立私人或一对一的消息系统.@H_404_3@

我需要为用户添加用户名或电子邮件或唯一套接字ID的方式.我对私人消息没有更多的想法.我试图在网上找到但却失败了.@H_404_3@

**如何在我的PHP应用程序中设置私人消息? **@H_404_3@

解决方法

变量的基本初始化: –

通常使mapOfSocketIdToSocket的MAP发送您要从前端发送消息的特定用户用户ID.在服务器中找到与userid映射的套接字obeject,并在该套接字中发出消息.以下是该想法的示例(不是完整代码)@H_404_3@

  1. var io = socketio.listen(server);
  2. var connectedCount = 0;
  3. var clients = [];
  4. var socketList = [];
  5. var socketInfo = {};
  6. var mapOfSocketIdToSocket={};
  7.  
  8. socket.on('connectionInitiation',function (user) {
  9. io.sockets.sockets['socketID'] = socket.id;
  10. socketInfo = {};
  11. socketInfo['userId']=user.userId;
  12. socketInfo['connectTime'] = new Date();
  13. socketInfo['socketId'] = socket.id;
  14. socketList.push(socketInfo);
  15.  
  16. socket.nickname = user.name;
  17. socket.userId= user.userId;
  18.  
  19. loggjs.debug("<"+ user.name + "> is just connected!!");
  20.  
  21. clients.push(user.userId);
  22. mapOfSocketIdToSocket[socket.id]=socket;
  23. }
  24. socket.on('messageFromClient',function (cMessageObj,callback) {
  25. for(var i=0; i<socketList.length;i++){
  26. if(socketList[i]['userId']==cMessageObj['messageToUserID']){ // if user is online
  27. mapOfSocketIdToSocket[socketList[i]['socketId']].emit('clientToClientMessage',{sMessageObj: cMessageObj});
  28. loggjs.debug(cMessageObj);
  29. }
  30. }
  31. })

你可能想要私人一对多或一对一的房间概念(如果只有两个成员)http://williammora.com/nodejs-tutorial-building-chatroom-with/@H_404_3@

猜你在找的Node.js相关文章