io.of('namespace').emit('event', message) 不适用于 socket.io 中的命名空间 [英] io.of('namespace').emit('event', message) not working with namespace in socket.io

查看:20
本文介绍了io.of('namespace').emit('event', message) 不适用于 socket.io 中的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的应用程序:

I have an app like this following:

io.of('/hello').on('connection', function(socket) {
    socket.emit('world', {});
});

app.post('/', function *(next) {
    console.log("At here......");
    var pushMessage = (yield parse.json(this));
    console.log(pushMessage);
    if(flag !== 0) {
//        io.of('/hello/').emit('world', pushMessage);
        io.sockets.emit('world', pushMessage);
    } else {
        console.log("Do Nothing");
    }
});

它接收一个 http 请求并发出一个事件.当我使用 io.sockets.emit 时它运行良好,但是当我使用 'io.of('hello').emit' 指定命名空间时它不起作用,为什么?

It receive a http request and emit an event. When I use io.sockets.emit it works well but when I specify a namespace with 'io.of('hello').emit' it doesn't work,why?

我的客户端是这样的:

var socket = io.connect('http://localhost:3000', {
  'reconnection delay': 100,
  'reconnection limit': 100,
  'max reconnection attempts': 10
});
//server side use io.sockets.emit
socket.on('world', function(data) {
  alert(data.a);
});

//if server side use io.of('/hello/').emit
//socket.of('/hello/').on('world', function(data) {
//  alert(data.a);
//});

推荐答案

你的代码或多或少不错,但你在不同的命名空间上.

Your code is more or less fine, but you are on different namespaces.

io.sockets.emit() 向当前通过套接字连接到服务器的每个人广播.这就是它起作用的原因.从技术上讲,这是因为这是 io.of('').emit() 的捷径"('' 是命名空间).

io.sockets.emit() broadcasts to everybody currently connected to your server via socket. That's the reason it works. Technically it's because that's a 'shortcut' for io.of('').emit() ('' being the namespace).

假设您要使用 /hello 命名空间,这就是您必须在客户端上执行的操作:

Assuming you're going to use the /hello namespace, this is what you have to do on your client:

var socket = io.connect('http://localhost:3000/hello'); // your namespace is /hello

在服务器上,您首先必须侦听该命名空间上的连接:

on the server you first have to listen for connections on that namespace:

io.of('/hello').on('connection', function(socket) {
  socket.emit('world', { a: 'hi world' });
});

然后:

io.of('/hello').emit('something');

你可能想看看这些:socket.io:如何使用socket.io 房间

### 更新 ###

我进行了一个小测试:

客户:

$('document').ready(function() {
  var socket = io.connect("localhost:3000/hello");

  socket.on('hello', function() {
    console.log('hello received');
  });

  var data = {};
  data.title = "title";
  data.message = "message";

  setTimeout(function() {
    $.ajax({
      type: 'POST',
      data: JSON.stringify(data),
      contentType: 'application/json',
      url: 'http://localhost:3000/hello',
      success: function(data) {
        console.log('success');
        console.log(JSON.stringify(data));
      }
    });
   }, 2000);
});

服务器:

io.of('/hello').on('connection', function() {
  console.log("client connected");
});

app.post('/hello', function(req, res) {
  io.of('/hello').emit('hello');
});

...它奏效了.我从这里复制了 jquery-ajax 代码.

... and it worked. I copied the jquery-ajax code from here.

这篇关于io.of('namespace').emit('event', message) 不适用于 socket.io 中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆