Skip to content

Commit

Permalink
Remove hardcoded websocket port
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Sep 23, 2023
1 parent 8038004 commit ff9f75a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/content.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPorts, requestPort } from "./messaging"
import { getNativeParams, getPorts, requestPort } from "./messaging"

// @ts-ignore
window.wrappedJSObject.navigator.serial = cloneInto({}, window.navigator, {})
Expand Down Expand Up @@ -26,6 +26,9 @@ function wrapPromise<T>(promise: Promise<T>): Promise<T> {
}

window.WebSerialPolyfill = {
getNativeParams: () => {
return wrapPromise(getNativeParams())
},
getPorts: () => {
return wrapPromise(getPorts(window.location.origin))
},
Expand Down
6 changes: 5 additions & 1 deletion src/serial/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ export class SerialWebSocket extends EventTarget implements SerialTransport {

if (this.connected) await this.disconnect()

const params = await WebSerialPolyfill.getNativeParams()
if (params.state !== "connected")
throw Error("Native application not connected")

await new Promise((resolve, reject) => {
this.ws_ = new WebSocket("ws:https://localhost:23290")
this.ws_ = new WebSocket(`ws:https://localhost:${params.wsPort}`)
this.ws_.binaryType = "arraybuffer"
this.ws_.onmessage = this.receive.bind(this)
this.ws_.onopen = resolve
Expand Down
2 changes: 2 additions & 0 deletions webserial.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { SerialPortData } from "./src/serial/types";
import { NativeParams } from "./src/utils/types";

export { }

declare global {
interface WebSerialPolyfill {
getNativeParams: () => Promise<NativeParams>
getPorts: () => Promise<SerialPortData[]>
requestPort: (options?: SerialPortRequestOptions) => Promise<SerialPortData>
}
Expand Down

0 comments on commit ff9f75a

Please sign in to comment.