我想使用Node.js将数据从许多客户端发送到一个稳定的客户端 [英] I want to send data from many clients to one stable client using Node.js

查看:89
本文介绍了我想使用Node.js将数据从许多客户端发送到一个稳定的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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);

每当新的"/"请求到达时,我都希望将数据发送到先前连接的套接字.

I want to send data to the previously connected socket whenever a new '/' request arrives.

推荐答案

一种通过波纹管变量var sock做到这一点的方法.

One way to do that through one variable var sock as bellow.

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
    }
});

这篇关于我想使用Node.js将数据从许多客户端发送到一个稳定的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆