我正在尝试在套接字上为路由的发布请求执行类似的操作。如何只在插座上做
客户端中的操作
export const registerUser = (userData, history) => dispatch => {
axios
.post('/users/register', {
type: `server/${REGISTER_USER}`,
payload: userData
})
.then(res => history.push('/login'))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
服务器中的路由器:
router.post("/register", (req, res) => {
req.io.sockets.on('action', ({ type, payload }) => {
switch (type) {
case 'server/REGISTER_USER':
//...
db.getConnection((err, connection) => {
if (err) throw err;
const query = 'SELECT count(email) as number FROM Employee WHERE email=?';
connection.query(query, [payload.email], function (error, results, fields) {
connection.release();
if (error) throw error;
//...
const query = `
INSERT INTO Employee
(id_employee, id_dep, id_post, id_company, name, surname, patron, email, password, avatar)
VALUES
(null, null, null, null, ?, ?, ?, ?, ?, ?)`;
connection.query(query,
[payload.name, payload.surname, payload.patron, payload.email, payload.password, avatar],
(error, results, fields) => {
if (error) throw error;
req.io.sockets.emit('action', { type: 'server/SUCCESS' })
})
});
});
}
});
})
如果我使用“ redux-socket.io”,如何获得积极回应并采取此行动?
.then(res => history.push('/login'))