[英]How to fix ‘io is not defined'
I am trying to integrate a socket.io into my project.我正在尝试将 socket.io 集成到我的项目中。 Code below works fine.
下面的代码工作正常。
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('someEvent', (data) => {
io.emit('someEvent2', data);
});
});
Everything worked well until I put the socket handler into a separate file.一切正常,直到我将套接字处理程序放入一个单独的文件中。
server.js服务器.js
const socketHandlers = require('./socket/handlers');
io.on('connection', socketHandlers);
handlers.js处理程序.js
const socketHandlers = (socket) => {
console.log('a user connected');
socket.on('someEvent', (data) => {
io.emit('someEvent2', data); ---here's a problem
});
};
module.exports = socketHandlers;
Now I get an error - io is not defined.现在我收到一个错误 - io 未定义。 What is the problem here?
这里有什么问题?
Maybe the following link can help you to get your answer , 也许以下链接可以帮助您获得答案,
link :
node.js /socket.io/socket.io.js not found link :
未找到node.js /socket.io/socket.io.js
Install socket.io 安装socket.io
npm i socket.io
and then use the below line 然后使用下面的行
var io = require('socket.io');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.