【问题标题】:Pass data from HTTP to node.js to TCP?将数据从 HTTP 传递到 node.js 到 TCP?
【发布时间】:2016-04-14 17:33:47
【问题描述】:

我最近开始使用 Socket.io,结果是 node.js,我有点卡住了。我什至不知道这是否是我的应用程序的实用解决方案,但希望有人可以提供帮助。

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

我想知道如何使这个事件连续,以便 TCP 客户端不断接收复选框状态的更新。

如果有人有任何想法,请告诉我,很抱歉代码太长...

服务器代码:

var net = require('net');
var app = require('express')();         <!-- These are mandatory variables -->
var http = require('http').Server(app);
var io = require('socket.io')(http);
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
  });

});


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

客户端代码:

<!doctype html>
<html>
  <head>
    <title>Socket IO Test</title>

<form action="">
  <input type='checkbox' onclick='checkbox1(this);'>Checkbox1</label>
</form>

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
  var socket = io();
  var number = 0;

function checkbox1(cb) {
    socket.emit('checkbox1', 'checkbox 1 = ' + cb.checked);
    return false;
   }
</script>

  </body>
</html>

干杯

【问题讨论】:

    标签: javascript node.js sockets tcp socket.io


    【解决方案1】:

    我认为这里的问题是您无法引用 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')();         <!-- 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 io = require('socket.io')(http);到 var io = require('socket.io')(3000);?我试过了,但它确实将它记录在控制台中,并将它设置为 3000,但是当我将它改回 http 时,它会抛出一个“缺少错误处理程序”?有什么想法吗?
    • 没关系,您的多客户解决方案完美运行!非常感谢!
    猜你喜欢
    • 2020-05-31
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2011-04-30
    • 2020-04-11
    相关资源
    最近更新 更多