Skip to content

Commit

Permalink
Allow patching the websocket class
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Feb 11, 2024
1 parent e0a207e commit 64ce7b9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imjoy-rpc",
"version": "0.5.46",
"version": "0.5.47",
"description": "Remote procedure calls for ImJoy.",
"module": "index.js",
"types": "index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion javascript/src/hypha/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,9 @@ export class RPC extends MessageEmitter {
) {
let target_id = encoded_method._rtarget;
if (remote_workspace && !target_id.includes("/")) {
target_id = remote_workspace + "/" + target_id;
if (remote_workspace !== target_id) {
target_id = remote_workspace + "/" + target_id;
}
// Fix the target id to be an absolute id
encoded_method._rtarget = target_id;
}
Expand Down
16 changes: 13 additions & 3 deletions javascript/src/hypha/websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export { getRTCService, registerRTCService };
const MAX_RETRY = 10000;

class WebsocketRPCConnection {
constructor(server_url, client_id, workspace, token, timeout = 60) {
constructor(
server_url,
client_id,
workspace,
token,
timeout = 60,
WebSocketClass = null
) {
assert(server_url && client_id, "server_url and client_id are required");
server_url = server_url + "?client_id=" + client_id;
if (workspace) {
Expand All @@ -27,6 +34,8 @@ class WebsocketRPCConnection {
this._opening = null;
this._retry_count = 0;
this._closing = false;
// Allow to override the WebSocket class for mocking or testing
this._WebSocketClass = WebSocketClass || WebSocket;
}

set_reconnection_token(token) {
Expand All @@ -48,7 +57,7 @@ class WebsocketRPCConnection {
: this._server_url;
console.info("Creating a new connection to ", server_url.split("?")[0]);

const websocket = new WebSocket(server_url);
const websocket = new this._WebSocketClass(server_url);
websocket.binaryType = "arraybuffer";
websocket.onmessage = event => {
const data = event.data;
Expand Down Expand Up @@ -180,7 +189,8 @@ export async function connectToServer(config) {
clientId,
config.workspace,
config.token,
config.method_timeout || 60
config.method_timeout || 60,
config.WebSocketClass,
);
await connection.open();
const rpc = new RPC(connection, {
Expand Down
2 changes: 1 addition & 1 deletion python/imjoy_rpc/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.5.46"
"version": "0.5.47"
}
3 changes: 2 additions & 1 deletion python/imjoy_rpc/hypha/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ def _generate_remote_method(
"""Return remote method."""
target_id = encoded_method["_rtarget"]
if remote_workspace and "/" not in target_id:
target_id = remote_workspace + "/" + target_id
if remote_workspace != target_id:
target_id = remote_workspace + "/" + target_id
# Fix the target id to be an absolute id
encoded_method["_rtarget"] = target_id
method_id = encoded_method["_rmethod"]
Expand Down

0 comments on commit 64ce7b9

Please sign in to comment.