我正在尝试在node.js和express应用中使用io.sockets.on。我一直在关注这里说的内容:https://stackoverflow.com/a/31277123/8271839
我可以成功发送io.sockets.mit事件,但是我无法通过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");
})
});
路由/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;
连接客户端时,我会在控制台中获得"连接"消息,而不是"连接的2"消息。
另外,当客户端发出"连接"消息时,我会在控制台中获得" hello",但是当客户端发出" taskResponse"消息时,我在控制台中不会得到" hello 2"。
虽然当调用 io.to(pickedUser).emit('taskRequest', req.body);
时,它有效,客户端会收到" taskrequest"消息。
为什么.emit((在我的路线内工作,而不是.on((?
根据您的代码, 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')(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);
路由/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;
i将Tromesh答案标记为正确的答案,因为确实您可以在代码中只有一个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) => {})
之外。因此,我无法在路由器内使用var io = req.app.get('socketio');
。这是我所做的:
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')(io);
app.use(cors())
app.use(express.json());
app.use('/api/taskRequest', taskRequest);
server.listen(4002);
路由/taskrequest.js
const express = require('express');
const router = express.Router();
module.exports = function(io) {
//we define the variables
var sendResponse = function () {};
io.sockets.on("connection",function(socket){
// Everytime a client logs in, display a connected message
console.log("Server-Client Connected!");
socket.on('connected', function(data) {
//listen to event at anytime (not only when endpoint is called)
//execute some code here
});
socket.on('taskResponse', data => {
//calling a function which is inside the router so we can send a res back
sendResponse(data);
})
});
router.post('/', async (req, res) => {
//pickedUser is one of the connected client
var pickedUser = "JZLpeA4pBECwbc5IAAAA";
io.to(pickedUser).emit('taskRequest', req.body);
sendResponse = function (data) {
return res.status(200).json({"text": "Success", "response": data.data});
}
});
return router;
};