简体   繁体   中英

How can I send data from javascript to a node.js TCP server?

I'm currently trying to write a plugin for the StreamDeck to send TCP messages to a computer on the same network without an Internet connection. So I have to send a specific message when the button is pressed but I don't know how to pass the data from my javascript file to my node.js TCP server . Can someone help me with that, please?

Yes sorry, so in my JS code I'm sending a HTTP request when the user pressed the key:

onKeyUp: async function (jsn, mode) {
    const order = structureMessage(jsn, mode);
    console.log(order);
    try {
        console.log(`[onKeyUp] ${JSON.stringify(jsn)}`);
        const raw = await fetch(jsn.payload.settings.targetUrl, {
            method: "POST", 
            headers: {
                "content-type": "application/json"
            },
            body: JSON.stringify({message: order})
        })
        const respData = await raw.text();
        console.log(respData);
    }
    catch(error) {
        console.error(error);
    }
}

the function structureMessage will get me a string who corresponds to a message with the CII protocol.

And this is the code of my server :

const host = '127.0.0.1';
const HTTPport = '8080';
const TCPport = '8081';

const CIIconnection = require('./connection.js');
const CII = new CIIconnection(host, TCPport);

const net = require('net');
const serverTCP = new net.createServer();
let sockets = [];
serverTCP.on('connection', (socket) => {
  console.log('new Client');
  sockets.push(socket);
  socket.setEncoding("utf8");

  socket.on('data', (data) => {
    console.log('receive data from socket : ', data);
    socket.write('*ok\r\n')
  });
  socket.on('end', function () {
    sockets.splice(sockets.indexOf(socket), 1);
    console.log('socket end');
  });
});
serverTCP.listen(TCPport, host, () => {
  console.log(`Server TCP running at http://${host}:${TCPport}`);
});

const express = require('express');
const appHTTP = express();
appHTTP.use(express.urlencoded({ extended: true }));
appHTTP.use(express.json());
appHTTP.post('/myaction', async function (req, res) {
  res.send('You sent "' + req.body + '".');
  try {
    await CII.sendCommand(req.body.message);
    console.log('commande envoyé')
  } catch (error) {
    console.error(error);
  }
});
appHTTP.listen(HTTPport, host, async () => {
  console.log(`Server HTTP running at http://${host}:${HTTPport}`);
  await init();
});

async function init() {
  try {
    await CII.connect();
  } catch (e) {
    console.error(e);
  }
}

What I want is to directly send data to the TCP client that I'm creating and don't use the HTTP server. Is it possible ?

I'm a newbie so I don't know if you will understand this code but thanks anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM