Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. var express = require('express');
  2. var methods = require('../methods/method');
  3. var router = express.Router();
  4.  
  5. /* GET home page. */
  6. router.get('/', function(req, res, next) {
  7. res.render('page1.html', { title: 'Welcome to My World'} );
  8. });
  9. /* GET POST data form entry */
  10. router.post('/action', function(req, res) {
  11. methods.process(req, res);
  12. });
  13. module.exports = router;
  14.  
  15. <script src="/javascripts/socket.io.js"></script>
  16. <script>
  17. var sock = io.connect('http://xx.xx.xxx.xx:xxxx', {'timeout' : 1000 , 'reconnection' : false, 'forceNew' : true});
  18. sock.on('start', function (data) {
  19. console.log('Start of arriving data');
  20. });
  21. sock.on('message', function (data) {
  22. console.log('Data arrived .. ');
  23. //code to render it in my current Web Page
  24. });
  25. sock.on('end', function (data) {
  26. console.log('my connection closed');
  27. sock.close();
  28. sock.disconnect();
  29. sock.destroy();
  30. });
  31. sock.on( 'disconnect', function() {
  32. sock.close();
  33. sock.disconnect();
  34. sock.destroy();
  35. console.log('my connection dropped');
  36. } );
  37. sock.on( 'reconnect', function() {
  38. sock.close();
  39. sock.disconnect();
  40. sock.destroy();
  41. console.log('my connection has been restored!');
  42. } );
  43. </script>
  44.  
  45. exports.process = function(req, resp) {
  46. resp.render('page2.html', { title: 'Welcome to My World' });
  47. exec_script(resp);
  48. };
  49.  
  50. exec_script = function(resp) {
  51. sock.on('connection', function (client) {
  52. client.emit('start');
  53. console.log('Connection Established');
  54. client.on('disconnect', function () {
  55. console.log('disconnected');
  56. });
  57. var pyshell = new PythonShell('./test.py', options ={ mode: 'text', pythonOptions: ['-u'] });
  58. pyshell.stdout.on('data', function(data) {
  59. client.emit('message', data);
  60. console.log(data);
  61. });
  62. pyshell.end(function(err) {
  63. if (err) throw err;
  64. console.log('End Script');
  65. client.emit('end', 'end');
  66. resp.end();
  67. });
  68. });
  69. };
  70.  
  71. First Execution:
  72.  
  73. Connection Established
  74. <output data of Python test.py for first execution>
  75. End Script
  76. disconnected
  77.  
  78. Second Execution
  79.  
  80. Connection Established
  81. Connection Established
  82. <output data of Python test.py for first execution>
  83. <output data of Python test.py for second execution>
  84. End Script
  85. End Script
  86. disconnected
  87. disconnected
  88.  
  89. exports.process = function(req, resp) {
  90. resp.render('page2.html', { title: 'Welcome to My World' });
  91. var state = 'start'; // declared a new variable and initialized it as 'start'
  92. exec_script(resp, state);
  93. };
  94.  
  95. exec_script = function(resp, state) {
  96. sock.on('connection', function (client) {
  97. console.log('State While Entering: ' + state);
  98. if (state === 'start')
  99. {
  100. console.log('State Inside if: ' + state);
  101. state = 'done';
  102. console.log('State After Changed: ' + state);
  103. client.emit('start');
  104. console.log('Connection Established');
  105. client.on('disconnect', function () {
  106. console.log('disconnected');
  107. });
  108. var pyshell = new PythonShell('./test.py', options ={ mode: 'text', pythonOptions: ['-u'] });
  109. pyshell.stdout.on('data', function(data) {
  110. client.emit('message', data);
  111. console.log(data);
  112. });
  113. pyshell.end(function(err) {
  114. if (err) throw err;
  115. console.log('End Script');
  116. client.emit('end', 'end');
  117. resp.end();
  118. });
  119. }
  120. });
  121.  
  122. exec_script = function(resp, state) {
  123. sock.on('connection', function (client) {
  124. console.log('State While Entering: ' + state);
  125. if (state === 'start')
  126. {
  127. console.log('State Inside if: ' + state);
  128. state = 'done';
  129. console.log('State After Changed: ' + state);
  130. var pyshell = new PythonShell('./test.py', options ={ mode: 'text', pythonOptions: ['-u'] });
  131. pyshell.stdout.on('data', function(data) {
  132. client.emit('message', data);
  133. console.log(data);
  134. });
  135. .............
  136. }
  137. });
  138.  
  139. First Execution:
  140.  
  141. State While Entering: start
  142. State Inside if: start
  143. State After Changed: done
  144. Connection Established
  145. <output data of Python test.py for first execution>
  146. End Script
  147. disconnected
  148.  
  149. Second Execution:
  150.  
  151. State While Entering: done
  152. State While Entering: done
  153. State While Entering: start
  154. State Inside if: start
  155. State After Changed: done
  156. Connection Established
  157. <output data of Python test.py for second execution>
  158. End Script
  159. disconnected
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement