为什么io不确定?

我有一个快速的应用程序使用socket.io。 我不清楚客户开始需要什么,所以如果有人能告诉我怎么做,我会很感激。 另外,我不确定我的客户端代码是否正确。 这是我的客户端套接字代码:

var socket = io.connect('http://localhost:3000'); var tView = new totalViewModel(); socket.on('connect', function (data) { function loggedIn(e) { e.preventDefault(); var logged = { username: document.getElementById('pEmail'), password: document.getElementById('pWord') }; socket.emit('login', JSONout(logged)); } socket.on('uploadList', function (data) { 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(); socket.emit('goLogout', { none: 'none' }); } function getIndex(e) { e.preventDefault(); socket.emit('goIndex', { none: 'none' }); } function getMonthly(e) { e.preventDefault(); socket.emit('goMonthly', { none: 'none' }); } function getYearly(e) { e.preventDefault(); socket.emit('goYearly', { none: 'none' }); } 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 }; 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('login.html', {title: 'Login'}); }); app.get('/index', function (req, res) { res.render('index.html', {title: 'Daily'}); }); app.get('/monthly', function (req, res) { res.render('monthly.html', {title: 'Monthly'}); }); app.get('/yearly', function (req, res) { res.render('yearly.html', {title: 'Login'}); }); app.get('/register', function (req, res) { res.render('register.html', {tittle: 'Register'}); }); app.get('/logout', function (req, res) { req.logout(); res.redirect('/'); }); var user=""; 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); console.log(checkUser); if(checkUser !== null) { console.log('checked user'); user=checkUser.username; app.get('/index', function(req, res) { res.direct('/index'); }); socket.emit('uploadList', checkUser.total); } }); 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('goLogout', function(data){ app.get('/logout', function (req, res) { req.logout(); res.redirect('/'); }); }); socket.on('goIndex', function(data){ app.get('/index', function (req, res) { res.redirect('/index'); }); }); socket.on('goMonthly', function(data){ app.get('/monthly', function (req, res) { res.redirect('/monthly'); }); var monTot = db.users.find({username: user}).monthlyTotal; socket.emit('wentMonthly', monTot); }); socket.on('goYearly', function(data){ app.get('/index', function (req, res) { res.redirect('/index'); }); var yearTot = db.users.find({username: user}).yearlyTotal; socket.emit('wentYearly', yearTot); }); socket.on('registered', function(data){ db.users.save(data); app.get('/index', function (req, res) { res.redirect('/index'); }); }); }); server.listen(3000); 

       

网上收集的解决方案 "为什么io不确定?"