问题描述
我正在尝试在Node.js和Express应用程序的路由内使用io.sockets.on。我一直在关注这里所说的话:
我可以成功发送io.sockets.emit事件,但是无法接收到io.sockets.on事件。
这里是我的代码:
index.js:
const cors = require('cors');
const express = require('express');
const app = express();
const server = require(’http’)。Server(app);
const io = require(’socket.io’)(服务器);
const taskRequest = require('./ routes / taskRequest');
app.use(cors())
app.use(express.json());
app.use(’/ api / taskRequest',taskRequest);
app.set('socketio',io);
server.listen(4002);
io.sockets.on( connection,function(socket){
console.log( connected);
socket.on( connected,function(数据){
console.log( hello);
})
});
routes / taskRequest.js:
const express = require('express');
const router = express.Router();
router.post('/',异步(req,res)=> {
var io = req.app.get('socketio');
// pickedUser是连接的客户端
之一var pickledUser = JZLpeA4pBECwbc5IAAAA;
//我们仅将发射事件发送给pickledUser
io .to(pickedUser).emit('taskRequest',req.body);
io.on('connection',function(socket){
console.log('connected 2' );
socket.on('taskResponse',function(){
console.log('hello 2');
});
});
});
module.exports =路由器;
连接客户端后,控制台中显示 connected消息,但未显示 connected消息2英寸的消息。
另外,当客户端发出 connected消息时,我在控制台中收到 hello,但是当客户端发出 taskResponse消息时,我没有收到 hello 2
尽管当 io.to(pickedUser).emit('taskRequest',req.body); $ c $
为什么.emit()在我的路由内有效,但.on()却不起作用?
p>
根据您的代码, io
是附加的Socket.IO服务器实例。到http.Server实例侦听传入事件。然后在路由内,您再次附加一个实例以侦听无效的传入事件。 io.to(pickedUser).emit
之所以有效,是因为具有socketio的服务器实例正确侦听了连接,从而使console.log( connected);。
index.js:
const cors = require('cors') ;
const express = require('express');
const app = express();
const server = require(’http’)。Server(app);
const io = require(’socket.io’)(服务器);
const taskRequest = require('./ routes / taskRequest');
app.use(cors())
app.use(express.json());
app.use(’/ api / taskRequest',taskRequest);
app.set('socketio',io);
server.listen(4002);
routes / taskRequest.js:
const express = require('express');
const router = express.Router();
router.post('/',异步(req,res)=> {
var io = req.app.get('socketio');
// pickedUser是已连接的客户端之一
var PickedUser = JZLpeA4pBECwbc5IAAAA;
io.on('connection',function(socket){
console.log('connected 2');
io.to(pickedUser).emit('taskRequest',req.body);
socket.on('taskResponse',function(){
console.log('hello 2');
});
});
});
module.exports =路由器;
I'm trying to use io.sockets.on inside a route in a Node.js and Express app. I have been following what is said here: https://stackoverflow.com/a/31277123/8271839
I can successfully send io.sockets.emit events, but I cannot receive events with io.sockets.on.
Here is my code:
index.js:
const cors = require('cors');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const taskRequest = require('./routes/taskRequest');
app.use(cors())
app.use(express.json());
app.use('/api/taskRequest', taskRequest);
app.set('socketio', io);
server.listen(4002);
io.sockets.on("connection",function(socket){
console.log("connected");
socket.on("connected", function (data) {
console.log("hello");
})
});
routes/taskRequest.js:
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
var io = req.app.get('socketio');
//pickedUser is one of the connected client
var pickedUser = "JZLpeA4pBECwbc5IAAAA";
//we only send the emit event to the pickedUser
io.to(pickedUser).emit('taskRequest', req.body);
io.on('connection', function (socket) {
console.log('connected 2');
socket.on('taskResponse', function () {
console.log('hello 2');
});
});
});
module.exports = router;
When a client is connected, I get the "connected" message in console, but not the "connected 2" message.
Also, when client emits "connected" message, I get "hello" in console, but when clients emits "taskResponse" message, I don't get "hello 2" in console.
Though when io.to(pickedUser).emit('taskRequest', req.body);
is called, it works, client receives the "taskRequest" message.
Why is .emit() working inside my route but not .on() ?
According to you code, io
is a Socket.IO server instance attached to an instance of http.Server listening for incoming events. Then inside the route you are again attaching a instance to listen to to incoming events which does not work. the io.to(pickedUser).emit
works because the server instance with socketio is correctly listening to the connection thus giving the console.log("connected");.
index.js:
const cors = require('cors');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const taskRequest = require('./routes/taskRequest');
app.use(cors())
app.use(express.json());
app.use('/api/taskRequest', taskRequest);
app.set('socketio', io);
server.listen(4002);
routes/taskRequest.js:
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
var io = req.app.get('socketio');
//pickedUser is one of the connected client
var pickedUser = "JZLpeA4pBECwbc5IAAAA";
io.on('connection', function (socket) {
console.log('connected 2');
io.to(pickedUser).emit('taskRequest', req.body);
socket.on('taskResponse', function () {
console.log('hello 2');
});
});
});
module.exports = router;
这篇关于io.sockets.on无法在Node.js中的路由内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!