Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. var app = require('http').createServer(handler),
  2. io = require('socket.io').listen(app),
  3. fs = require('fs'),
  4. express=require('express'),
  5. session=require('express-session'),
  6. mysql = require('mysql'),
  7. execPhp = require('exec-php'),
  8. connectionsArray = [],
  9. connection = mysql.createConnection({
  10. host: 'localhost',
  11. user: 'root',
  12. password: '',
  13. database: 'test',
  14. port: 3306
  15. }),
  16. POLLING_INTERVAL = 3000,
  17. pollingTimer;
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. // If there is an error connecting to the database
  25. connection.connect(function(err) {
  26. // connected! (unless `err` is set)
  27. if (err) {
  28. console.log(err);
  29. }
  30. });
  31.  
  32. // creating the server ( localhost:8000 )
  33. app.listen(8000);
  34.  
  35. // on server started we can load our client.html page
  36. function handler(req, res) {
  37. fs.readFile(__dirname + '/client.php', function(err, data) {
  38. if (err) {
  39. console.log(err);
  40. res.writeHead(500);
  41. return res.end('Error loading client.php');
  42. }
  43. res.writeHead(200);
  44. res.end(data);
  45. });
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. execPhp('k.php', function(error, php, outprint){
  53. // Here I expected The outprint Will be 'One' but it print undefined
  54. console.log(outprint);
  55. php.my_function(1, 2, function(err, result, output, printed){
  56. //this my_function is also showing Error
  57. });
  58. });
  59.  
  60.  
  61. var pollingLoop = function() {
  62.  
  63.  
  64.  
  65. // Doing the database query
  66. var query = connection.query('SELECT * FROM users where user_id=1'),
  67. users = []; // this array will contain the result of our db query
  68.  
  69. // setting the query listeners
  70. query
  71. .on('error', function(err) {
  72. // Handle error, and 'end' event will be emitted after this as well
  73. console.log(err);
  74. updateSockets(err);
  75. })
  76. .on('result', function(user) {
  77. // it fills our array looping on each user row inside the db
  78. users.push(user);
  79. })
  80. .on('end', function() {
  81. // loop on itself only if there are sockets still connected
  82. if (connectionsArray.length) {
  83.  
  84. pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
  85.  
  86. updateSockets({
  87. users: users
  88. });
  89. } else {
  90.  
  91. console.log('The server timer was stopped because there are no more socket connections on the app')
  92.  
  93. }
  94. });
  95. };
  96.  
  97.  
  98. // creating a new websocket to keep the content updated without any AJAX request
  99. io.sockets.on('connection', function(socket) {
  100.  
  101. console.log('Number of connections:' + connectionsArray.length);
  102. // starting the loop only if at least there is one user connected
  103. if (!connectionsArray.length) {
  104. pollingLoop();
  105. }
  106.  
  107. socket.on('disconnect', function() {
  108. var socketIndex = connectionsArray.indexOf(socket);
  109. console.log('socketID = %s got disconnected', socketIndex);
  110. if (~socketIndex) {
  111. connectionsArray.splice(socketIndex, 1);
  112. }
  113. });
  114.  
  115. console.log('A new socket is connected!');
  116. connectionsArray.push(socket);
  117.  
  118. });
  119.  
  120. var updateSockets = function(data) {
  121. // adding the time of the last update
  122. data.time = new Date();
  123. console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
  124. // sending new data to all the sockets connected
  125. connectionsArray.forEach(function(tmpSocket) {
  126. tmpSocket.volatile.emit('notification', data);
  127. });
  128. };
  129.  
  130. console.log('Please use your browser to navigate to http://localhost:8000');
  131.  
  132. execPhp('k.php', function(error, php, outprint){
  133. // Here I expected The outprint Will be 'One' but it print undefined
  134. console.log(outprint);
  135. php.my_function(1, 2, function(err, result, output, printed){
  136. //this my_function is also showing Error
  137. });
  138. });
  139.  
  140. <?php
  141.  
  142.  
  143.  
  144.  
  145. echo "One";
  146. function my_function($arg1, $arg2){
  147. echo "Two";
  148. return $arg1 + $arg2;
  149. }
  150.  
  151.  
  152.  
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement