node.js - 如何使用 Peer.js 向特定对等方发送通知

标签 node.js express peerjs

我正在尝试为连接到我的服务器的每个对等方发送特定通知。

我尝试以与对等客户端相同的方式发送通知,但其方法不受支持。如何从服务器向特定对等点发送通知?

var PeerServer = require('peer').PeerServer;
var server = new PeerServer({port: 9000, path: '/myapp'});
var connected = [];
server.on('connection', function (id) {
  var idx = connected.indexOf(id); // only add id if it's not in the list yet
  if (idx === -1) {connected.push(id);}
});
server.on('disconnect', function (id) {
  var idx = connected.indexOf(id); // only attempt to remove id if it's in the list
  if (idx !== -1) {connected.splice(idx, 1);}
});

var app = express();

var turn = {
  type: 1,
  selection: "NINGUNA",
  timestamp: new Date().getTime()
};

app.get('/connected-people', function (req, res) {
  var list = connected;

  for(var i = 0; i < connected.length; i++){    
    var conn = server.connect(connected[i]);
    console.log(connected[i]);
    conn.on('open', function(){
      conn.send(turn);
    });
  }

  return res.json(connected);
});

但是这样,通知就不会发送给对等方。 有办法做到吗?

最佳答案

我没有足够的代表来发表评论,但看看你想要做什么,我认为你可以使用 socket.io用于直接服务器到对等通信。

查看 PeerJS 的文档表明

To broker connections, PeerJS connects to a PeerServer. Note that no peer-to-peer data goes through the server; The server acts only as a connection broker.

它似乎没有设置为处理与对等点的直接通信,但 socket.io 可以。

关于node.js - 如何使用 Peer.js 向特定对等方发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34337553/

相关文章:

npm - 如何创建和运行自己的peerjs服务器?

node.js - 如何解决在 vps 上设置 WebRTC 服务器的问题?

http - 开发网络服务器有哪些不同的方法?

node.js - 未收到 Firebase 云消息传递收据

javascript - Express 的 API 发布问题

node.js - 将项目发布到 JSON 服务器数据库 - Angular

javascript - 只有一帧显示 WebRTC 流

javascript - 在 node.js 中,什么时候使用事件,什么时候使用直接回调函数?

node.js - Socket IO 不从手机发出事件

javascript - Socket.io 随机聊天