mirror of
https://github.com/danbulant/node-x11
synced 2026-06-21 15:52:04 +00:00
Fall back to TCP if socket does not exist
Also have client emit error on stream error if there is no initCb.
This commit is contained in:
parent
fb817b8e2b
commit
2d7d6cbc8f
1 changed files with 35 additions and 20 deletions
55
lib/xcore.js
55
lib/xcore.js
|
|
@ -23,7 +23,6 @@ var em = require('./eventmask').eventMask;
|
||||||
function XClient(stream, displayNum, screenNum, options)
|
function XClient(stream, displayNum, screenNum, options)
|
||||||
{
|
{
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
this.stream = stream;
|
|
||||||
this.options = options ? options : {};
|
this.options = options ? options : {};
|
||||||
|
|
||||||
// TODO: this is probably not used
|
// TODO: this is probably not used
|
||||||
|
|
@ -33,6 +32,12 @@ function XClient(stream, displayNum, screenNum, options)
|
||||||
this.displayNum = displayNum;
|
this.displayNum = displayNum;
|
||||||
this.screenNum = screenNum;
|
this.screenNum = screenNum;
|
||||||
this.authHost = os.hostname();
|
this.authHost = os.hostname();
|
||||||
|
}
|
||||||
|
util.inherits(XClient, EventEmitter);
|
||||||
|
|
||||||
|
XClient.prototype.init = function(stream)
|
||||||
|
{
|
||||||
|
this.stream = stream;
|
||||||
|
|
||||||
var pack_stream = new PackStream();
|
var pack_stream = new PackStream();
|
||||||
|
|
||||||
|
|
@ -108,7 +113,6 @@ function XClient(stream, displayNum, screenNum, options)
|
||||||
this.startHandshake();
|
this.startHandshake();
|
||||||
this._closing = false;
|
this._closing = false;
|
||||||
}
|
}
|
||||||
util.inherits(XClient, EventEmitter);
|
|
||||||
|
|
||||||
// TODO: close() = set 'closing' flag, watch it in replies and writeQueue, terminate if empty
|
// TODO: close() = set 'closing' flag, watch it in replies and writeQueue, terminate if empty
|
||||||
XClient.prototype.terminate = function()
|
XClient.prototype.terminate = function()
|
||||||
|
|
@ -569,6 +573,8 @@ module.exports.createClient = function(options, initCb)
|
||||||
|
|
||||||
// open stream
|
// open stream
|
||||||
var stream;
|
var stream;
|
||||||
|
var connected = false;
|
||||||
|
var cbCalled = false;
|
||||||
var socketPath;
|
var socketPath;
|
||||||
|
|
||||||
// try local socket on non-windows platforms
|
// try local socket on non-windows platforms
|
||||||
|
|
@ -585,18 +591,35 @@ module.exports.createClient = function(options, initCb)
|
||||||
socketPath = '/tmp/.X11-unix/X' + displayNum;
|
socketPath = '/tmp/.X11-unix/X' + displayNum;
|
||||||
}
|
}
|
||||||
//socketPath = '/tmp/.X11-unix/X' + displayNum;
|
//socketPath = '/tmp/.X11-unix/X' + displayNum;
|
||||||
if(socketPath)
|
var client = new XClient(displayNum, screenNum, options);
|
||||||
{
|
|
||||||
stream = net.createConnection(socketPath);
|
var connectStream = function() {
|
||||||
}
|
if (socketPath) {
|
||||||
else
|
stream = net.createConnection(socketPath);
|
||||||
{
|
} else {
|
||||||
stream = net.createConnection(6000 + parseInt(displayNum), host);
|
stream = net.createConnection(6000 + parseInt(displayNum), host);
|
||||||
}
|
}
|
||||||
var client = new XClient(stream, displayNum, screenNum, options);
|
stream.on('connect', function() {
|
||||||
|
connected = true;
|
||||||
|
client.init(stream);
|
||||||
|
});
|
||||||
|
stream.on('error', function(err) {
|
||||||
|
if (!connected && socketPath && err.code === 'ENOENT') {
|
||||||
|
// Retry connection with TCP on localhost
|
||||||
|
socketPath = null;
|
||||||
|
host = 'localhost';
|
||||||
|
connectStream();
|
||||||
|
} else if (initCb && !cbCalled) {
|
||||||
|
cbCalled = true;
|
||||||
|
initCb(err);
|
||||||
|
} else {
|
||||||
|
client.emit('error', err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
connectStream();
|
||||||
if (initCb)
|
if (initCb)
|
||||||
{
|
{
|
||||||
var cbCalled = false;
|
|
||||||
client.on('connect', function(display) {
|
client.on('connect', function(display) {
|
||||||
// opt-in BigReq
|
// opt-in BigReq
|
||||||
if (!options.disableBigRequests) {
|
if (!options.disableBigRequests) {
|
||||||
|
|
@ -614,14 +637,6 @@ module.exports.createClient = function(options, initCb)
|
||||||
initCb(undefined, display);
|
initCb(undefined, display);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
stream.on('error', function(err) {
|
|
||||||
if (cbCalled)
|
|
||||||
client.emit('error', err);
|
|
||||||
else {
|
|
||||||
cbCalled = true;
|
|
||||||
initCb(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue