Как я могу запустить socket.write из module.export - PullRequest
0 голосов
/ 25 октября 2019

Эй, я хочу вызвать функцию в сокете из modules.exports, поэтому я хочу отправить глобальное сообщение с помощью метода write, но я не могу запустить извне функцию сокета

Я пытался запустить этот

const { TeamSpeak } = require("ts3-nodejs-library");
const dialog = require("./dialog");
const tcp = require("./tcpserver");
const random = require("./random");
const socket = net.createServer(function (socket) {   
...
socket.on("data", function (data) {

    if (data.toString() === "support_close") {
        ts3.support_close();
        socket.write("TS3 support wurde geschlossen!");
    }

    if (data.toString() === "support_open") {
        ts3.support_open();
        socket.write("TS3 support wurde geöffnet!");
    }

 });
}).listen(6969);

module.exports = {

  test: function (msg) {
    socket.write(msg);
  }

}

другой файл

 const { TeamSpeak } = require("ts3-nodejs-library");
 const dialog = require("./dialog");
 const tcp = require("./tcpserver");

 teamspeak.on("textmessage", function (data) {

  if (data.msg == 1) {

    data.invoker.move(110);
    data.invoker.message("Ein Supporter wird sich gleich um dich kümmern!");
    tcp.test("Der benutzer " + data.invoker.nickname + " hat den Warteberreich für Allgemeines Betreten!");
}

 });

, и я получил это сообщение об ошибке

(node:9120) UnhandledPromiseRejectionWarning: TypeError: tcp.test is not a function
    at TeamSpeak.<anonymous> (D:\SoftwareProjects\TS3supportBot\BotServerNodeJS\components\ts3query.js:71:13)
    at TeamSpeak.emit (events.js:210:5)
    at D:\SoftwareProjects\TS3supportBot\BotServerNodeJS\node_modules\ts3-nodejs-library\lib\TeamSpeak.js:125:19
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:9120) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9120) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
...