2014-12-28 2 views
0

가상 사설 서버를 통해 스마트 폰에서 라즈베리에게 명령을 보내야합니다. 이 작업을 위해 Node.js를 사용하려고합니다.websocket에서받은 값을 다른 websocket으로 전달하는 방법은 무엇입니까?

스마트 폰과 라즈베리 (클라이언트)에서 서버로 메시지/요청을 보낼 수 있지만 안드로이드 앱에서받은 메시지를 라즈베리로 전달할 수는 없습니다. 모바일 앱에서 전송 명령은 그렇게 저장됩니다

var http = require("http") 
, fs = require('fs') 
, ursa = require('ursa') 
, url = require('url') 
, querystring = require('querystring') 
, net = require('net') 
, io = require('socket.io').listen(80) 
, crt 
, key 
, msg 
, id 
; 

io.sockets.on('connection', function (socket) { 
    socket.emit('news', { hello: 'synchronized' }); 

var server = http.createServer(function(request, response){ 
    console.log('Connection'); 
    var path = url.parse(request.url).pathname; 

    switch(path){ 
     case '/inst': 
      var req = request; 

       if (req.method == 'POST') { 
        console.log("[200] " + req.method + " to " + req.url); 
        var fullBody = ''; 
        req.on('data', function(chunk) { 

          // append the current chunk of data to the fullBody variable 
          fullBody += chunk.toString(); 
        }); 
        req.on('end', function() { 
          // parse the received body data 
          var jsonObject = querystring.parse(fullBody); 

          msg = key.decrypt(jsonObject.cr, 'base64', 'utf8'); 
          console.log(msg); 
        if(msg){ 
          response.writeHead(200, {'Content-Type': 'text/html'}); 
          response.write('all ok '); 
          } 
        }); 
      } 

      break; 
     case '/app': //Commands received from android "app" 
       var req = request; 

       if (req.method == 'POST') { 
        console.log("[200] " + req.method + " to " + req.url); 
        var fullBody = ''; 
        req.on('data', function(chunk) { 

         // append the current chunk of data to the fullBody variable 
         fullBody += chunk.toString(); 
        }); 
        req.on('end', function() { 
         // parse the received body data 
         var jsonObject = querystring.parse(fullBody); 
         console.log(fullBody); 
         //TODO remove all console.log 
         console.log(jsonObject.food); 
        }); 
      } 
     break; 

     default: 
      response.writeHead(404); 
      response.write("opps this doesn't exist - 404"); 
      break; 
     } 
     response.end(); 
    }); 
    server.listen(8002); 

    socket.on('my other event', function (data) { 
    console.log(data); 
    }); 
}); 

답변

1

난 아직도 소켓이 어떻게 작동하는지에 조금 혼란 스러워요 :

var jsonObject = querystring.parse(fullBody);

이 전체 스크립트입니다.

case '/app': //Commands received from android "app" 
     var req = request; 

     if (req.method == 'POST') { 
      console.log("[200] " + req.method + " to " + req.url); 
      var fullBody = ''; 
      req.on('data', function(chunk) { 
       // append the current chunk of data to the fullBody variable 
       fullBody += chunk.toString(); 
       }); 
      req.on('end', function() { 
       // parse the received body data 
       var jsonObject = querystring.parse(fullBody); 

       //****Added emit socket event**** 
       socket.emit('message', {'message':jsonObject }); 

       }); 
     } 
break; 
: 내 스크립트에서

내가 단순히 "/ 응용 프로그램"스위치 케이스에에 "방출"이벤트를 추가 위

관련 문제