Skip to content

Commit

Permalink
Add TCP Socket Support (#750)
Browse files Browse the repository at this point in the history
Add new `qz.socket` API
Move method enum out of client class
Add disclaimer about Socket API usage

Co-authored-by: Berenz <[email protected]>
  • Loading branch information
tresf and Berenz authored Mar 16, 2021
1 parent d16df4a commit d2692ad
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 277 deletions.
100 changes: 99 additions & 1 deletion js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var qz = (function() {

//stream types
streams: {
serial: 'SERIAL', usb: 'USB', hid: 'HID', printer: 'PRINTER', file: 'FILE'
serial: 'SERIAL', usb: 'USB', hid: 'HID', printer: 'PRINTER', file: 'FILE', socket: 'SOCKET'
},


Expand Down Expand Up @@ -296,6 +296,9 @@ var qz = (function() {

_qz.serial.callSerial(JSON.parse(returned.event));
break;
case _qz.streams.socket:
_qz.socket.callSocket(JSON.parse(returned.event));
break;
case _qz.streams.usb:
if (!returned.event) {
returned.event = JSON.stringify({ vendorId: returned.key[0], productId: returned.key[1], output: returned.data });
Expand Down Expand Up @@ -472,6 +475,22 @@ var qz = (function() {
},


socket: {
/** List of functions called when receiving data from network socket connection. */
socketCallbacks: [],
/** Calls all functions registered to listen for network socket events. */
callSocket: function(socketEvent) {
if (Array.isArray(_qz.socket.socketCallbacks)) {
for(var i = 0; i < _qz.socket.socketCallbacks.length; i++) {
_qz.socket.socketCallbacks[i](socketEvent);
}
} else {
_qz.socket.socketCallbacks(socketEvent);
}
}
},


usb: {
/** List of functions called when receiving data from usb connection. */
usbCallbacks: [],
Expand Down Expand Up @@ -1608,6 +1627,85 @@ var qz = (function() {
}
},

/**
* Calls related to interaction with communication sockets.
* @namespace qz.socket
*/
socket: {
/**
* Opens a network port for sending and receiving data.
*
* @param {string} host The connection hostname.
* @param {number} port The connection port number.
* @param {Object} [options] Network socket configuration.
* @param {string} [options.encoding='UTF-8'] Character set for communications.
*
* @memberof qz.socket
*/
open: function(host, port, options) {
var params = {
host: host,
port: port,
options: options
};
return _qz.websocket.dataPromise("socket.open", params);
},

/**
* @param {string} host The connection hostname.
* @param {number} port The connection port number.
*
* @memberof qz.socket
*/
close: function(host, port) {
var params = {
host: host,
port: port
};
return _qz.websocket.dataPromise("socket.close", params);
},

/**
* Send data over an open socket.
*
* @param {string} host The connection hostname.
* @param {number} port The connection port number.
* @param {string|Object} data Data to be sent over the port.
* @param {string} [data.type='PLAIN'] Valid values <code>[PLAIN]</code>
* @param {string} data.data Data to be sent over the port.
*
* @memberof qz.socket
*/
sendData: function(host, port, data) {
if (typeof data !== 'object') {
data = {
data: data,
type: "PLAIN"
};
}

var params = {
host: host,
port: port,
data: data
};
return _qz.websocket.dataPromise("socket.sendData", params);
},

/**
* List of functions called for any response from open network sockets.
* Event data will contain <code>{string} host</code> and <code>{number} port</code> for all types.
* For RECEIVE types, <code>{string} response</code>.
* For ERROR types, <code>{string} exception</code>.
*
* @param {Function|Array<Function>} calls Single or array of <code>Function({Object} eventData)</code> calls.
*
* @memberof qz.socket
*/
setSocketCallbacks: function(calls) {
_qz.socket.socketCallbacks = calls;
}
},

/**
* Calls related to interaction with USB devices.
Expand Down
Loading

0 comments on commit d2692ad

Please sign in to comment.