Socket.io连接事件未触发

时间:2017-07-04 17:54:51

标签: javascript node.js pug

我知道套接字已连接,但事件未触发。 服务器:

var express = require('express'),
    path = require('path');

var app = express(),
    server = require('http').createServer(app);


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));


/* Socket.io stuff */
var io = require('socket.io')(server);

io.use(function(socket, next) {
    auth.session(socket.request, socket.request.res, next);
});

//On socket connection
io.sockets.on("connection", function(socket) {
    console.log("Socket connected");
});

/* Start server */
server.listen(3000, function () {
    console.log("Listening on port 3000");
});


/* Mount routers */
var auth = require('./routes/auth'),
    index = require('./routes/index');

app.use('/', index.router);
app.use('/auth', auth.router);


/* Exports */
module.exports = app;

Pug文件:

html
    head
        link(rel='stylesheet', href='/stylesheets/chat.css')
        script(src="http://127.0.0.1:3000/socket.io/socket.io.js")
        script(type='text/javascript', src='../javascripts/chat.js')
    body
        include nav.pug
        .chat
            .chat-messages
                .chat-message
                    | Alex: Hello There
                .chat-message
                    | Billy: Hello There
            input.chat(type="text", placeholder="enter ur message")

Clientside Javascript:

 try {
        var socket = io.connect('http://127.0.0.1:3000');

    } catch(e) {
        //Set status to warn user
        console.log(e);
    }

我知道套接字正在连接,因为它没有返回错误。我也可以手动拨打var socket = io.connect("http://127.0.0.1:3000"); 在控制台中,它返回undefined但连接事件仍然不会触发。关于如何解决这个问题的任何想法?

0 个答案:

没有答案