我有以下server.js,可在node.js / express上运行socket.io。
var logger = require('morgan');
var cors = require('cors');
var http = require('http');
var https = require('https');
//fs for reading certificate from filesystem
var fs = require('fs');
var express = require('express');
var errorhandler = require('errorhandler');
var dotenv = require('dotenv');
var bodyParser = require('body-parser');
//path for the static html files
var path = require('path');
//using gzip for compressing content to speed things up
var compression = require('compression');
// the app from express
var app = express();
// Restricts possible calls/sec to the API
const rateLimit = require('express-rate-limit');
// socket.io module
var socketIO = require('socket.io');
// Helmet package: Helmet can help protect your app from some well-known web vulnerabilities by setting HTTP headers
var helmet = require('helmet');
dotenv.load();
// Limit calls/sec to the API
/////////////////////////////////////////// /////////////////////////////
// app.enable("trust proxy"); // only if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// Restrict to 100 calls in 10 min
const apiLimiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes, calc done by milisecs
max: 100
});
// only apply to requests that begin with /api/, Web pages not affected
app.use('/api/', apiLimiter);
////////////////////////////////////////////////////////////////////////
// Make app using helmet for security regarding http headerss
app.use(helmet());
// Parsers
// File limit up to 10 MB
app.use(bodyParser.json({ limit: '10mb', extended: true }));
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
// use gzip for making network calls smaller in size by compressing them
app.use(compression());
//using CORS for protection, todo
app.use(cors());
app.options('*', cors());
app.use(function(err, req, res, next) {
if (err.name === 'StatusError') {
res.send(err.status, err.message);
} else {
next(err);
}
});
// change _env to production if ready
if (process.env.NODE_ENV === 'development') {
app.use(logger('dev'));
app.use(errorhandler());
}
//Setup basic Routes of the API for this app:
app.use(require('./anonymous-routes'));
app.use(require('./protected-routes'));
//set the port for HTTP
var port = process.env.PORT || 3001;
//set the port for HTTPS
var portSec = process.env.PORT || 4433;
/////// Config for the app and server part done ///////
// create the server from the module and tie configured app to it
var server = http.createServer(app);
// tie the server to socket.io
var io = socketIO(server);
// implement real time connected client count
var numClients = 0;
io.on('connection', function(socket) {
numClients++;
io.emit('stats', { numClients: numClients});
console.log('Connected clientinstances:', numClients);
socket.on('disconnect', function() {
numClients--;
io.emit('stats', { numClients: numClients });
console.log('Connected client instances:', numClients);
});
});
//startup the HTTP server
server.listen(port, function(err) {
console.log('active');
});
我有以下客户端应连接到socket.io。
<!--Get socket.io JS-->
<script src="/socket.io/socket.io.js">
</script>
<script>
// Inform sockets of joining
var socket = io.connect('/');
socket.emit('event', { message: 'Im logged in now ' });
</script>
运行此命令时,没有任何反应,也没有任何错误,也没有任何控制台打印输出。 我怀疑某些软件包可能会使sockets.io混乱。据我从socket.io文档了解,客户端可以通过调用connect方法轻松访问它。
我在这里想念什么?