Skip to content

Commit

Permalink
Merge pull request #10 from ptcrealitylab/parallelSockets
Browse files Browse the repository at this point in the history
Parallel sockets
  • Loading branch information
dangond-ptc authored May 7, 2024
2 parents e83ca44 + 11aadec commit 1c6bda8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "toolsocket",
"version": "2.1.0",
"version": "2.1.1",
"description": "A communication layer built on top of WebSocket for Node.js and browsers.",
"main": "src/index.js",
"scripts": {
Expand Down
48 changes: 41 additions & 7 deletions src/ToolSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,47 @@ class ToolSocketServer {
*/
requestParallelSocket(toolSocket) {
const id = generateUniqueId(8);
toolSocket.meta('requestParallel', id, null, null);
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
this.pendingParallelRequests.set(id, resolve);
return promise;
const requestTimeout = 5 * 1000;
if (toolSocket.supportsParallelSocket === undefined) {
// We don't know if the socket supports this feature due to backwards-compatibility requirements
toolSocket.meta('requestParallel', id, null, null);
let promiseResolved = false;
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
}).then(parallelSocket => {
toolSocket.supportsParallelSocket = true;
promiseResolved = true;
return parallelSocket;
});
this.pendingParallelRequests.set(id, resolve);
setTimeout(() => {
if (promiseResolved) {
return; // Do nothing if already resolved
}
this.pendingParallelRequests.delete(id);
if (toolSocket.supportsParallelSocket === undefined) {
toolSocket.supportsParallelSocket = false;
reject('Unable to create parallel socket connection. Might not be supported by client.');
} else {
reject('Unable to create parallel socket connection. Client does support it, but there\'s likely a network issue.'); // If it's succeeded at least once before, we don't need to set it to false, might just be a network issue
}
}, requestTimeout);
return promise;
} else if (toolSocket.supportsParallelSocket) {
// We know the socket supports this feature
toolSocket.meta('requestParallel', id, null, null);
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
this.pendingParallelRequests.set(id, resolve);
return promise;
} else {
// The socket does not support this feature
return Promise.reject('Unable to create parallel socket connection. Might not be supported by client.');
}
}

close() {
Expand Down

0 comments on commit 1c6bda8

Please sign in to comment.