无法在Express中连接Socket.io

时间:2017-09-04 18:10:43

标签: node.js sockets express websocket socket.io

我尝试使用其他解决方案连接到socket.io但我无法连接到它以下是我的代码片段。

app.js

var express = require('express');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use('/', index);
app.use('/users', users);
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', function(socket) {
    console.log('Client connected.');
});


http.listen(3000, function(){
  console.log('listening on *:3000');
});

index.jade

extends layout

   script(src='/socket.io/socket.io.js')
script.
  var socket = io();

安装socket.io后的Package.json

"socket.io": "^2.0.3"

请建议我在哪里做错了

1 个答案:

答案 0 :(得分:0)

尝试使用新的关键字,可能会起作用

var app = new express();