我使用以下配置在haproxy后面配置了socket.io服务器 -
frontend http-in
mode http
bind *:9090
acl is_websocket path_beg /prodSocket
use_backend socket if is_websocket
backend socket
timeout server 180s
server 127.0.0.1 127.0.0.1:9091
server 148.251.76.84 148.251.76.84:9000
My Node socket.io服务器代码 -
var Server = require('socket io');
var io = new Server({'path': '/prodSocket'});
io.listen(9091);
console.log('started on ' + 9091);
io.on('connection', function(socket) {
console.log(socket.id);
socket.emit('news', { hello: 'connected' });
socket.on('client', function (data) {
socket.emit('news', { hello: 'world' });
console.log(data);
});
socket.on('disconnected', function(socket) {
console.log(socket.id + " disconnected");
});
});
我的客户代码 -
var socket = io('http://127.0.0.1:9090', {
'path': '/prodSocket',
'force new connection': false,
'reconnection delay': 500,
'max reconnection attempts': 10,
});
socket.emit('client', { my: 'data' });
socket.on('news', function (data) {
console.log(data);
});
错误 -
WebSocket connection to 'ws://127.0.0.1:9090/prodSocket/?EIO=3&transport=websocket&sid=cf1I2nsJrsksPpzfAAAA' failed: Error during WebSocket handshake: Unexpected response code: 502
我真的不明白这里错了什么。虽然此错误,我的服务器和客户端都能够交换消息。但必须解决这个错误。
真的很感激任何帮助。
Haproxy Logs -
Jul 27 17:17:28 localhost haproxy[1332]: 127.0.0.1:42436 [27/Jul/2016:17:17:28.455] http-in socket/127.0.0.1 0/0/0/-1/15 502 205 - - SH-- 1/1/1/0/0 0/0 "GET /prodSocket/?EIO=3&transport=websocket&sid=kZXRU_7vBf09aoE7AAAc HTTP/1.1"
Jul 27 17:17:28 localhost haproxy[1332]: 127.0.0.1:42436 [27/Jul/2016:17:17:28.455] http-in socket/127.0.0.1 0/0/0/-1/15 502 205 - - SH-- 1/1/1/0/0 0/0 "GET /prodSocket/?EIO=3&transport=websocket&sid=kZXRU_7vBf09aoE7AAAc HTTP/1.1"