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

时间:2013-11-05 16:57:06

标签: 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');
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))我不会以相同的顺序回复其他客户端,如果客户端中的数字如200,201,300,202,320,203。

我认为这是因为PHP延迟响应。

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

1 个答案:

答案 0 :(得分:-2)

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

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

连接方式: mysql = require('mysql'),

connection = mysql.createConnection({     主持人:'localhost',     用户:'用户名',     密码:'密码',     数据库:'数据库名称',     港口:3306   }),

使用以下命令抛出查询:

var q = connection.query('select * from table);