简体   繁体   English

socket.io无法正常工作node.js

[英]socket.io not working node.js

I am not able to run socket.io code in node.js, console.log() is also not displaying when running the code. 我无法在node.js中运行socket.io代码,运行代码时也不会显示console.log()。 Below is the code. 下面是代码。

app.js app.js

var express = require('express');
var http = require('http');
var app = express();
app.set('port', process.env.PORT || 3000);
app.post('/testStream',test.testStream);
var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

module.exports.appServer = server;

and I have created a test.js file where I am accessing this exported variable appServer. 并且我创建了一个test.js文件,在这里我可以访问此导出的变量appServer。

var server = require('../app.js');

exports.testStream = function(req,res){        
    var io = require('socket.io').listen(server.appServer);
    io.on('connection',function(socket){
        console.log("in socket");
        fs.readFile('E:/temp/testimg.png',function(err,buf){
            socket.emit('image',{image: true,buffer: buf});
            console.log("test image");
        });
    })       
}

when the code runs it stucks and not showing the console.logs(). 代码运行时卡住,没有显示console.logs()。 What I am doing wrong over here. 我在这儿做错了。 Any help is very much appreciated. 很感谢任何形式的帮助。

I would suggest following the code structure as suggested in socket.io docs. 我建议遵循socket.io文档中建议的代码结构。

Also, you should not be calling io.listen or io.on('connection') inside your testStream express middleware. 此外,你应该调用io.listenio.on('connection')的内部testStream快递中间件。 These are things you should only be doing once, and ideally they should happen during startup, inside app.js and not in reaction to a POST request. 这些都是您只应该执行一次的事情,理想情况下,它们应该在启动期间在app.js内发生,而不是响应POST请求。 In fact, I'm not sure what the purpose of your testStream middleware is, its not even returning any response (eg res.end() ) 实际上,我不确定您的testStream中间件的用途是什么,它甚至不返回任何响应(例如res.end()

If you want to handle socket connections in a separate module you can, but instead of exporting your app's server the way you are, try passing the io instance as variable to your submodule. 如果您想在一个单独的模块中处理套接字连接,则可以将io实例作为变量传递给子模块,而不是像以前那样导出应用程序server In short, try this: 简而言之,请尝试以下操作:

app.js app.js

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var test = require('./test')(io);

app.set('port', process.env.PORT || 3000);

server.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
});

test.js test.js

module.exports = function(io) {
    io.on('connection', function(socket) {
        console.log("in socket");
        fs.readFile('E:/temp/testimg.png', function(err, buf) {
            socket.emit('image', {
                image: true,
                buffer: buf
            });
            console.log("test image");
        });
    });
};

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

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