ReferenceError:未定义套接字。套接字

时间:2018-09-15 15:21:35

标签: node.js socket.io

此错误出现在socket.io中

// SERVER.JS

var express    = require('express');
var app        = express();
var bodyParser = require('body-parser');
var port       = process.env.PORT || 8080;
var http       = require('http').Server(app);
var io         = require('socket.io')(http);
require('./app/routes.js')(app, io);

// ROUTES.JS

socket.emit('dlReady', { description: 'Your file is ready!'});

我收到此错误:

ReferenceError: socket is not defined

编辑:

app.post('/pst', function(req, res) {
        var url = req.body.convo;

        res.render('processing.ejs');

               funker(url).then(result => {
                    console.log('TAKE A LOOK AT THIS!'); 

                    socket.emit('dlReady', { description: 'Your file is ready!'});


                    //transforming array to string to pass to Buffer.from()
                    //then we remove ',' with newlines, so each index of array is a new line
                    var str = result.toString();
                    result = str.split(',').join('\r\n');
                    str = ''; //garbage collection
                    resultGlobal = result;
                    result = ''; //garbage collection




                }).catch(err => {
                    console.log(err);
                    res.render('error.ejs');
                })
    });

1 个答案:

答案 0 :(得分:1)

如何使用套接字:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});