0
私は配列との接続を保存しています。ここには含まれていないボタンがあります。このボタンをクリックすると、最初の2つの接続が部屋の中に配置されます。 私はそれらの2つの間でp2p接続を確立し、p2pを介していくつかのランダムなテキストまたはアラートを転送したいと考えています。 誰かが私がそれをどうするべきか考えている人はいますか? この場合、browserifyを使用する必要がないように、htmlにsocket.io-p2pスクリプトをロードしています。socket.io-p2pで部屋内のピア接続を確立するにはどうすればよいですか?
クライアントファイル:
<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);
});