我正在爲我的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應用程序中設置私人信息? **
示例應用程序:http://justchatnow.herokuapp.com/和源代碼https://github.com/codebazz/justchatnow –
::它不在私人消息傳遞。我想我錯過了一些東西。請您引導我多一點作爲服務器端和客戶端使用Redis客戶端? –
@Md。 Sariful islam似乎開放的聊天框,所有用戶都可以在一個位置發送和接收消息。但它似乎是一對一的信息系統。 – naf4me