我正在为php应用程序中的实时私有消息系统工作。我的代码对所有用户都有效。但我需要一对一的私人信息系统。
在设置节点和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应用程序中运行得很好。现在我想建立私人或一对一的消息系统。
我需要为用户添加用户名或电子邮件或唯一的socketID的方式。我对私人信息没有任何想法了。我试着上网,但没有成功。
**如何在php应用程序中设置私有消息?**
变量的基本初始化:-
首先,制作mapOfSocketIdToSocket的MAP,然后从前端发送要向其发送消息的特定用户的userid。在服务器中,找到与userid映射的套接字对象,并在该套接字中发出消息。以下是的想法示例(不是完整的代码)
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/
我建议您使用socketIO命名空间,它允许您将事件从/发送到特定的通信"通道"。
这是socketIO文档的链接,该文档涉及房间&命名空间
http://socket.io/docs/rooms-and-namespaces/
干杯
一种解决方案可以向具有特定套接字id的人发送消息。由于您已经在使用redis,您可以在用户加入时将用户的详细信息和套接字id存储在redis中,然后每当您想向用户发送消息时,通过从redis获取套接字id来向用户发送信息。呼叫事件,如
套接字从前端发出("发送专用")
并且在后端处理
socket.on("发送专用"){//在这个}中执行redis操作
使用Pusher。它提供频道使用,使私人聊天成为可能,而无需任何额外的代码