[英]Using Socket.io emit in a different module (Node.js)
I've been struggling to get this working for a day now, I'm hoping someone can help with some foundation for me as I've tried to read up on this with little success.我一直在努力让这个工作一天,我希望有人可以为我提供一些基础,因为我试图阅读这个但收效甚微。
Purpose of app: My app takes a username and password from a user, launches a module that runs puppeteer to automate tasks while providing the user with live updates on the puppeteer progress.应用程序的目的:我的应用程序从用户那里获取用户名和密码,启动一个运行 puppeteer 的模块以自动执行任务,同时为用户提供 puppeteer 进度的实时更新。
2 users connected at the same time can enter different credentials and puppeteer should run simultaneously outputting progress to that specific user of their job.同时连接的 2 个用户可以输入不同的凭据,并且 puppeteer 应该同时运行,向其工作的特定用户输出进度。
I'm using node.js + express + ejs + socket.io created with the express generator.我正在使用用 express 生成器创建的 node.js + express + ejs + socket.io。
The file structure looks like this:文件结构如下所示:
bin/
--www.js
app.js
scraper.js
views/
--index.js
routes/
--index.ejs
public/
...
I've got socket.io working in the www.js
module where the server is configured, what I've set up is where a user clicks the submit button on the front end, socket.io emits a unique message received (in www.js) and calls the function launchScraper
inside scraper.js
.我有 socket.io 在配置服务器的
www.js
模块中工作,我设置的是用户单击前端的提交按钮,socket.io 发出收到的唯一消息(在 www.js 中)和在 scraper.js 中调用scraper.js
launchScraper
。
I want launchScraper
to then emit the live updates to that individual user.我希望
launchScraper
然后向该个人用户发出实时更新。 ie logged in successfully, job done etc.即成功登录,完成工作等。
Question: I cannot seem to get scraper.js to emit the message to that unique user (using their socket.id
), I think I'm missing some theory.问题:我似乎无法让 scraper.js 向该唯一用户发送消息(使用他们的
socket.id
),我想我错过了一些理论。 Here's my code:这是我的代码:
www.js www.js
const scraper = require('../scraper');
const socketio = require('socket.io');
...
const server = http.createServer(app);
const io = socketio(server);
...
io.on('connection', (socket) => {
console.log('New connection: '+ socket.id);
...
socket.on('launchScraper', (socket) =>{
console.log('received client launchScraper');
scraper.launchScraper(io,userCredentials);
})
scraper.js刮板.js
const puppeteer = require('puppeteer');
//This emits the message to all users, but I've tried variations sending `socket.id` in etc and I cannot seem to get it to work.
function emitMessage(io,msg){
io.emit('message',msg);
console.log(msg);
}
async function launchScraper(io,userData) {
emitMessage(io, 'Launching Scraper...'); //Sending io in
const browser = await puppeteer.launch({..});
const page = await browser.newPage();
...
Can anyone help or suggest a better way to do this?任何人都可以帮助或建议更好的方法来做到这一点吗? I just don't want a lot of code is the www.js as I don't believe the code should live there..
我只是不想要很多代码是 www.js,因为我不相信代码应该在那里..
I'm a newbie on Node and I'm struggling to understand instances, constructors etc.我是 Node 上的新手,我正在努力理解实例、构造函数等。
Turns out I needed to send the "socket" with my request and not the "io" instance.原来我需要用我的请求发送“socket”而不是“io”实例。
Doing that seemed to resolve it.这样做似乎可以解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.