Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var methods = require('../methods/method');
- var router = express.Router();
- /* GET home page. */
- router.get('/', function(req, res, next) {
- res.render('page1.html', { title: 'Welcome to My World'} );
- });
- /* GET POST data form entry */
- router.post('/action', function(req, res) {
- methods.process(req, res);
- });
- module.exports = router;
- <script src="/javascripts/socket.io.js"></script>
- <script>
- var sock = io.connect('http://xx.xx.xxx.xx:xxxx', {'timeout' : 1000 , 'reconnection' : false, 'forceNew' : true});
- sock.on('start', function (data) {
- console.log('Start of arriving data');
- });
- sock.on('message', function (data) {
- console.log('Data arrived .. ');
- //code to render it in my current Web Page
- });
- sock.on('end', function (data) {
- console.log('my connection closed');
- sock.close();
- sock.disconnect();
- sock.destroy();
- });
- sock.on( 'disconnect', function() {
- sock.close();
- sock.disconnect();
- sock.destroy();
- console.log('my connection dropped');
- } );
- sock.on( 'reconnect', function() {
- sock.close();
- sock.disconnect();
- sock.destroy();
- console.log('my connection has been restored!');
- } );
- </script>
- exports.process = function(req, resp) {
- resp.render('page2.html', { title: 'Welcome to My World' });
- exec_script(resp);
- };
- exec_script = function(resp) {
- sock.on('connection', function (client) {
- client.emit('start');
- console.log('Connection Established');
- client.on('disconnect', function () {
- console.log('disconnected');
- });
- var pyshell = new PythonShell('./test.py', options ={ mode: 'text', pythonOptions: ['-u'] });
- pyshell.stdout.on('data', function(data) {
- client.emit('message', data);
- console.log(data);
- });
- pyshell.end(function(err) {
- if (err) throw err;
- console.log('End Script');
- client.emit('end', 'end');
- resp.end();
- });
- });
- };
- First Execution:
- Connection Established
- <output data of Python test.py for first execution>
- End Script
- disconnected
- Second Execution
- Connection Established
- Connection Established
- <output data of Python test.py for first execution>
- <output data of Python test.py for second execution>
- End Script
- End Script
- disconnected
- disconnected
- exports.process = function(req, resp) {
- resp.render('page2.html', { title: 'Welcome to My World' });
- var state = 'start'; // declared a new variable and initialized it as 'start'
- exec_script(resp, state);
- };
- exec_script = function(resp, state) {
- sock.on('connection', function (client) {
- console.log('State While Entering: ' + state);
- if (state === 'start')
- {
- console.log('State Inside if: ' + state);
- state = 'done';
- console.log('State After Changed: ' + state);
- client.emit('start');
- console.log('Connection Established');
- client.on('disconnect', function () {
- console.log('disconnected');
- });
- var pyshell = new PythonShell('./test.py', options ={ mode: 'text', pythonOptions: ['-u'] });
- pyshell.stdout.on('data', function(data) {
- client.emit('message', data);
- console.log(data);
- });
- pyshell.end(function(err) {
- if (err) throw err;
- console.log('End Script');
- client.emit('end', 'end');
- resp.end();
- });
- }
- });
- exec_script = function(resp, state) {
- sock.on('connection', function (client) {
- console.log('State While Entering: ' + state);
- if (state === 'start')
- {
- console.log('State Inside if: ' + state);
- state = 'done';
- console.log('State After Changed: ' + state);
- var pyshell = new PythonShell('./test.py', options ={ mode: 'text', pythonOptions: ['-u'] });
- pyshell.stdout.on('data', function(data) {
- client.emit('message', data);
- console.log(data);
- });
- .............
- }
- });
- First Execution:
- State While Entering: start
- State Inside if: start
- State After Changed: done
- Connection Established
- <output data of Python test.py for first execution>
- End Script
- disconnected
- Second Execution:
- State While Entering: done
- State While Entering: done
- State While Entering: start
- State Inside if: start
- State After Changed: done
- Connection Established
- <output data of Python test.py for second execution>
- End Script
- disconnected
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement