简体   繁体   English

NodeJS + Socket.io连接事件处理程序未成功调用?

[英]NodeJS + Socket.io Connection event handler not being called successfully?

I have the following code in a file called server.js, and index.html 我在名为server.js和index.html的文件中包含以下代码

For some reason, the io.on('connection') part is not calling the console.log method in its callback when I navigate to my server in my web browser. 出于某种原因,当我在Web浏览器中导航到服务器时,io.on('connection')部分未在其回调中调用console.log方法。

Check the code below, it speaks for itself. 检查下面的代码,它说明了一切。 server.js server.js

var express = require('express'),
    app = express(),
    io = require('socket.io')(app.Server),
    mongoose = require('mongoose'),
    bodyParser = require('body-parser'),
    apiRouter = require('./app/routes/api.js');

//Clears Node Console.
process.stdout.write('\033c');
console.log('Server starting!');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(apiRouter);
app.use(express.static('public'));

app.use('*', function(req, res, next) {
    //All requests return single page angular application.
    res.sendFile(__dirname + '/public/index.html');
});

mongoose.connect('localhost', 'triviaattack', function(err) {
    if (err) {
        console.log('An error occured when connecting to the MongoDB Database');
        throw err;
    }
});

io.on('connection', function(socket) {
    console.log('client connected via socket'); //This is the line that isn't being called
});

app.listen(1337, function() {
    console.log('Server started successfully @ ' + Date());
});

index.html index.html

<!DOCTYPE html>

<html ng-app="myApp">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular-route.min.js"></script>
    <script src="./app/app.js"></script>

    <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    <script>
        var socket = io();
    </script>

</head>
<body>
    <div ng-controller="HomeCtrl">
        {{message}}
    </div>
</body>
</html>

Found the answer. 找到了答案。

Had to change server.js to the following, notice the new http variable near the top. 必须将server.js更改为以下内容,注意顶部附近的新http变量。 Still kind of curious as to why just passing in app.Server wasn't working in my original post...oh well. 仍然对为什么只传入app.Server在我的原始帖子中无法正常工作感到好奇...哦,很好。

var app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io')(http),
    mongoose = require('mongoose'),
    bodyParser = require('body-parser'),
    apiRouter = require('./app/routes/api.js');

//Clears Node Console.
process.stdout.write('\033c');
console.log('Server starting!');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(apiRouter);

app.use('*', function(req, res, next) {
    //All requests return single page angular application.
    res.sendFile(__dirname + '/public/index.html');
});

mongoose.connect('localhost', 'triviaattack', function(err) {
    if (err) {
        console.log('An error occured when connecting to the MongoDB Database');
        throw err;
    }
});

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

http.listen(1337, function() {
    console.log('Server started successfully @ ' + Date());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM