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

Refactoring connecting websocket #293

Merged
merged 1 commit into from
Sep 26, 2022
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
4 changes: 1 addition & 3 deletions packages/stlite-kernel/src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,12 @@ export class StliteKernel {
}

public connectWebSocket(path: string): Promise<void> {
this._worker.postMessage({
return this._asyncPostMessage({
type: "websocket:connect",
data: {
path,
},
});

return Promise.resolve(); // TODO: Communicate the worker to confirm the connection
}

public sendWebSocketMessage(payload: Uint8Array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class ConnectionManager {
}

private async connect(): Promise<void> {
const WEBSOCKET_STREAM_PATH = "stream" // The original is defined in streamlit/frontend/src/lib/WebsocketConnection.tsx
const WEBSOCKET_STREAM_PATH = "/stream" // The original is defined in streamlit/frontend/src/lib/WebsocketConnection.tsx

try {
await this.props.kernel.connectWebSocket(WEBSOCKET_STREAM_PATH)
Expand Down
2 changes: 1 addition & 1 deletion packages/stlite-kernel/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type OutMessage =
| WebSocketBackMessage;

/**
* Reply message
* Reply message to InMessage
*/
interface ReplyMessageBase {
type: string;
Expand Down
70 changes: 42 additions & 28 deletions packages/stlite-kernel/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,35 +297,49 @@ self.onmessage = async (event: MessageEvent<InMessage>): Promise<void> => {
case "websocket:connect": {
console.debug("websocket:connect", data.data);

httpServer.start_websocket(
"/stream",
(messageProxy: any, binary: boolean) => {
// XXX: Now there is no session mechanism

if (binary) {
const buffer = messageProxy.getBuffer("u8");
messageProxy.destroy();
const payload = new Uint8ClampedArray(
buffer.data.buffer,
buffer.data.byteOffset,
buffer.data.byteLength
);
ctx.postMessage({
type: "websocket:message",
data: {
payload: new Uint8Array(payload),
},
});
} else {
ctx.postMessage({
type: "websocket:message",
data: {
payload: messageProxy,
},
});
const messagePort = event.ports[0];
const { path } = data.data;

try {
httpServer.start_websocket(
path,
(messageProxy: any, binary: boolean) => {
// XXX: Now there is no session mechanism

if (binary) {
const buffer = messageProxy.getBuffer("u8");
messageProxy.destroy();
const payload = new Uint8ClampedArray(
buffer.data.buffer,
buffer.data.byteOffset,
buffer.data.byteLength
);
ctx.postMessage({
type: "websocket:message",
data: {
payload: new Uint8Array(payload),
},
});
} else {
ctx.postMessage({
type: "websocket:message",
data: {
payload: messageProxy,
},
});
}
}
}
);
);

messagePort.postMessage({
type: "reply",
});
} catch (error) {
messagePort.postMessage({
type: "reply",
error,
});
}
break;
}
case "websocket:send": {
Expand Down