开发者

Sending contents of var within http.request area through socket

开发者 https://www.devze.com 2023-03-29 01:43 出处:网络
I have a program written with node that connects to Flash. In Flash when a key is pressed the keypress is sent through a socket to node, which then echoes the keypress back to Flash with some extra te

I have a program written with node that connects to Flash. In Flash when a key is pressed the keypress is sent through a socket to node, which then echoes the keypress back to Flash with some extra text. The Flash part works fine. The node portion basically works, except for one part where I connect to a web page. What I want to do is get the contents of a web page and echo that back to Flash though a socket. Here's my code:

var net = require('net');
var http = require('http'),
    sys = require('sys'),
    fs = require('fs');

var options = {
  host: 'theurl.com',
  port: 80,
  path: '/',
  method: 'POST'
};  

var msg;
var mySocket;

var server = net.createServer(function(socket) {
    mySocket = socket;
    mySocket.on("connect", onConnect);
    mySocket.on("data", onData);
});

function onConnect() {
    console.log("Connected to Flash");
}

function onData(d) {

    if(d == "exit\0") {
        console.log("exit");
        mySocket.end();
        server.close();
    } else if(d == "32\0") {
        var req = http.request(options, function(res) {
          res.setEncoding('utf8');
          res.on('data', function (chunk) {
            msg = chunk;
            console.log("in function: msg = " + msg);
            mySocket.write(msg, 'utf8');
          });
        });
        console.log("right after res: msg = " + msg);
        mySocket.write("right after res: "+msg, 'utf8');

        // write data to request body
        req.write('data\n');
        req.write('data\n');
        req.end();

        mySocket.write("32 part: " + d, 'utf8');
    } else {
        console.log("From Flash = " + d );
        mySocket.write(d, 'utf8');
    }
}

server.listen(8080, "192.168.0.1");

When I press spacebar (no 32) the data seems to be written but doesn't show up on the Flash side until I press 2 keys. Note that the part outside of the "http.request" area DOES show up on the Flash side correctly.开发者_Go百科 So the mysocket.write within the res.on area doesn't show up until 2 keypresses, but the mysocket.write that says "right after res" shows up just like I want it to. SO I figure that I need one of these things to happen:

1) The chunk returned from the http.request area be visible outside of that area 2) the mysocket.write within the http.request area to work the same way as the mysocket.write outside of the http.request area 3) store the chunk to the hard drive and then reread the contents stored later and send that

Those are the options I've thought of. I think #3 makes no sense since I seem to have the var in the program, but just can't access it where I want to. Not sure about why the socket doesn't write the same within that area as it does outside of that area, but if there were a way to do #1 that seems to me to make the most sense. I am open to suggestions though. Thanks for your time.

Darryl

0

精彩评论

暂无评论...
验证码 换一张
取 消