如何在函数中 return websocket 消息?
How to return websocket message in functions?
我正在尝试创建一个函数,该函数 return 从 WebSocket 服务器传入信息。
我希望它在完成后在主文件中看起来像这样:
const getMessage = require('./getMessage.js');
let message = getMessage();
console.log(message);
现在我的问题是我不知道如何return 来自事件侦听器的传入消息
websocket.on('message', function incoming(reply) {
});
我的 getMessage.js 文件如下所示:
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost');
module.exports = function getMessage() {
let msg;
ws.on('message', function incoming(reply) {
msg = reply;
});
return msg;
}
但不是 return 传入消息,而是 return 未定义。如何在一个函数中获取 Websocket 服务器的传入消息?
index.js当前代码:
const api = require('./api.js');
let message = api();
console.log(messag);
api.js当前代码:
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost');
let api = (function(msg) {
console.log(msg);
return msg;
});
ws.on('message', function msg(reply) {
api(reply);
});
如果我 运行 它记录的程序:
undefined
undefined
Hello
您不能调用 api
之类的函数来从 websocket 获取消息。 Websockets 本质上是异步的,所以你的 incoming()
应该调用另一个函数,或者设置变量。
也许可以尝试添加一个可以接收消息的回调函数。
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost');
module.exports = function api(onMessageReceived) {
if (typeof onMessageReceived !== 'function') {
throw new Error('onMessageReceived must be a callback function');
}
ws.on('message', function(reply){
onMessageReceived(reply);
});
};
编辑:
您不想将 api
函数括在括号中,也不需要它是一个变量。试试更像:
function api(msg) {
console.log(msg);
// "return"ing a value doesn't do anything, it is unneeded.
}
ws.on('message', function msg(reply) {
if (reply) {
api(reply);
}
});
我正在尝试创建一个函数,该函数 return 从 WebSocket 服务器传入信息。
我希望它在完成后在主文件中看起来像这样:
const getMessage = require('./getMessage.js');
let message = getMessage();
console.log(message);
现在我的问题是我不知道如何return 来自事件侦听器的传入消息
websocket.on('message', function incoming(reply) {
});
我的 getMessage.js 文件如下所示:
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost');
module.exports = function getMessage() {
let msg;
ws.on('message', function incoming(reply) {
msg = reply;
});
return msg;
}
但不是 return 传入消息,而是 return 未定义。如何在一个函数中获取 Websocket 服务器的传入消息?
index.js当前代码:
const api = require('./api.js');
let message = api();
console.log(messag);
api.js当前代码:
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost');
let api = (function(msg) {
console.log(msg);
return msg;
});
ws.on('message', function msg(reply) {
api(reply);
});
如果我 运行 它记录的程序:
undefined
undefined
Hello
您不能调用 api
之类的函数来从 websocket 获取消息。 Websockets 本质上是异步的,所以你的 incoming()
应该调用另一个函数,或者设置变量。
也许可以尝试添加一个可以接收消息的回调函数。
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost');
module.exports = function api(onMessageReceived) {
if (typeof onMessageReceived !== 'function') {
throw new Error('onMessageReceived must be a callback function');
}
ws.on('message', function(reply){
onMessageReceived(reply);
});
};
编辑:
您不想将 api
函数括在括号中,也不需要它是一个变量。试试更像:
function api(msg) {
console.log(msg);
// "return"ing a value doesn't do anything, it is unneeded.
}
ws.on('message', function msg(reply) {
if (reply) {
api(reply);
}
});