我一直在使用john papa的hottowel一段时间并且正在解决如何使用socket-io但无处可去的问题。我已经在Stack Overflow中阅读了一些文章和答案,但似乎无法找到答案。套接字似乎不起作用,我不知道问题。我收到了一个错误:
未捕获的TypeError:无法在仪表板上读取未定义的属性'208'
这是我的代码:
app.js
/*jshint node:true*/
'use strict';
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server); // it was require('socket.io')(server);
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var logger = require('morgan');
var mongoose = require('mongoose');
var port = process.env.PORT || 8001;
var four0four = require('./utils/404')();
var multer = require('multer');
var environment = process.env.NODE_ENV;
app.use(favicon(__dirname + '/favicon.ico'));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true,
parameterLimit: 50000
}));
app.use(logger('dev'));
app.use('/api', require('./routes'));
mongoose.connect(
'mongodb://localhost/hrisdb',
{ useMongoClient: true },
function (err) {
if (err) {
console.log('Not connected to Remote Database' + err);
} else {
console.log('Successfully connected Remote Database');
}
});
console.log('connection', mongoose.connection.readyState);
console.log('About to crank up node');
console.log('PORT=' + port);
console.log('NODE_ENV=' + environment);
switch (environment) {
case 'build':
console.log('** BUILD **');
app.use(express.static('./build/'));
// Any invalid calls for templateUrls are under app/* and should return 404
app.use('/app/*', function (req, res, next) {
four0four.send404(req, res);
});
// Any deep link calls should return index.html
app.use('/*', express.static('./build/index.html'));
break;
default:
console.log('** DEV **');
app.use(express.static('./src/client/'));
app.use(express.static('./'));
app.use(express.static('./tmp'));
// Any invalid calls for templateUrls are under app/* and should return 404
app.use('/app/*', function (req, res, next) {
four0four.send404(req, res);
});
// Any deep link calls should return index.html
app.use('/*', express.static('./src/client/index.html'));
break;
}
server.listen(port, function () {
console.log('Express server listening on port ' + port);
console.log('env = ' + app.get('env') +
'\n__dirname = ' + __dirname +
'\nprocess.cwd = ' + process.cwd());
});
客户端代码
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
io.sockets.on('connection', function (socket) {
console.log('a user connected');
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
</script>