Advertisement
Weslei_Ramos

Servidor

Sep 14th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs');
  2. var webSocket = require('ws');
  3. var child = require('child_process');
  4.  
  5. var tempFileNum = 0;
  6. var interpretadorBirl = 'C:\\birlscript.exe';
  7. var webSocketServer = new webSocket.Server({port:8080});
  8.  
  9. webSocketServer.on('connection', function(cliente) {
  10.     cliente.on('message', function(message) {
  11.         var json = JSON.parse(message);
  12.  
  13.         if (json.hasOwnProperty('codigo')) {
  14.  
  15.             var arquivo = 'temp' + (tempFileNum++) + '.birl';
  16.  
  17.             fs.writeFile(arquivo, json['codigo'], function(err) {
  18.                 if (err) return console.log(err);
  19.  
  20.                 var processo = child.execFile(interpretadorBirl, [arquivo]);
  21.  
  22.                 processo.stdout.on('data', function(data) {
  23.                     cliente.send(JSON.stringify({
  24.                         output: data.toString()
  25.                     }));
  26.                     fs.unlink(arquivo);
  27.                 });
  28.             });
  29.         }
  30.     });
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement