PeerServerを使用するconnection
と、2 つのイベントを取得できますdisconnect
。これを使用して内部リストを作成し、それをアプリケーションに取得させることができます。
部分的な例:
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);}
});
someexpressapp.get('/connected-people', function (req, res) {
return res.json(connected);
});
次に、クライアント側のコードで AJAXを実行/connected-people
し、そのリストを使用できます。
メタデータについては、上記のコードを拡張して、ユーザー ステータスとそのステータスを更新する方法を追加できます。
お役に立てれば!
編集執筆時点では、イベントの名前はconnect
. と名付けられましconnection
た。
(また、PeerJS を 6 時間ほど使用する予定です。これまでに行ったことを理解していただければ幸いです。)