我正在node.js中构建一个简单的回合制游戏,并且我使用本地托管的mongoDB 100%运行,套接字事件工作正常,但是当我从本地数据库(使用mongo)更改为mLab时mongoDB托管它停止工作,我仍然可以登录并正确注册帐户,数据库仍然返回完全相同的结果,所以它显然没有连接和查询数据库的麻烦。但我完全迷失了问题所在。当我更改数据库URL时,它永远不会触发socket.emit('key')事件;
然而,它确实触发了客户端套接字上的('find match')事件,但服务器从不处理它。 (它不会在服务器上记录“findMatch”。
服务器正在记录“连接”但从未接收任何其他套接字事件。
即使我没有得到解决方案,我只是完全陷入困境,试图调试问题发生的地方,所以任何类型的输入都会非常感激
这是我在控制台中的套接字调试日志。
//--------------------------------------------------------------------------------------------------------------
looking for match
socket.io-1.2.0.js:1 socket.io-client:manager writing packet %j +3s Object {type: 2, data: Array[1], nsp: "/"}
socket.io-1.2.0.js:1 socket.io-parser encoding packet %j +2s Object {type: 2, data: Array[1], nsp: "/"}
socket.io-1.2.0.js:1 socket.io-parser encoded %j as Object +0ms 2["findMatch"]
socket.io-1.2.0.js:1 engine.io-client:socket flushing 1 packets in socket +2s
//--------------------------------------------------------------------------------------------------------------
var express = require('express');
app = express();
http = require('http').Server(app);
io = require('socket.io').listen(http);
mongoose = require('mongoose');
passport = require('passport');
flash = require('connect-flash');
morgan = require('morgan');
bodyParser = require('body-parser');
cookieParser = require('cookie-parser');
session = require('express-session');
methodOverride = require('method-override');
User = require('./models/user');
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
mongoose.connection.on('error', console.error);
app.use(express.static('public', {redirect: false}));
app.use(express.static('private/tests', {redirect: false}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//app.use(methodOverride());
app.use(cookieParser());
app.set('view engine', 'ejs');
require('./config/passport')(passport);
app.use(session({secret: 'password'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
require('./routes')(app,passport);
var rooms = [];
io.sockets.on('connection', function(socket){
console.log("connection");
socket.on('key', function(data){
console.log("key");
socket._id = data.user_id;
socket.nickname = data.username;
socket.rank = data.rank;
socket.on('findMatch', function() {
console.log('findMatch');
//do some other stuff
});
然后在客户端我有这个设置
scotchApp.controller('lobbyController', function ($scope, $http) {
$http.get('/play').then(function successCallback(results){
socket.on('connect',function(){
console.log("emit key")
socket.emit('key',{
user_id: results.data._id,
username: results.data.local.email,
rank: results.data.local.rank,
});
});
$scope.findMatch = function(){
console.log('looking for match');
socket.emit('findMatch')
};