我面临着socket.io和节点js的问题。
上下文
我有两个服务器,一个是处理繁重的任务,另一个是响应客户端。主要情况如下:
这是示例代码
客户端
<script type="text/javascript"/>
var socket = io('https://localhost:9091', {'forceNew': true);
// send a request to the mid server
socket.emit('request', data);
socket.on('response', async (response) => {
// when the mid server responds, the response is printed
$('#container').append(response);
});
</script>中间服务器
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
// in order to use this server as a Socket.io client
const secondServer = require('socket.io-client').connect('http://localhost:9092');
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index', {})
});
io.on('connection', async (socket) => {
console.log('User connected');
// On client request
socket.on('request', async (data) => {
console.log('Requesting from ' + socket.id);
// The same request is transmited to the second server
secondServer.emit('request', data);
});
// Once the second server has finished his job
secondServer.on('response', async (data) => {
console.log('Responding for ' + socket.id);
// I send the data back to the client
socket.emit('response', data);
});
socket.on('disconnect', () => {
socket.disconnect();
});
});
// port is 9091
http.listen(port, () => {
console.log('Server listening on port ' + port);
});第二服务器
const io = require("socket.io").listen(9092);
io.on("connection", function (socket) {
socket.on('request', async () => {
// await in order to wait the heavyJob to be done before sending the response
var data = await heavyJob()
// Send back the data to the mid server
socket.emit('response', data);
});
});问题
我面临的问题是,如果刷新客户端页面,mid服务器将发送两次数据,一次发送到旧套接字,一次发送给新套接字,如下所示:

我还尝试使用中间服务器端的socket.emit('response', data)和客户端的socket.on('response', (data) => {})来响应客户机,而不是使用callback函数。这不会改变任何事情。
我是不是误会了什么?
谢谢你的帮忙
编辑
这不仅发生在客户端刷新他的页面时。当两个不同的客户端同时发送请求时,就会发生这种情况。服务器响应四次,每个客户端响应两次。

发布于 2019-10-05 01:56:45
你说得对,妮可,我不知道回电话是联系不到的。这是我的错。
根据您编辑的代码,您可以从"secondServer.on('response'.."回调中提取"io.on('connection'"'s。
你可以在下面试一试,我希望这会有帮助。
中间服务器
io.on('connection', async (socket) => {
console.log('User connected');
// On client request
socket.on('request', async (data) => {
console.log('Requesting from ' + socket.id);
// The same request is transmited to the second server
// give data and socket.id to secondServer.
secondServer.emit('request', {data:data, id:socket.id});
});
// Once the second server has finished his job
socket.on('disconnect', () => {
socket.disconnect();
});
});
secondServer.on('response', async (reply) => {
const {id, data} = reply;
console.log('Responding for ' + id);
// I send the data back to the client
io.to(id).emit('response', data);
});第二服务器
const io = require("socket.io").listen(9092);
io.on("connection", function (socket) {
socket.on('request', async (req) => {
// await in order to wait the heavyJob to be done before sending the response
const {id} = req;
var data = await heavyJob();
const reply = { id, data };
// Send back the data to the mid server
socket.emit('response', reply);
});
});我认为你需要拔出"secondServer.on('response'..“)来自“socket.on(‘请求’,.”)的代码回调。
io.on('connection', async (socket) => {
console.log('User connected');
// On client request
socket.on('request', async (data, callback) => {
console.log('Requesting from ' + socket.id);
// The same request is transmited to the second server
secondServer.emit('request', data);
});
secondServer.on('response', async (data) => {
console.log('Responding for ' + socket.id);
callback(data.images);
});
});https://stackoverflow.com/questions/58244187
复制相似问题