forked from alecgorge/nodecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.js
42 lines (34 loc) · 894 Bytes
/
session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var EventEmitter = require('events').EventEmitter;
var sys = require('util');
var Session = function (world, stream) {
this.world = world;
this.stream = stream;
this.uid = world.uidgen.allocate();
this.player = {};
this.outgoingQueue = [];
this.closed = false;
};
Session.prototype = new EventEmitter();
/* pump the outgoing message queue */
Session.prototype.pump = function () {
if (!this.outgoingQueue.length) {
return;
}
var item = this.outgoingQueue.shift();
var me = this;
// Cancel all low-priority sends if the client has disconnected
if (this.closed) {
this.outgoingQueue = [];
return;
}
// Defer the next low-priority item for better server responsiveness
item(function () {
process.nextTick(function () {
me.pump();
});
});
};
Session.prototype.addOutgoing = function (tocall) {
this.outgoingQueue.push(tocall);
};
exports.Session = Session;