Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据从HTTP传递到node.js到TCP?_Javascript_Node.js_Sockets_Tcp_Socket.io - Fatal编程技术网

Javascript 将数据从HTTP传递到node.js到TCP?

Javascript 将数据从HTTP传递到node.js到TCP?,javascript,node.js,sockets,tcp,socket.io,Javascript,Node.js,Sockets,Tcp,Socket.io,因此,我最近开始使用Socket.io和node.js,我有点被卡住了。我甚至不知道这对我的应用程序来说是否是一个实用的解决方案,但希望有人能帮助我 我这里只有一个带有复选框的网页,它向节点控制台报告它的状态,然后当TCP客户端连接时,它也会收到状态 我想知道如何使此事件连续进行,以便TCP客户端不断接收有关复选框状态的更新 如果有人有任何想法,请让我知道,并为漫长的代码抱歉 服务器代码: var net=require('net'); var app=require('express')();

因此,我最近开始使用Socket.io和node.js,我有点被卡住了。我甚至不知道这对我的应用程序来说是否是一个实用的解决方案,但希望有人能帮助我

我这里只有一个带有复选框的网页,它向节点控制台报告它的状态,然后当TCP客户端连接时,它也会收到状态

我想知道如何使此事件连续进行,以便TCP客户端不断接收有关复选框状态的更新

如果有人有任何想法,请让我知道,并为漫长的代码抱歉

服务器代码:
var net=require('net');
var app=require('express')();
var http=require('http')。服务器(应用程序);
var io=require('socket.io')(http);
var HOST='localhost';
var端口=4040;
GLOBAL.MYVAR=“你好世界”;
var server=net.createServer();
监听(端口、主机);
app.get('/',函数(req,res){
//发送所有请求的index.html文件
res.sendFile(uu dirname+'/index.html');
});
http.listen(3001,函数(){
log('在*:3001上侦听HTTP');
log('在端口'+port'上侦听TCP);
});
io.on('connection',function(socket)//打开套接字
{ 
on('checkbox1',函数(msg){//创建一个事件
console.log(msg);//在控制台中显示消息
MYVAR=msg;//将全局变量设置为接收到的消息的内容
});
});
on('connection',function(socket){//打开TCP连接的套接字
socket.write(MYVAR);
}).侦听(端口、主机);
客户端代码:

插座IO测试
复选框1
var socket=io();
var数=0;
功能检查框1(cb){
发出('checkbox1','checkbox1='+cb.checked);
返回false;
}

干杯

我认为这里的问题是您没有引用TCP套接字的方法。一旦你有了参考资料,你就可以很容易地收到并发送一条信息

这将适用于单个客户端

    var net = require('net');
    var app = require('express')();         <!-- These are mandatory variables -->
    var http = require('http').Server(app);
    var io = require('socket.io')(3000);
    var s;
    var HOST = 'localhost';
    var PORT = 4040;
    GLOBAL.MYVAR = "Hello world";
    var server = net.createServer();
    server.listen(PORT, HOST);


    app.get('/', function(req, res){           <!-- This sends the html file -->
    //send the index.html file for all requests
      res.sendFile(__dirname + '/index.html');

    });

    http.listen(3001, function(){    <!-- Tells the HTTP server which port to use -->

      console.log('listening for HTTP on *:3001');   <!-- Outputs text to the console -->
      console.log('listening for TCP on port ' + PORT);
    });

    <!-- everything below this line are actual commands for the actual app -->

    io.on('connection', function(socket) // Opens the socket

    {
      socket.on('checkbox1', function(msg){  // Creates an event
         console.log(msg); // displays the message in the console
         MYVAR = msg; // Sets the global variable to be the contents of the message recieved
         s.write(MYVAR, 'utf-8');
      });

    });


    server.on('connection', function(socket){ // Opens the socket for the TCP connection
        s = socket;
        s.write(MYVAR, 'utf-8');
    }).listen(PORT, HOST);
var net=require('net');
var app=require('express')();
var http=require('http')。服务器(应用程序);
var io=require('socket.io')(3000);
var s;
var HOST='localhost';
var端口=4040;
GLOBAL.MYVAR=“你好世界”;
var server=net.createServer();
监听(端口、主机);
app.get('/',函数(req,res){
//发送所有请求的index.html文件
res.sendFile(uu dirname+'/index.html');
});
http.listen(3001,函数(){
log('在*:3001上侦听HTTP');
log('在端口'+port'上侦听TCP);
});
io.on('connection',function(socket)//打开套接字
{
on('checkbox1',函数(msg){//创建一个事件
console.log(msg);//在控制台中显示消息
MYVAR=msg;//将全局变量设置为接收到的消息的内容
s、 写入(MYVAR,'utf-8');
});
});
on('connection',function(socket){//打开TCP连接的套接字
s=插座;
s、 写入(MYVAR,'utf-8');
}).侦听(端口、主机);
这将适用于多个客户端

    var net = require('net');
    var app = require('express')();         <!-- These are mandatory variables -->
    var http = require('http').Server(app);
    var io = require('socket.io')(3000);
    var sockets = [];
    var HOST = 'localhost';
    var PORT = 4040;
    GLOBAL.MYVAR = "Hello world";
    var server = net.createServer();
    server.listen(PORT, HOST);


    app.get('/', function(req, res){           <!-- This sends the html file -->
    //send the index.html file for all requests
      res.sendFile(__dirname + '/index.html');

    });

    http.listen(3001, function(){    <!-- Tells the HTTP server which port to use -->

      console.log('listening for HTTP on *:3001');   <!-- Outputs text to the console -->
      console.log('listening for TCP on port ' + PORT);
    });

    <!-- everything below this line are actual commands for the actual app -->

    io.on('connection', function(socket) // Opens the socket

    {
      socket.on('checkbox1', function(msg){  // Creates an event
         console.log(msg); // displays the message in the console
         MYVAR = msg; // Sets the global variable to be the contents of the message recieved
         for (var i = 0; i < sockets.length; i++) {
          if(sockets[i]) {
            sockets[i].write(MYVAR, 'utf-8');
          }
        }
      });

    });


    server.on('connection', function(socket){ // Opens the socket for the TCP connection
        sockets.push(socket);
        socket.write(MYVAR, 'utf-8');
    }).listen(PORT, HOST);
var net=require('net');
var app=require('express')();
var http=require('http')。服务器(应用程序);
var io=require('socket.io')(3000);
var套接字=[];
var HOST='localhost';
var端口=4040;
GLOBAL.MYVAR=“你好世界”;
var server=net.createServer();
监听(端口、主机);
app.get('/',函数(req,res){
//发送所有请求的index.html文件
res.sendFile(uu dirname+'/index.html');
});
http.listen(3001,函数(){
log('在*:3001上侦听HTTP');
log('在端口'+port'上侦听TCP);
});
io.on('connection',function(socket)//打开套接字
{
on('checkbox1',函数(msg){//创建一个事件
console.log(msg);//在控制台中显示消息
MYVAR=msg;//将全局变量设置为接收到的消息的内容
对于(变量i=0;i
首先非常感谢您的帮助,但是为什么要更改var io=require('socket.io')(http);to var io=require('socket.io')(3000);?我试过了,但它确实会在控制台中记录它,并将其设置为3000,但当我将其更改回http时,它会抛出一个“丢失的错误处理程序”?有什么想法吗?没关系,您针对多个客户的解决方案非常有效!非常感谢你!
    var net = require('net');
    var app = require('express')();         <!-- These are mandatory variables -->
    var http = require('http').Server(app);
    var io = require('socket.io')(3000);
    var s;
    var HOST = 'localhost';
    var PORT = 4040;
    GLOBAL.MYVAR = "Hello world";
    var server = net.createServer();
    server.listen(PORT, HOST);


    app.get('/', function(req, res){           <!-- This sends the html file -->
    //send the index.html file for all requests
      res.sendFile(__dirname + '/index.html');

    });

    http.listen(3001, function(){    <!-- Tells the HTTP server which port to use -->

      console.log('listening for HTTP on *:3001');   <!-- Outputs text to the console -->
      console.log('listening for TCP on port ' + PORT);
    });

    <!-- everything below this line are actual commands for the actual app -->

    io.on('connection', function(socket) // Opens the socket

    {
      socket.on('checkbox1', function(msg){  // Creates an event
         console.log(msg); // displays the message in the console
         MYVAR = msg; // Sets the global variable to be the contents of the message recieved
         s.write(MYVAR, 'utf-8');
      });

    });


    server.on('connection', function(socket){ // Opens the socket for the TCP connection
        s = socket;
        s.write(MYVAR, 'utf-8');
    }).listen(PORT, HOST);
    var net = require('net');
    var app = require('express')();         <!-- These are mandatory variables -->
    var http = require('http').Server(app);
    var io = require('socket.io')(3000);
    var sockets = [];
    var HOST = 'localhost';
    var PORT = 4040;
    GLOBAL.MYVAR = "Hello world";
    var server = net.createServer();
    server.listen(PORT, HOST);


    app.get('/', function(req, res){           <!-- This sends the html file -->
    //send the index.html file for all requests
      res.sendFile(__dirname + '/index.html');

    });

    http.listen(3001, function(){    <!-- Tells the HTTP server which port to use -->

      console.log('listening for HTTP on *:3001');   <!-- Outputs text to the console -->
      console.log('listening for TCP on port ' + PORT);
    });

    <!-- everything below this line are actual commands for the actual app -->

    io.on('connection', function(socket) // Opens the socket

    {
      socket.on('checkbox1', function(msg){  // Creates an event
         console.log(msg); // displays the message in the console
         MYVAR = msg; // Sets the global variable to be the contents of the message recieved
         for (var i = 0; i < sockets.length; i++) {
          if(sockets[i]) {
            sockets[i].write(MYVAR, 'utf-8');
          }
        }
      });

    });


    server.on('connection', function(socket){ // Opens the socket for the TCP connection
        sockets.push(socket);
        socket.write(MYVAR, 'utf-8');
    }).listen(PORT, HOST);