Safe constructor: net.Server, net.Stream
This commit is contained in:
parent
f86ad1693f
commit
4fdebab005
@ -513,6 +513,7 @@ function initStream (self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Stream (fd, type) {
|
function Stream (fd, type) {
|
||||||
|
if (!(this instanceof Stream)) return new Stream(fd, type);
|
||||||
events.EventEmitter.call(this);
|
events.EventEmitter.call(this);
|
||||||
|
|
||||||
this.fd = null;
|
this.fd = null;
|
||||||
@ -1032,6 +1033,7 @@ Stream.prototype.end = function (data, encoding) {
|
|||||||
|
|
||||||
|
|
||||||
function Server (listener) {
|
function Server (listener) {
|
||||||
|
if (!(this instanceof Server)) return new Server(listener);
|
||||||
events.EventEmitter.call(this);
|
events.EventEmitter.call(this);
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
common = require("../common");
|
common = require("../common");
|
||||||
assert = common.assert
|
assert = common.assert
|
||||||
tcp = require("tcp");
|
net = require("net");
|
||||||
|
|
||||||
binaryString = "";
|
binaryString = "";
|
||||||
for (var i = 255; i >= 0; i--) {
|
for (var i = 255; i >= 0; i--) {
|
||||||
@ -19,7 +19,8 @@ for (var i = 255; i >= 0; i--) {
|
|||||||
binaryString += S;
|
binaryString += S;
|
||||||
}
|
}
|
||||||
|
|
||||||
var echoServer = tcp.createServer(function (connection) {
|
// safe constructor
|
||||||
|
var echoServer = net.Server(function (connection) {
|
||||||
connection.setEncoding("binary");
|
connection.setEncoding("binary");
|
||||||
connection.addListener("data", function (chunk) {
|
connection.addListener("data", function (chunk) {
|
||||||
common.error("recved: " + JSON.stringify(chunk));
|
common.error("recved: " + JSON.stringify(chunk));
|
||||||
@ -35,7 +36,7 @@ var recv = "";
|
|||||||
|
|
||||||
echoServer.addListener("listening", function() {
|
echoServer.addListener("listening", function() {
|
||||||
var j = 0;
|
var j = 0;
|
||||||
var c = tcp.createConnection(common.PORT);
|
var c = net.createConnection(common.PORT);
|
||||||
|
|
||||||
c.setEncoding("binary");
|
c.setEncoding("binary");
|
||||||
c.addListener("data", function (chunk) {
|
c.addListener("data", function (chunk) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user