我无法获取将服务器调用数据发送给客户端的模块(请参见第36行),以便客户端可以随后返回传感器数据。
我已经没有想法了,尝试了很多事情。
如果我取消注释“ use strict”,为什么将console.log称为错误?
注意:我已在开发目录中包含文件.jshintrc,以确保ES6受到验证。
代码如下;
// See https://techbrij.com/node-js-tcp-server-client-promisify
//'use strict';
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;
调用模块如下;
// To test server, use following code in another file and run it
const Server = require('./ServerCommsV05.js');
new Server();