gpt4 book ai didi

node.js - 代理服务器后面的客户端无法连接socket.io

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:31 28 4
gpt4 key购买 nike

我正在将 socket.io 与我的expressjs (NodeJs) 应用程序一起使用。我有一个简单的场景,我有一个房间,只允许两个人加入。

每当我连接到我的网站并且我不在代理服务器后面时,一切都会正常工作。服务器和客户端之间交换消息。

当我在代理服务器后面尝试连接我的网站时,不会发生这种情况。我在服务器端收到以下错误。

debug - client authorized
info - handshake authorized HjRiaKY1tR0zSbrieZ4t
debug - setting request GET /socket.io/1/websocket/HjRiaKY1tR0zSbrieZ4t
debug - set heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
debug - client authorized for
debug - websocket writing 1::
debug - setting request GET /socket.io/1/websocket/HjRiaKY1tR0zSbrieZ4t
debug - set heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
warn - websocket connection invalid
info - transport end (undefined)
debug - set close timeout for client HjRiaKY1tR0zSbrieZ4t
debug - cleared close timeout for client HjRiaKY1tR0zSbrieZ4t
debug - cleared heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
debug - discarding transport
debug - cleared heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
debug - setting request GET /socket.io/1/websocket/HjRiaKY1tR0zSbrieZ4t
debug - set heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
warn - websocket connection invalid
info - transport end (undefined)
debug - set close timeout for client HjRiaKY1tR0zSbrieZ4t
debug - cleared close timeout for client HjRiaKY1tR0zSbrieZ4t
debug - cleared heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
debug - setting request GET /socket.io/1/websocket/HjRiaKY1tR0zSbrieZ4t
debug - set heartbeat interval for client HjRiaKY1tR0zSbrieZ4t
warn - websocket connection invalid
info - transport end (undefined)
debug - set close timeout for client HjRiaKY1tR0zSbrieZ4t
debug - cleared close timeout for client HjRiaKY1tR0zSbrieZ4t
debug - cleared heartbeat interval for client HjRiaKY1tR0zSbrieZ4t

这就是我的服务器代码的样子(为了简单起见,删除了不必要的细节)

var app = express();
require('./routes')(app);

var server = http.createServer(app)

var io = require('socket.io').listen(server);

io.set('heartbeat interval', 20);
io.set('heartbeat timeout', 60);
io.set('close timeout', 60);

io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
//io.set('log level', 1); // reduce logging

//Commented on purpose for testing (before this it used polling but it also failed)
io.set('transports', [
'websocket'
// , 'htmlfile'
// , 'xhr-polling'
// , 'jsonp-polling'
]);

server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

io.sockets.on('connection', function (socket){

// convenience function to log server messages on the client
function log(){
var array = [">>> Message from server: "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}

socket.on('message', function (message) {
log('Got message:', message);
// for a real app, would be room only (not broadcast)
socket.broadcast.emit('message', message);
});

socket.on('create or join', function (room) {
var numClients = io.sockets.clients(room).length;

log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room ' + room);

if (numClients === 0){
socket.join(room);
socket.emit('created', room);
} else if (numClients === 1) {
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else { // max two clients
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);

});

});

这就是我的客户的样子:

var room = prompt("Enter room name:");

var socket = io.connect();

if (room !== '') {
console.log('Create or join room', room);
socket.emit('create or join', room);
}

socket.on('created', function (room){
console.log('Created room ' + room);
isInitiator = true;
});

socket.on('full', function (room){
console.log('Room ' + room + ' is full');
});

socket.on('join', function (room){
console.log('Another peer made a request to join room ' + room);
console.log('This peer is the initiator of room ' + room + '!');
isChannelReady = true;
});

socket.on('joined', function (room){
console.log('This peer has joined room ' + room);
isChannelReady = true;
});

socket.on('log', function (array){
console.log.apply(console, array);
});

我进行了很多搜索,但找不到任何解决方案。如果有人知道,请帮助我。谢谢。

最佳答案

一个老问题。也许有人需要答案。

如果您使用简单的 http(s) 代理,则可以使用以下内容,这是用 typescript 编写的 Node 客户端...

var HttpsProxyAgent = require('https-proxy-agent');

let proxy = 'http://172.18.200.182:8080';
let opts: SocketIOClient.ConnectOpts = { agent: new HttpsProxyAgent(proxy) };

var socket = require('socket.io-client').connect('http://your.socket.io.server', opts);

关于node.js - 代理服务器后面的客户端无法连接socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22520251/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com