我正在尝试在Node.js和Express应用程序的路由内使用io.sockets.on。我一直在关注这里所说的话:https://stackoverflow.com/a/31277123/8271839
我可以成功发送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')(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;
连接客户端后,我在控制台中收到“ connected”消息,但未收到“ connected 2”消息。
此外,当客户端发出“已连接”消息时,我在控制台中收到“ hello”,但是当客户端发出“ taskResponse”消息时,我在控制台中却没有得到“ hello 2”。
尽管调用io.to(pickedUser).emit('taskRequest', req.body);
可以正常工作,但是客户端会收到“ taskRequest”消息。
.emit()为什么在我的路由内起作用,但.on()却不起作用?
答案 0 :(得分:2)
根据您的代码,io
是附加到http.Server实例的Socket.IO服务器实例,用于侦听传入事件。然后在路由内,您再次附加一个实例以侦听无效的传入事件。 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')(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;
答案 1 :(得分:0)
我将TRomesh答案标记为正确答案,因为实际上您的代码中只能有一个$this->form_validation->set_rules('username', 'Email', 'required', 'callback_IsEmail_exist');
。
现在要做的就是使它对我有用:问题是,如果将io.on('connection', function (socket) {})
放在io.on('connection', function (socket) {})
内,则只有在调用端点时才会触发它。就我而言,我想随时调用一些套接字事件,而不仅是在调用端点时。因此,我不得不将router.post('/', async (req, res) => {})
放在我的io.on('connection', function (socket) {})
之外。因此,我无法在路由器内部使用router.post('/', async (req, res) => {})
。这是我所做的:
index.js:
var io = req.app.get('socketio');
routes / taskRequest.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')(io);
app.use(cors())
app.use(express.json());
app.use('/api/taskRequest', taskRequest);
server.listen(4002);