您好我正在使用Laravel 5.2 redis nodeJs开发实时聊天应用程序它对聊天室工作正常但我想使用私聊 按唯一用户名过滤。
socket.emit(receiverUniqueUserName, data); //NodeJs server side using the receiver name on socket emit.
socket.on(receiverUniqueUserName, function (data) { //using the same receiver name at client side of socket.
我不确定它是否有效?如果没有,那么有人可以建议我使用laravel5.2,MySql,Redis,Socket.io私人聊天的示例吗?
代码块: 从控制器我通过频道“message-channel”发布数据
public function sendMessage(){
$redis = LRedis::connection();
$data = ['message' => Request::input('message'), 'senderUniqueUserName' => Request::input('senderUniqueUserName'), 'receiverUniqueUserName' => Request::input('receiverUniqueUserName')];
$redis->publish('message-channel', json_encode($data));
return response()->json([$redis]);
}
在nodeJs的服务器端:
io.on('connection', function (socket) {
var redisClient = redis.createClient();
redisClient.subscribe('message-channel');
redisClient.on('message', function (channel, data) {
var jsonObj = JSON.parse(data);
var receiverUniqueUserName = jsonObj.receiverUniqueUserName;
socket.emit(receiverUniqueUserName, data); //using the receiver name on socket emit.
});
});
在socket的客户端:
socket.on(receiverUniqueUserName, function (data) { //using the same receiver name at client side of socket.
data = jQuery.parseJSON(data);
$("#messages").append("<div><strong>" + data.senderUniqueUserName + " : </strong><span>" + data.message + "</span></div>");
});