私はゆっくりとプロジェクトの構造を設定しています。また、pingソース以外のすべてのユーザーに警告を送ることができますが、特定のユーザーに警告を送る必要があります。私はこれを行う最善の方法は特定のsocket.idにpingすることだと仮定します。どのようにこれを行うにはどのようなアイデア?私はエラーが発生し続けるので、socket.broadcast.to(socket.id)がセットアップで動作しないように見えます(未定義の 'to'プロパティを読み取れません)。特定のsocket.idにのみメッセージを送信するにはどうすればよいですか?
client.js
// BROWSERIFY
//DEPENDENCIES
var P2P = require("socket.io-p2p");
var io = require("socket.io-client");
var ss = require("socket.io-stream");
var socket = io();
var opts = { autoUpgrade: true, peerOpts: { numClients: 10 } };
var p2p = new P2P(socket, opts);
var $ = require("jquery");
//Handler for the ping button
var pingRoom = function() {
socket.emit('pingAlert');
};
window.pingRoom = pingRoom;
var pingTwo = function() {
socket.emit('pingAlertTwo');
};
window.pingTwo = pingTwo;
////////////////////////////////////////////////////////
//Intercepts the pingBack event from the server side
socket.on('pingBack', function (data) {
alert("ALERT");
});
socket.on('pingBackTwo', function (data) {
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
});
////////////////////////////////////////////////////////
//Peer number counter (incomplete functionality)
p2p.on("peer-num", function (num) {
console.log("You are peer number " + num);
$(".peerNum").html("You are connected to " + num + " peers.");
});
////////////////////////////////////////////////////////
//Appends stream to element
p2p.on("file", function (stream) {
//console.log(stream);
var img = document.createElement("img");
img.src = (window.URL || window.webkitURL).createObjectURL(new Blob(stream));
document.getElementById("receivedImages").appendChild(img);
});
////////////////////////////////////////////////////////
//Converts file to binary stream and logs progress in the console
$(function() {
$("#file").change(function (e) {
ss.forceBase64 = true;
var file = e.target.files[0];
var stream = ss.createStream();
ss(socket).emit("file", stream, { size: file.size, name: file.name });
var blobStream = ss.createBlobReadStream(file);
var size = 0;
blobStream.on("data", function (chunk) {
size += chunk.length;
console.log(Math.floor(size/file.size * 100) + "%");
});
blobStream.pipe(stream);
});
});
////////////////////////////////////////////////////////
//Logs users in the user log
socket.on('users_log', function (data) {
$('#log').append(data + "<br>");
console.log(data);
});
////////////////////////////////////////////////////////
server.js
//DEPENDENCIES
var app = require("express")();
var express = require("express");
var server = require("http").Server(app);
var p2pserver = require("socket.io-p2p-server").Server;
var io = require("socket.io")(server);
var ss = require("socket.io-stream");
var path = require("path");
//Added configuration
app.use(express.static(__dirname));
app.use(express.static("server_scripts"));
app.use(express.static("client_scripts"));
io.use(p2pserver);
//Peer number
var peerNum = 0;
////////////////////////////////////////////////////////
//Connections and disconnections
io.on("connection", function (socket) {
//Increments the peer number for connected peers
console.log("Peer " + peerNum + " connected");
io.emit("peer-num", peerNum);
peerNum++;
////////////////////////////////////////////////////////
//Streamer
ss(socket).on("file", function (stream, data) {
var filename = path.basename(data.name);
var parts = [];
stream.on("data", function (data) {
parts.push(data);
});
stream.on("end", function() {
socket.broadcast.emit("file", parts);
});
});
////////////////////////////////////////////////////////
//Holds the socket.id of connections
ID = socket.id;
////////////////////////////////////////////////////////
//Emits connection status to user end (User ID log)
io.sockets.emit('users_log', "client id - " + ID + " connected");
io.sockets.emit('users_online', ID);
////////////////////////////////////////////////////////
//Server side logging of connections
console.log('client id - ' + ID + ' connected.');
////////////////////////////////////////////////////////
//Alert listener and response
socket.on('pingAlert', function() {
console.log('Ping');
io.sockets.emit('pingBack');
});
socket.on('pingAlertTwo', function() {
console.log('PingTwo');
io.sockets.emit('pingBackTwo');
});
////////////////////////////////////////////////////////
//Handles disconnections
socket.on('disconnect', function() {
//Emits disconnection to user end (User ID log)
io.sockets.emit('users_log', "client id - " + ID + " disconnected");
//Decreases peer counter on disconnect
peerNum--;
////////////////////////////////////////////////////////
//Server side logging of disconnections
console.log('client id - ' + ID + ' disconnected.')
io.sockets.emit('users_offline', ID);
//NOTE: Features need to be added here
////////////////////////////////////////////////////////
})
});
//Listen on
server.listen(8000, function() {
console.log("Listening on 8000")
});
私のコードを少し変更しましたが、この回答が私には最も近いソリューションになりました。 これで正常に動作します。ありがとう。 –