我在Yii 1.1中配置了yii-node socket,一切都在localhost上运行正常。没问题。
现在我正在尝试在Google Compute Engine上托管我的项目,我遇到了将外部IP绑定到内部IP的问题,以便启动在socket.io.js上发送404的NodeJs服务器。我能够通过从server.listen
方法中删除主机来修复它,以便它可以绑定到任何IP地址,并使用它的公共IP将socket.io js文件加载到客户端。我为我们的实例创建了一个保留的静态IP,并在以下配置中使用了相同的IP。
客户端与服务器的握手不起作用,因为生成的uinque url在加载网站时仍指向GCP上的localhost。我认为它应该是server-ip而不是localhost。
我的节点套接字配置
'nodeSocket' => array(
'class' => 'application.extensions.yii-node-socket.lib.php.NodeSocket',
'host' => 'xxx.211.xxx.99', // default is 127.0.0.1, can be ip or domain name, without http
'allowedServerAddresses' => [ "127.0.0.1", "xxx.211.xxx.99", "::ffff:xxx.211.xxx.99" ],
'origin' => '*:*',
'port' => 3001 // default is 3001, should be integer )
以下是我尝试从Yii发送事件时的日志
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized nvQHUiA9E1dBBKmHv69k
debug - setting request GET /socket.io/1/websocket/nvQHUiA9E1dBBKmHv69k
debug - set heartbeat interval for client nvQHUiA9E1dBBKmHv69k
debug - client authorized for
debug - websocket writing 1::
debug - client authorized for /server
debug - websocket writing 1::/server
info - transport end (undefined)
debug - set close timeout for client nvQHUiA9E1dBBKmHv69k
debug - cleared close timeout for client nvQHUiA9E1dBBKmHv69k
debug - cleared heartbeat interval for client nvQHUiA9E1dBBKmHv69k
debug - discarding transport
server.js代码
var express = require('express');
var app = express();
var server = require('http').createServer(app)
var io = require('socket.io').listen(server);
var cookie = require('cookie');
var serverConfiguration = require('./server.config.js');
var storeProvider = express.session.MemoryStore;
var sessionStorage = new storeProvider();
var componentManager = require('./components/component.manager.js');
componentManager.set('config', serverConfiguration);
var eventManager = require('./components/event.manager.js');
var socketPull = require('./components/socket.pull.js');
var db = require('./components/db.js');
db.init(serverConfiguration.dbOptions);
componentManager.set('db', db);
componentManager.set('sp', socketPull);
componentManager.set('io', io);
componentManager.set('eventManager', eventManager);
componentManager.set('sessionStorage', sessionStorage);
server.listen(serverConfiguration.port);
console.log('Listening ' + serverConfiguration.host + ':' + serverConfiguration.port);
// accept all connections from local server
if (serverConfiguration.checkClientOrigin) {
console.log('Set origin: ' + serverConfiguration.origin);
io.set("origins", serverConfiguration.origin);
}
// client
io.of('/client').authorization(function (handshakeData,accept) {
if (!handshakeData.headers.cookie) {
return accept('NO COOKIE TRANSMITTED', false);
}
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
var sid = handshakeData.cookie[serverConfiguration.sessionVarName];
if (!sid) {
return accept('Have no session id', false);
}
handshakeData.sid = sid;
handshakeData.uid = null;
// create write method
handshakeData.writeSession = function (fn) {
sessionStorage.set(sid, handshakeData.session, function () {
if (fn) {
fn();
}
});
};
// trying to get session
sessionStorage.get(sid, function (err, session) {
// create session handler
var createSession = function () {
var sessionData = {
sid : sid,
cookie : handshakeData.cookie,
user : {
role : 'guest',
id : null,
isAuthenticated : false
}
};
// store session in session storage
sessionStorage.set(sid, sessionData, function () {
// authenticate and authorise client
handshakeData.session = sessionData;
accept(null, true);
});
};
// check on errors or empty session
if (err || !session) {
if (!session) {
// create new session
createSession();
} else {
// not authorise client if errors occurred
accept('ERROR: ' + err, false);
}
} else {
if (!session) {
createSession();
} else {
// authorize client
handshakeData.session = session;
handshakeData.uid = session.user.id;
accept(null, true);
}
}
});
}).on('connection', function (socket) {
// add socket to pull
socketPull.add(socket);
// connect socket to him channels
componentManager.get('channel').attachToChannels(socket);
// bind events to socket
eventManager.client.bind(socket);
});
// server
io.of('/server').authorization(function (data, accept) {
if (data && data.address) {
if (data.headers['cookie']) {
data.cookie = cookie.parse(data.headers.cookie);
if (data.cookie.PHPSESSID) {
data.sid = data.cookie.PHPSESSID;
var found = false;
for (var i in serverConfiguration.allowedServers) {
if (serverConfiguration.allowedServers[i] == data.address.address) {
found = true;
break;
}
}
if (found) {
var createSession = function () {
var sessionData = {
sid : data.cookie.PHPSESSID,
cookie : data.cookie,
user : {
role : 'guest',
id : null,
isAuthenticated : false
}
};
// store session in session storage
sessionStorage.set(data.cookie.PHPSESSID, sessionData, function () {
// authenticate and authorise client
data.session = sessionData;
accept(null, true);
});
};
data.writeSession = function (fn) {
sessionStorage.set(data.cookie.PHPSESSID, data.session, function () {
if (fn) {
fn();
}
});
};
sessionStorage.get(data.cookie.PHPSESSID, function (err, session) {
if (err || !session) {
if (!session) {
createSession();
} else {
accept('ERROR: ' + err, false);
}
} else {
if (!session) {
createSession();
} else {
// authorize client
data.session = session;
data.uid = session.user.id;
accept(null, true);
}
}
});
} else {
accept('INVALID SERVER: server host ' + data.address.address + ' not allowed');
}
} else {
accept('PHPSESSID is undefined', false);
}
} else {
accept('No cookie', false);
}
} else {
accept('NO ADDRESS TRANSMITTED.', false);
return false;
}
}).on('connection', function (socket) {
// bind events
eventManager.server.bind(socket);
});
componentManager.initCompleted();
答案 0 :(得分:0)
我能够自己解决它.. :)我错过了查看缓存的资产,其中旧代码指向localhost,当我清除yii资产文件夹缓存时,它工作。
配置只需要进行一次更改即可删除主题与我在问题中已提及的server.listen
方法的绑定,以便它可以接受来自Google Compute Engine上任何IP地址的请求。