Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TCP Socket Support #750

Merged
merged 5 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1606,6 +1625,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