我想使用Node.js将数据从许多客户端发送到一个稳定的客户端

时间:2016-02-21 06:42:13

标签: node.js express socket.io openshift openshift-cartridge

这是我的代码:

var express = require('express');
var net = require('net');
var app = express();


//first of all connect to a stable client
var server = net.createServer(function(socket) {
// do nothing here . what i want is to use this socket in 
// (following) app.get() function
});
server.listen(1337, '127.0.0.1');

//receive request from other clients
app.get('/', function (req, res) 
{
// retriving mobileNumber and message
var mobileNumber = req.query.mobileNumber;
var message      = req.query.message;

//now i want to send this data to the stable client
// to which i have connected earlier.
res.end();
});

app.listen(6544);

每当有新的'/'请求到达时,我想将数据发送到先前连接的套接字。

1 个答案:

答案 0 :(得分:0)

通过一个变量var sock执行此操作的方法之一。

var sock;
var server = net.createServer(function(socket) {
   sock = socket;
});
server.listen(1337, '127.0.0.1');

app.get('/', function (req, res) 
{
    if (sock) {
        // use sock here
    }
});