简体   繁体   English

无法使socket.io-emitter与socket.io-redis一起使用

[英]cannot get socket.io-emitter to work with socket.io-redis

i have a pretty simple code, trying to emit from an external process into a socket.io server. 我有一个非常简单的代码,试图从外部进程发出到socket.io服务器。

server.js server.js

var io = require('socket.io')(3201);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

io.on('connection', function (socket) {
  console.log("test 1");
  socket.on('testemit', function (data) {
      console.log("test 2");
      console.log(data);
  });
});

the client, emit.js (as found in the docs): 客户端,emit.js(在文档中找到):

var io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
setInterval(function(){
      io.emit('testemit', {a: 'aaa'});
}, 3000);

on the server side, if i run it with 在服务器端,如果我用

 DEBUG=* node server.js

all i get is: 我所得到的是:

socket.io:server initializing namespace / +0ms
  socket.io:server creating http server and binding to 3201 +3ms
  socket.io:server creating engine.io instance with opts {"path":"/socket.io"} +1ms
  socket.io:server attaching client serving req handler +2ms
  socket.io-parser encoding packet  aaa {"type":2,"data":["testemit",{"a":"aaa"}],"nsp":"/"} +0ms
  socket.io-parser encoded {"type":2,"data":["testemit",{"a":"aaa"}],"nsp":"/"} as 2["testemit",{"a":"aaa"}] +1ms

but the callback in for testemit does not get called at all. 但是testemit中的回调根本不会被调用。

what am i missing? 我想念什么?

i completely misunderstood this. 我完全误解了这一点。 all works. 所有作品。

the emit does an emit to all the socket.io clients of this socket.io server. 这个emit向这个socket.io服务器的所有socket.io客户端发出一个emission。

so in the browser it works, like this: 因此在浏览器中可以正常运行,如下所示:

        _socket = io.connect(url);
        _socket.on('testemit', function (message) {
            console.log('testemit');
            console.log(message);
        });

i am wrongly expecting it to work in the server too. 我错误地期望它也可以在服务器中工作。

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

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