No response is returned for the Ajax request to subscribe to Redis.

Source: Internet
Author: User
There is a button on the interface, click to send an Ajax request to the background, the requested file content: redis. php: {code ...} then, the server simulates the release of a message to the channel XGD and publishes the operation code: Publish. php: {code ...} the server uses Centos6.5, and the server uses the php Command to run pu... there is a button on the interface, click to send an Ajax request to the background, the requested file content:

Redis. php:

Ini_set ('default _ socket_timeout ', 25); $ redis = new Redis (); $ conn = $ redis-> connect ('2017. 127. 0.0.1 ', '000000'); if ($ conn) {$ redis-> subscribe (array ('xgd'), 'ckr ');} else {echo 'local Redis cannot be connected! '; Exit;} function ckr ($ instance, $ channel, $ msg) {echo $ msg ;}

Then, the server sends a message to the channel XGD and publishes the operation code:

Publish. php:

$ Redis = new Redis (); $ conn = $ redis-> connect ('2017. 0.0.1 ', '000000'); if ($ conn) {$ I = 0; while (true) {sleep (1); $ I ++; $ respose ['id'] = uniqid (); $ respose ['v _ ltl_status '] = uniqid (); $ respose ['ltl _ time'] = uniqid (); $ respose ['ltl _ reckon '] = uniqid (); $ respose ['ltl _ cardno'] = uniqid (); $ respose ['Card _ type'] = uniqid (); $ respose ['Card _ name'] = uniqid (); $ respose ['ltl _ cardflag '] = uniqid (); $ respose ['rsd _ bicc'] = uniqid (); $ respose ['v _ ltl_type '] = uniqid (); $ res_json = json_encode (array ('result' => $ respose )); $ redis-> publish ('xgd ', $ res_json); // echo 'data published '. $ I.'
';}} Else {echo' Local Redis cannot be connected! '; Exit ;}

The server uses Centos6.5 and runs publish. php with the php Command on the server,
At the same time, enter the local redis-cli command line and execute the command: subscribe XGD,
The page shows the content published by the subscription channel cyclically. restart a command terminal and run redis. php with the php Command. The message published by the subscription channel also appears on the command line. The problem is:
Ajax requests on the Web Front end always run out of time and no results are returned. I don't know why. Please kindly enlighten me.

The Ajax code is:

Function loadResult () {// $. pdialog. open ("/index. php/Article/getRedisInfo ", 'operate', 'Operation result', {'width': '000000', 'height': '000000', 'mask ': true}); var dt = new Date (); var ts = dt. getTime (); $. ajax ({cache: false, type: 'post', url: '/redis. php ', global: false, dataType: 'json', async: true, data: {time: ts}, timeout: 1000, success: function (data) {$ (". redis "2.16.html ($ (". redis "2.16.html () + data. result) ;}, complete: function () {loadResult ();}})}

(The project uses Thinkphp + DWZ)

========================================================== ======================================

Reply content:

There is a button on the interface, click to send an Ajax request to the background, the requested file content:

Redis. php:

Ini_set ('default _ socket_timeout ', 25); $ redis = new Redis (); $ conn = $ redis-> connect ('2017. 127. 0.0.1 ', '000000'); if ($ conn) {$ redis-> subscribe (array ('xgd'), 'ckr ');} else {echo 'local Redis cannot be connected! '; Exit;} function ckr ($ instance, $ channel, $ msg) {echo $ msg ;}

Then, the server sends a message to the channel XGD and publishes the operation code:

Publish. php:

$ Redis = new Redis (); $ conn = $ redis-> connect ('2017. 0.0.1 ', '000000'); if ($ conn) {$ I = 0; while (true) {sleep (1); $ I ++; $ respose ['id'] = uniqid (); $ respose ['v _ ltl_status '] = uniqid (); $ respose ['ltl _ time'] = uniqid (); $ respose ['ltl _ reckon '] = uniqid (); $ respose ['ltl _ cardno'] = uniqid (); $ respose ['Card _ type'] = uniqid (); $ respose ['Card _ name'] = uniqid (); $ respose ['ltl _ cardflag '] = uniqid (); $ respose ['rsd _ bicc'] = uniqid (); $ respose ['v _ ltl_type '] = uniqid (); $ res_json = json_encode (array ('result' => $ respose )); $ redis-> publish ('xgd ', $ res_json); // echo 'data published '. $ I.'
';}} Else {echo' Local Redis cannot be connected! '; Exit ;}

The server uses Centos6.5 and runs publish. php with the php Command on the server,
At the same time, enter the local redis-cli command line and execute the command: subscribe XGD,
The page shows the content published by the subscription channel cyclically. restart a command terminal and run redis. php with the php Command. The message published by the subscription channel also appears on the command line. The problem is:
Ajax requests on the Web Front end always run out of time and no results are returned. I don't know why. Please kindly enlighten me.

The Ajax code is:

Function loadResult () {// $. pdialog. open ("/index. php/Article/getRedisInfo ", 'operate', 'Operation result', {'width': '000000', 'height': '000000', 'mask ': true}); var dt = new Date (); var ts = dt. getTime (); $. ajax ({cache: false, type: 'post', url: '/redis. php ', global: false, dataType: 'json', async: true, data: {time: ts}, timeout: 1000, success: function (data) {$ (". redis "2.16.html ($ (". redis "2.16.html () + data. result) ;}, complete: function () {loadResult ();}})}

(The project uses Thinkphp + DWZ)

========================================================== ======================================

The blocking mode does not end, so it times out. Do you need to exit in callback?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.