Michelle Bu 11 år sedan
förälder
incheckning
a17fe6380a
2 ändrade filer med 19 tillägg och 0 borttagningar
  1. 15 0
      README.md
  2. 4 0
      lib/server.js

+ 15 - 0
README.md

@@ -55,6 +55,21 @@ var server = new PeerServer({
 });
 ```
 
+### Events
+
+The `'connection'` event is emitted when a peer connects to the server.
+
+```javascript
+PeerServer#on('connection', function(id) { ... })
+```
+
+The `'disconnect'` event is emitted when a peer disconnects from the server or
+when the peer can no longer be reached.
+
+```javascript
+PeerServer#on('disconnect', function(id) { ... })
+```
+
 ## Problems?
 
 Discuss PeerJS on our Google Group:

+ 4 - 0
lib/server.js

@@ -79,6 +79,9 @@ PeerServer.prototype._initializeWSS = function() {
             self._clients[key][id] = { token: token, ip: ip };
             self._ips[ip]++;
             socket.send(JSON.stringify({ type: 'OPEN' }));
+            // We're going to emit here, because for XHR we don't *know* when someone
+            // disconnects.
+            self.emit('connection', id);
           }
           self._configureWS(socket, key, id, token);
         } else {
@@ -345,6 +348,7 @@ PeerServer.prototype._removePeer = function(key, id) {
   if (this._clients[key] && this._clients[key][id]) {
     this._ips[this._clients[key][id].ip]--;
     delete this._clients[key][id];
+    this.emit('disconnect', id);
   }
 };