看下面的例子:
var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});
我想有一种从createServer()
函数外部访问 c.write 的方法
基本上我想做的是:
app.get('/msg/:theMsg', function (req, res) {
res.setHeader('Content-Type', 'application/json');
server.c.write(req.params.theMsg, socketName); // i got socketName stored in an array
});