本文整理汇总了TypeScript中stream.Duplex.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Duplex.on方法的具体用法?TypeScript Duplex.on怎么用?TypeScript Duplex.on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Duplex
的用法示例。
在下文中一共展示了Duplex.on方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
}, function(client) {
var stream;
var document = null;
log.debug("New Client Connected");
log.debug(client);
var rawSessionCookie = cookie.parse(client.headers.cookie)['connect.sid'];
var sessionId = signature.unsign(rawSessionCookie.slice(2), options.sessionSecret);
if (sessionId == false) {
// Invalid session
log.error({message:"Tried to get sessionId that doesn't exist",value:rawSessionCookie});
client.stop();
return;
}
stream = new Duplex({
objectMode: true
});
stream._write = function(chunk, encoding, callback) {
log.debug('s->c ', JSON.stringify(chunk));
if (client.state !== 'closed') {
client.send(chunk);
} else {
log.debug("CLIENT IS CLOSED");
}
callback();
};
stream._read = function() {};
stream.headers = client.headers;
stream.remoteAddress = stream.address;
client.on('message', function(data) {
log.debug('c->s ', JSON.stringify(data));
if (data['a']=='sub' || data['a']=='bs') {
if (data['a']=='sub') {
// User is subscribing to a new document
log.debug("Got new sub");
document = data['d'];
} else { // data['a']=='bs'
var collectionDocumentVersionMap = data['s'];
var numCollections = Object.keys(collectionDocumentVersionMap).length;
if (numCollections != 1) {
log.error({message:"Zero or more than one collection not expected",value:numCollections});
client.stop();
return;
}
var cName = Object.keys(collectionDocumentVersionMap)[0];
var numDocuments = Object.keys(collectionDocumentVersionMap[cName]).length;
if (numDocuments != 1) {
log.error({message:"Zero or more than one document not expected",value:numDocuments});
client.stop();
return;
}
var docName = Object.keys(collectionDocumentVersionMap[cName])[0];
document = docName;
}
mongoStore.get(sessionId, function(err, session) {
if (err) {
log.error(err);
client.stop();
return;
}
if (!session) {
log.error({message:"Tried to get session that doesn't exist",value:rawSessionCookie});
client.stop();
return;
}
var userId = session.passport.user;
if (!userId) {
log.error({message:"Tried to get userId that doesn't exist",value:session});
client.stop();
return;
}
AuthHelper.userIdCanAccessPageId(userId, document, function(canAccess) {
if (!canAccess) {
client.stop();
return;
}
pageConnectionMap[document] = pageConnectionMap[document] ?
pageConnectionMap[document]+1 :
1;
log.info(pageConnectionMap[document] + " CLIENTS CONNECTED TO " + document);
stream.push(data);
});
});
} else {
stream.push(data);
}
});
stream.on('error', function(msg) {
log.info("GOT CLIENT ERROR: " + msg);
client.stop();
});
client.on('close', function(reason) {
stream.push(null);
stream.emit('close');
log.debug('client went away');
});
stream.on('end', function() {
log.debug("CLIENT END");
if (document) {
pageConnectionMap[document]--;
log.info(pageConnectionMap[document] + " CLIENTS ARE CONNECTED TO " + document + "\n");
//.........这里部分代码省略.........
示例2: connect
/**
* Starts the connection establishment to the proxy and destination.
* @param existing_socket Connected socket to use instead of creating a new one (internal use).
*/
public connect(existing_socket?: Duplex) {
this._onDataReceived = (data: Buffer) => this.onDataReceived(data);
this._onClose = () => this.onClose();
this._onError = (err: Error) => this.onError(err);
this._onConnect = () => this.onConnect();
// Start timeout timer (defaults to 30 seconds)
const timer = setTimeout(
() => this.onEstablishedTimeout(),
this._options.timeout || DEFAULT_TIMEOUT
);
// check whether unref is available as it differs from browser to NodeJS (#33)
if (timer.unref && typeof timer.unref === 'function') {
timer.unref();
}
// If an existing socket is provided, use it to negotiate SOCKS handshake. Otherwise create a new Socket.
if (existing_socket) {
this._socket = existing_socket;
} else {
this._socket = new net.Socket();
}
// Attach Socket error handlers.
this._socket.once('close', this._onClose);
this._socket.once('error', this._onError);
this._socket.once('connect', this._onConnect);
this._socket.on('data', this._onDataReceived);
this.state = SocksClientState.Connecting;
this._receiveBuffer = new ReceiveBuffer();
if (existing_socket) {
this._socket.emit('connect');
} else {
(this._socket as net.Socket).connect(
this._options.proxy.port,
this._options.proxy.host || this._options.proxy.ipaddress
);
if (
this._options.set_tcp_nodelay !== undefined &&
this._options.set_tcp_nodelay !== null
) {
(this._socket as net.Socket).setNoDelay(
!!this._options.set_tcp_nodelay
);
}
}
// Listen for established event so we can re-emit any excess data received during handshakes.
this.prependOnceListener('established', info => {
setImmediate(() => {
if (this._receiveBuffer.length > 0) {
const excessData = this._receiveBuffer.get(
this._receiveBuffer.length
);
info.socket.emit('data', excessData);
}
info.socket.resume();
});
});
}