我正在使用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);
});
我搜索了很多但找不到任何解决方案。如果有人知道,请帮助我。 感谢。
答案 0 :(得分:1)
一个老问题。也许有人需要答案。
如果您使用的是简单的http(s)代理,则可以使用以下内容,这是用打字稿编写的节点客户端...
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);