Skip to content

Instantly share code, notes, and snippets.

@lightsofapollo
Created May 8, 2012 21:16
Show Gist options
  • Select an option

  • Save lightsofapollo/2639365 to your computer and use it in GitHub Desktop.

Select an option

Save lightsofapollo/2639365 to your computer and use it in GitHub Desktop.
TCP Example
var Abstract, CommandStream;
Abstract = require('./abstract').Marionette.Drivers.Abstract;
CommandStream = require('../../node/command-stream');
/** TCP **/
Tcp.Socket = require('net').Socket;
function Tcp(options) {
if (typeof(options)) {
options = {};
}
Abstract.call(this, options);
this.connectionId = 0;
this.host = options.host || 'localhost';
this.port = options.port || 2828;
}
Tcp.prototype = Object.create(Abstract.prototype);
/**
* Sends a command to the websocket server.
*
* @param {Object} command remote marionette command.
* @private
*/
Tcp.prototype._sendCommand = function _sendCommand(cmd) {
this.client.send(cmd);
};
Tcp.prototype._connect = function connect() {
var client, self = this;
this.socket = new Tcp.Socket();
this.socket.connect(this.port, this.host);
client = this.client = new CommandStream(this.socket);
this.client.on('command', this._onClientCommand.bind(this));
};
Tcp.prototype._onClientCommand = function(data) {
this._onDeviceResponse({
id: this.connectionId,
response: data
});
}
/**
* Closes connection to marionette.
*/
Tcp.prototype._close = function close() {
if (this.socket && this.socket.destroy) {
this.socket.destroy();
}
};
module.exports = exports = Tcp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment