Merge pull request #29 from peers/events

Emit connection/disconnect events.
This commit is contained in:
Michelle Bu 2014-01-05 12:00:35 -08:00
commit 1f246028e4
2 changed files with 19 additions and 0 deletions

View File

@ -55,6 +55,21 @@ var server = new PeerServer({
}); });
``` ```
### Events
The `'connection'` event is emitted when a peer connects to the server.
```javascript
server.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
server.on('disconnect', function(id) { ... })
```
## Problems? ## Problems?
Discuss PeerJS on our Google Group: Discuss PeerJS on our Google Group:

View File

@ -78,6 +78,9 @@ PeerServer.prototype._initializeWSS = function() {
self._clients[key][id] = { token: token, ip: ip }; self._clients[key][id] = { token: token, ip: ip };
self._ips[ip]++; self._ips[ip]++;
socket.send(JSON.stringify({ type: 'OPEN' })); 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); self._configureWS(socket, key, id, token);
} else { } else {
@ -344,6 +347,7 @@ PeerServer.prototype._removePeer = function(key, id) {
if (this._clients[key] && this._clients[key][id]) { if (this._clients[key] && this._clients[key][id]) {
this._ips[this._clients[key][id].ip]--; this._ips[this._clients[key][id].ip]--;
delete this._clients[key][id]; delete this._clients[key][id];
this.emit('disconnect', id);
} }
}; };