Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将PHP和NodeJS与Redis同步并按顺序获取数据_Php_Node.js_Redis - Fatal编程技术网

如何将PHP和NodeJS与Redis同步并按顺序获取数据

如何将PHP和NodeJS与Redis同步并按顺序获取数据,php,node.js,redis,Php,Node.js,Redis,我有一个示例代码,用于尝试在一个简单的聊天示例中思考如何同步nodejs和php 这是我的NodeJS服务器: var redis = require('redis'), subscriber = redis.createClient(), publisher = redis.createClient(); //var sckio = require('socket.io').listen(8888); var http = require('http');

我有一个示例代码,用于尝试在一个简单的聊天示例中思考如何同步nodejs和php

这是我的NodeJS服务器:

var redis       = require('redis'),
    subscriber  = redis.createClient(),
    publisher   = redis.createClient();

//var sckio = require('socket.io').listen(8888);
var http = require('http');
var querystring = require('querystring');

var WebSocketServer = require('ws').Server

var ENCODING = 'utf8';

var tCounter = 0;

/* #################################### */

// Event on "subscribe" to any channel
subscriber.on("subscribe", function (channel, count) {

    // Publish to redis server Test Message
    publisher.publish("chat", "NODEJS MESSAGE");

});

// Suscrib to redis server
subscriber.on('message', function (channel, json) {
    console.log('SUB: ' + channel + ' | ' + json);

    console.log('PHP PUSH TO REDIS, AND NODE CAPTURE REDIS PUSH: ' + (getMicrotime(true) - tCounter));       

});

subscriber.subscribe('chat'); // Subs to "mysql" channel


/*
var clients = [];
sckio.sockets.on('connection', function (socket) {

  clients.push(socket);
  publisher.publish("chat", "User connected");

  socket.on('message', function (from, msg) {

    publisher.publish("chat", msg);

    clients.forEach(function (client) {
      if (client === socket) return;
      client.send(msg);
    });

  });

  socket.on('disconnect', function () {

    clients.splice(clients.indexOf(socket), 1);
    publisher.publish("chat", "User disconnected");

  });

});
*/

var wss = new WebSocketServer({port: 8888, timeout : 500});
var wsClients = [];

wss.on('connection', function(ws) {


    ws.AUTH_ID = Math.random();
    wsClients.push(ws);

    publisher.publish("chat", "User enter");

    ws.on('message', function(message) {

        wsClients.forEach(function (client) {
           client.send(ws.AUTH_ID + ' ' + message);
        });

        tCounter = getMicrotime(true);
        console.log('CALL TO PHP: ' + tCounter);

        PostCode('CODE TO PHP FROM NODE', function() {
            wsClients.forEach(function (client) {
               client.send('PHP SAVE DATA');
            });

        });

    });

    ws.on('close', function(message) {

      wsClients.splice(wsClients.indexOf(ws), 1);
      publisher.publish("chat", "User left");

    });

    ws.send('HELLO USER!');

});    

function getMicrotime(get_as_float) {
    var now = new Date().getTime() / 1000;
    var s = parseInt(now, 10);

    return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}

function PostCode(codestring, callback) {

  // Build the post string from an object
  var post_data = querystring.stringify({
      'output_format': 'json',
        'js_code' : codestring
  });

  // An object of options to indicate where to post to
  var post_options = {
      host: '127.0.0.1',
      port: '80',
      path: '/NodeJS/chat_system/php_system.php',
      method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': post_data.length
        }
  };

  // Set up the request
  var post_req = http.request(post_options, function(res) {
      res.setEncoding(ENCODING);
      res.on('data', function (chunk) {

          console.log('Response FROM PHP: ' + chunk);
          if (typeof callback == 'function') {
            callback(chunk);
          }

      });
  });

  // post the data
  post_req.write(post_data);
  post_req.end();

}
这是我的PHP服务器

require 'Predis/Autoloader.php';
Predis\Autoloader::register();



function pushToRedis($data) {

    try {

        $redis = new Predis\Client(array(
            'scheme' => 'tcp',
            'host'   => '127.0.0.1',
            'port'   => 6379,
        ));       

    } catch (Exception $e) {
        echo "Couldn't connected to Redis";
        echo $e->getMessage();
        return false;
    }

    $json = json_encode($data);
    $redis->publish("chat", $json);

    return true;

}


pushToRedis('PHP PUSH TO REDIS!');

header('Content-Type: application/json');
echo json_encode(array('response' => print_r(array($_REQUEST, $_SERVER), true)));
我的客户:

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<title>WebSockets - Simple chat</title>

<style>

    .chat { width: 400px; height: 250px; overflow-y: scroll; }

</style>

</head>
<body>


<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<script>

    var connection = new WebSocket('ws://127.0.0.1:8888');

    $(document).ready(function() {

        /*
        var socket = io.connect('http://127.0.0.1:8888');
        socket.on('message', function (data) {
          alert(data);
        });
        socket.send('HELLO!');
        */


        connection.onopen = function () {
            console.log('connected!');
        };

        connection.onerror = function (error) {

        };  

        connection.onmessage = function (message) {
            $('.chat').append(message.data + '<br>');
            $('.chat').scrollTop($('.chat')[0].scrollHeight);
        };

        $('input[name="text"]').on('keydown', function(e) {

            if (e.keyCode === 13) {
                var msg = $(this).val();
                connection.send(msg);
                $(this).val('').focus();
            }

        });




    });


</script>


<div class="chat">

</div>

<input type="text" name="text">

</body>
</html>
问题在于当网络处于繁忙状态时,PHP通过Redis将响应返回给NodeJS的顺序

例如:我从de javascript客户端发送更多消息,然后,NodeJS调用PHP每一条消息,PHP将数据保存在MYSQL中,并调用Redis,NodeJS检测Redis推送并更新客户端。但是,在某些情况下,如果我从Javascript客户机循环发送一些0-10000的消息,我不会以相同的顺序回复其他客户机,如果在客户机中获得像200201300202320203这样的数字

我认为这是为了PHP延迟响应


我的问题是如何管理响应以正确的顺序更新客户端?因为这个问题可能会导致客户端以错误的顺序接收聊天消息。

但是为什么要使用php,您可以使用nodejs的mysql包直接向mysql发送数据

您可以通过以下方式安装mysql软件包: npm安装mysql

连接方式: mysql=需要“mysql”

connection=mysql.createConnection{ 主机:“localhost”, 用户:“用户名”, 密码:“password”, 数据库:'数据库名称', 港口:3306 },

并使用以下命令抛出查询:


var q=connection.query'select*from表

我认为您无法控制这一点,因为您有3个变量需要考虑,1是从节点到php的延迟,2是sql写入的延迟,3是redis的延迟。我想。。。但我不是专家,我的想法是另一种。。当我从web客户端向节点发送消息时,我会等待将消息发送给聊天室的其他用户,完成后,向原始用户发送“确定”以在消息聊天室中加载消息,此方法会同步消息,但如果用户延迟太长,消息不会出现,用户可能会认为消息未发送,并尝试写入更多消息。。我认为用户体验很差。。等待响应是一个选项,但会将用户的延迟增加一倍。。