为什么我的socket.io事件不起作用?

时间:2013-09-17 18:56:54

标签: javascript node.js express connection socket.io

我正在编写一个使用socket.io的快速应用程序;然而我所得到的只是服务器方面告诉我我已经连接,除此之外,我的插座都没有工作。如果有人能够说出为什么会发生这种情况会非常感激。以下是我的客户端和服务器代码:

客户端:

var socket = new io.Socket();
socket.connect('127.0.0.1:3000');
var pageView = new pageViewModel();
pageView.setCurrentPage(pageView.getLogin());
pageView.setNav(pageView.getLogNav());
var tView = new totalViewModel();
socket.on('connection', function () {
  ko.applyBindings(pageView);

  function loggedIn(e) {
    e.preventDefault();
    var logged = {
      username: document.getElementById('pEmail').value,
      password: document.getElementById('pWord').value
    };
    socket.emit('login', JSONout(logged));
    alert(JSONout(logged));
  }

  function signUp() {
    pageView.setCurrentPage(pageView.getRegister());
    pageView.setNav(pageView.getRegisterNav());
    ko.applyBindings(pageView);
  }
  socket.on('uploadList', function (data) {
    pageView.setCurrentPage(pageView.getIndex());
    pageView.setNav(pageView.getIndexNav());
    ko.applyBindings(pageView);
    tView.setNum(JSONin(data).total);
    ko.applyBindings(tView);
  });

  function logPurchase(e) {
    e.preventDefault();
    var purchObj = {
      price: document.getElementById('pPrice').value,
      priceName: document.getElementById('pName').value,
      entryDate: new Date()
    };
    socket.emit('purchaseLog', JSONout(purchObj));
  }

  function logDeposit(e) {
    e.preventDefault();
    var depositObj = {
      price: document.getElementById('dPrice').value,
      entryDate: new Date()
    };
    socket.emit('depositLog', JSONout(purchObj));
  }

  function getLogout(e) {
    e.preventDefault();
    pageView.setCurrentPage(pageView.getLogin());
    pageView.setNav(pageView.getLogNav());
    ko.applyBindings(pageView);
    socket.emit('goLogout', {
      none: 'none'
    });
  }

  function getIndex(e) {
    e.preventDefault();
    pageView.setCurrentPage(pageView.getIndex());
    pageView.setNav(pageView.getIndexNav());
    ko.applyBindings(pageView);
  }

  function getMonthly(e) {
    e.preventDefault();
    pageView.setCurrentPage(pageView.getMonthly());
    pageView.setNav(pageView.getMonthlyNav());
    ko.applyBindings(pageView);
  }

  function getYearly(e) {
    e.preventDefault();
    pageView.setCurrentPage(pageView.getYearly());
    pageView.setNav(pageView.getYearlyNav());
    ko.applyBindings(pageView);
  }
  socket.on('wentMonth', function (data) {
    tView.setNum(total in JSONin(data));
    ko.applyBindings(tView);
  });
  socket.on('wentYearly', function (data) {
    tView.setNum(total in JSONin(data));
    ko.applyBindings(tView);
  });

  function register(e) {
    e.preventDefault();
    var reg = {
      username: document.getElementById('pEmail').value,
      password: document.getElementById('pWord').value,
      dateJoined: new Date(),
      total: 0,
      purchase: {},
      deposit: {},
      monthlyTotal: 0,
      yearlyTotal: 0
    };
    pageView.setCurrentPage(pageView.getIndex());
    pageView.setNav(pageView.getIndexNav());
    ko.applyBindings(pageView);
    socket.emit('registered', JSONout(reg));
  }
});

服务器:

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var http = require('http');
var db = mongojs('127.0.0.1:27017/mySpendingDB', ['users']);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var path = require('path');
app.configure(function () {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.engine('html', require('ejs').renderFile);
  app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', function (req, res) {
  res.render('mainPage.html', {
    title: 'app'
  });
});
var user = "";
db.users.find(function (err, docs) {
  console.log(docs);
  return docs;
});
io.sockets.on('connection', function (socket) {
  console.log('socket.io started');
  socket.on('login', function (data) {
    console.log('login');
    var checkUser = db.users.find(data, function (err, docs) {
      return docs;
    });
    console.log(checkUser);
    if (checkUser !== null) {
      console.log('checked user');
      user = checkUser.username;
      socket.emit('uploadList', checkUser.total);
    } else {
      console.log('Log in invalid');
    }
  });
  socket.on('purchaseLog', function (data) {
    var usercollection = db.users.find({
      username: user
    });
    usercollection.purchase.save(data);
  });
  socket.on('depositLog', function (data) {
    var usercollection = db.users.find({
      username: user
    });
    usercollection.deposit.save(data);
  });
  socket.on('goMonthly', function (data) {
    var monTot = db.users.find({
      username: user
    }).monthlyTotal;
    socket.emit('wentMonthly', monTot);
  });
  socket.on('goYearly', function (data) {
    var yearTot = db.users.find({
      username: user
    }).yearlyTotal;
    socket.emit('wentYearly', yearTot);
  });
  socket.on('registered', function (data) {
    db.users.save(data);
  });
});
server.listen(3000);

0 个答案:

没有答案