create writer synchronously, win detection fix
This commit is contained in:
parent
00fd61add0
commit
fea769a271
10
README.md
10
README.md
@ -11,12 +11,12 @@ Similar to `stream-pair`, but with sockets with real fds. A workaround for <http
|
||||
```javascript
|
||||
var socketPair = require('socket-pair');
|
||||
|
||||
socketPair.create(function (err, pair) {
|
||||
var a = pair.client; // as in `client = net.connect()`
|
||||
var b = pair.connection; // as in `server.on('connection', function (conn) { ... })`
|
||||
var socket = socketPair.create(function (err, other) {
|
||||
// socket as in `client = net.connect()`
|
||||
// other as in `server.on('connection', function (conn) { ... })`
|
||||
|
||||
a.write('123');
|
||||
b.on('data', function (chunk) {
|
||||
socket.write('123');
|
||||
other.on('data', function (chunk) {
|
||||
console.log(chunk.toString('utf8'));
|
||||
});
|
||||
|
||||
|
@ -10,7 +10,7 @@ function createServer(cb, prefix) {
|
||||
var path = require('path');
|
||||
var sockname = (prefix || 'node-socket-pair') + '.' + require('crypto').randomBytes(16).toString('hex') + '.sock';
|
||||
|
||||
if (/win/.test(os.platform())) {
|
||||
if (/^win/.test(os.platform())) {
|
||||
sock = path.join('\\\\?\\pipe', process.cwd(), sockname);
|
||||
}
|
||||
else {
|
||||
@ -30,13 +30,13 @@ function createServer(cb, prefix) {
|
||||
|
||||
exports.create = function create(cb, prefix) {
|
||||
var net = require('net');
|
||||
var client;
|
||||
var client = new net.Socket();
|
||||
|
||||
function createConnection() {
|
||||
function onClientError(err) {
|
||||
cb(err);
|
||||
}
|
||||
client = net.connect(sock, function () {
|
||||
client.connect(sock, function () {
|
||||
client.removeListener('error', onClientError);
|
||||
});
|
||||
client.once('error', onClientError);
|
||||
@ -48,7 +48,7 @@ exports.create = function create(cb, prefix) {
|
||||
}
|
||||
|
||||
server.once('connection', function onEach(connection) {
|
||||
cb(null, { client: client, connection: connection });
|
||||
cb(null, connection);
|
||||
});
|
||||
|
||||
if (!listening) {
|
||||
@ -57,6 +57,8 @@ exports.create = function create(cb, prefix) {
|
||||
else {
|
||||
createConnection();
|
||||
}
|
||||
|
||||
return client;
|
||||
};
|
||||
|
||||
exports.closeAll = function () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user