如何在socket.io-p2p的房间内建立对等连接?

时间:2017-10-06 11:01:48

标签: node.js sockets server socket.io p2p

我使用数组存储我的连接,并且我有一个按钮(此处未包含),单击该按钮时,将前两个连接放在一个房间内。 我想在这两者之间建立一个p2p连接,并在p2p上传输一些随机文本或警报。 有没有人知道我应该怎么做? 请注意,我在html中加载了socket.io-p2p脚本,因此在这种情况下我不必使用browserify。

客户档案:

<html>

    <head>
        <script src="/socket.io/socket.io.js"></script>
        <script src="http://localhost:8000/js"></script>

        <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
        <script>
            var socket = io('http://localhost:8000');



            var p2p = new P2P(socket);




            socket.on('C1Trigger', function (data) {
                document.getElementById("A").style.backgroundColor = "green";
            });

            socket.on('C2Trigger', function (data) {
                document.getElementById("A").style.backgroundColor = "green";
            });

            socket.on('C1BYellowTrigger', function (data) {
                document.getElementById("B").style.backgroundColor = "yellow";
            });

            socket.on('C2BGreenTrigger', function (data) {


                document.getElementById("B").style.backgroundColor = "green";


            });

        </script>
    </head>

    <body>
        <div id="A">A</div>
        <div id="B">B</div>

    </body>
    <style>
        div {
            background-color: #FF0000;
            border: none;
            color: white;
            padding: 30px 60px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 60px;
        }
    </style>

    </html>

server.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 8000;
//P2P Requirements
var p2p = require('socket.io-p2p-server').Server;
io.use(p2p);

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/client.html');
});

app.get('/js', function (req, res) {
  res.sendFile(__dirname + '/socketiop2p.min.js');
});






clients = [];
clientSockets = [];






io.on('connection', function (socket) {

  clientSockets.push(socket);




  console.log('Client: ' + socket.id + ' connected.');



  clients.push(socket.id);
  io.sockets.emit('clients', clients);
  // console.log(clients);


  socket.on('executeC1', function () {
    console.log('C1 in MainRoom pinged!');
    io.to(clients[0]).emit('C1Trigger');

  });

  socket.on('executeC2', function () {
    console.log('C2 in MainRoom pinged!');
    io.to(clients[1]).emit('C2Trigger');
  });

  socket.on('greenifyBC2', function () {
    io.to(clients[1]).emit('GreenifyTrigger');
  });

  socket.on('executeC1C2', function (data) {


    console.log('C1C2 in MainRoom pinged!');

    clientSockets[0].join('room C1C2', () => {
    //expand with features
    });
    clientSockets[1].join('room C1C2', () => {
    //expand with features
    });


    io.to(clients[0]).emit('C1BYellowTrigger');

    io.to(clients[1]).emit('C2BGreenTrigger');




    console.log(io.sockets.adapter.rooms);



  });




  socket.on('disconnect', function () {

    console.log('Client: ' + socket.id + ' disconnected');

    var index = clients.indexOf(socket.id);

    if (index > -1) {
      clients[index] = undefined;
    }
    console.log(clients);

  });
});

http.listen(port, function () {
  console.log('listening on *:' + port);
});

2 个答案:

答案 0 :(得分:0)

我会使用你的socket.io房间为webrtc建立一个信号。然后我会使用webrtc进行p2p连接。

答案 1 :(得分:0)

古老的问题,但我试图找出同样的事情。根据{{​​3}},诀窍是使用告诉P2P服务器在服务器代码的on('connection')中为新连接的套接字在房间内发送信号:

io.on('connection', function(socket) {
  clients[socket.id] = socket
  var room = findOrCreateRoom() //create a room (needs a property called name)
  socket.join(room.name) // join the socket
  
  p2pserver(socket, null, room) //tell the P2P server to have that socket signal to other
  // sockets in the room and not on the common namespace.
})

希望这会帮助人们找到文档的那一部分。

我试图弄清楚如何仅在有座套的人进入房间后才开始发信号。因此,不要立即连接。不确定如何“暂停”信令...