使用TCP Node.JS从服务器创建到客户端的连接和调用

时间:2019-04-29 14:19:00

标签: node.js tcp module

这是三层客户端服务器架构(利用拉取请求)架构的中间层(即comms层),用于我正在从事家庭用途的IoT项目。

但是我无法请求服务器向客户端发送数据。

我尝试了很多事情,但现在陷入了困境。非常感谢您的帮助和指导

/*
*  Module Name : CommsLayer.js
*
* This is the communications layer of the three tier Server Client Architecture.
*
*  The following is the algorithm for this module
*
*  CreateServer(IPAddress, port)
*  ReceiveClientIP(clientIP, port, jobID)
*  ConnectToClient(clientIP, port, jobID)
*  ServerSendsJobRequest(clientIP, jobID)
*  ClientReturns(clientIP, jobID, sensorData)
*  CloseClientConnetion(clientIP)
*
*  Notes
*    1) The ClientComms() object (see below) will be passed into this module from main.js.
*
*       var ClientComms = {  // Client Comms Data
*         clientIP    :"192.168.100.101",  // This is just an example IP address, used for testing purposes.
*         clientPort  : 9000
*       };  // End ClientComms
*       Note : This will be JSON data.
*
*
*    2) The following data will be returned from the client (see client code listing)
*
*      typedef struct // Sensor Data to send to Server
*      {
*        String   clientIP;
*        int      jobID;
*        string   SensorData;
*      }  // End SensorData
*      Note : This will be JSON data.
*
*
*    3) I have 'dummied' the following components;
*        ClientComms( ... ) object - for current testing purposes - feel free to update this.
*
*
*    4) I have written this Node.JS code to comply with ES6 .
*
*/

//'use strict';  // For some reason when I uncomment this line console.log becomes a compiler error - don't know why?

const ServerComms = {  // Server Comms Data
  serverIP    :"192.168.100.199",
  serverPort  : 9000
};  // End ClientComms

var ClientComms = {  // Client Comms Data
  clientIP    :"192.168.100.101",
  clientPort  : 9000
};  // End ClientComms

var ServerCall = {  // Server job call to client
  clientIP    : ClientComms.clientIP, // '192.168.100.101', Same as ClientComms.clientIP
  jobID       : 1
};  // End ServerCall

const net = require('net');

class MyServer {
  constructor() {
    this.address  = ServerComms.serverIP;
    this.port     = ServerComms.serverPort;
  this.init();
  }  // End constructor


  init() {
    let server = this;

    let onClientConnected = (socket) => {

      console.log(`Sending request job to RSU : ${ServerComms.serverPort} ${ServerComms.serverIP}`);
      server.socket.write( ServerCall.clientIP, ServerCall.jobID );  // Send request and data to client so that client can respond with remote sensor data.


      let clientName = `${ClientComms.clientIP} : ${ClientComms.clientPort}`;
      console.log(`New client is connected: ${clientName}`);

      socket.on('data', (clientData) => {
        console.log(`${clientName} Says: ${clientData}`);
        socket.write(clientData);
        socket.write('exit');
      });

      socket.only('close', () => {
        console.log(`Connection from ${clientName} closed`);
      });

      socket.on('error', (err) => {
        console.log(`Connection ${clientName} error: ${err.message}`);
      });

    };  // End onClientConnect.

    //server.connection = net.createServer(onClientConnected);
    server.connection = net.createServer(  onClientConnected, function(){
      console.log(`Server created at: ${ServerComms.serverIP} : ${ServerComms.serverPort}`);
    });

    server.connection.listen(ServerComms.serverPort, ServerComms.serverIP, function() {
      console.log(`Server started at: ${ServerComms.serverIP} : ${ServerComms.serverPort}`);
    });  // End server.connection.listen

  }  // End init

}  // End class Server

module.exports = MyServer;

0 个答案:

没有答案