socket.io服务器没有监听连接事件(快速服务器)

时间:2017-02-26 16:14:57

标签: node.js express socket.io

我一直在抨击几位导游以及他们的文档,看起来我的一切都是正确的。我希望当我导航到localhost:8080连接事件触发时,我的console.log消息应该出现。

// modules =================================================
var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io')(server),
    bodyParser = require('body-parser'),
    methodOverride = require('method-override')
;

// config files ============================================
var db = require('./config/db');

// configuration ===========================================
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); 
app.use(methodOverride('X-HTTP-Method-Override')); 

app.use(express.static(__dirname + '/public'));

// routes  =================================================
require('./app/routes')(app);


io.on('connection', function(){
    console.log("Why won't this display anything");
});

// set port && listen
var port = process.env.PORT || 8080;
server.listen(port);
console.log('Server connected on port ' + port);

exports = module.exports = app;

1 个答案:

答案 0 :(得分:1)

要在服务器上查看socket.io连接,您需要在网页中创建socket.io连接的客户端代码。

通常看起来像这样:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
  // do more things with the connection here
</script>