-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Websocket communication between k6 Studio and browser (#382)
- Loading branch information
Showing
15 changed files
with
266 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,80 @@ | ||
import { MessageEnvelope, MessagePayload } from '@/services/browser/schemas' | ||
import { browser } from 'webextension-polyfill-ts' | ||
|
||
let socket: WebSocket | null = null | ||
let buffer: MessageEnvelope[] = [] | ||
|
||
function send(message: MessagePayload) { | ||
const envelope: MessageEnvelope = { | ||
messageId: crypto.randomUUID(), | ||
payload: message, | ||
} | ||
|
||
if (socket) { | ||
socket.send(JSON.stringify(envelope)) | ||
} else { | ||
buffer.push(envelope) | ||
} | ||
} | ||
|
||
function flush(socket: WebSocket) { | ||
for (const message of buffer) { | ||
socket.send(JSON.stringify(message)) | ||
} | ||
|
||
buffer = [] | ||
} | ||
|
||
function reconnect() { | ||
socket?.close() | ||
socket = null | ||
|
||
setTimeout(() => { | ||
connect() | ||
}, 1000) | ||
} | ||
|
||
function connect() { | ||
const ws = new WebSocket('ws://localhost:7554') | ||
|
||
ws.onopen = () => { | ||
console.log('Connected to server...') | ||
|
||
socket = ws | ||
|
||
flush(ws) | ||
} | ||
|
||
ws.onerror = (err) => { | ||
console.log('Connection error...', err) | ||
|
||
reconnect() | ||
} | ||
|
||
ws.onclose = () => { | ||
console.log('Connection closed...') | ||
|
||
reconnect() | ||
} | ||
} | ||
|
||
setInterval(() => { | ||
send({ | ||
type: 'events-captured', | ||
events: [ | ||
{ | ||
eventId: crypto.randomUUID(), | ||
timestamp: Date.now(), | ||
type: 'dummy', | ||
selector: 'button', | ||
message: 'Clicked button', | ||
}, | ||
], | ||
}) | ||
}, 5000) | ||
|
||
connect() | ||
|
||
browser.runtime.onInstalled.addListener(() => { | ||
console.log('Extension installed...') | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BrowserEvent } from '@/schemas/recording' | ||
import { useEffect, useState } from 'react' | ||
|
||
export function useListenBrowserEvent() { | ||
const [events, setEvents] = useState<BrowserEvent[]>([]) | ||
|
||
useEffect(() => { | ||
return window.studio.browser.onBrowserEvent((events: BrowserEvent[]) => { | ||
console.log('Received browser events', events) | ||
|
||
setEvents((prevEvents) => [...prevEvents, ...events]) | ||
}) | ||
}, []) | ||
|
||
return events | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './v1/browser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod' | ||
|
||
const DummyEvent = z.object({ | ||
eventId: z.string(), | ||
timestamp: z.number(), | ||
type: z.literal('dummy'), | ||
selector: z.string(), | ||
message: z.string(), | ||
}) | ||
|
||
export const BrowserEventSchema = DummyEvent | ||
|
||
export type DummyEvent = z.infer<typeof DummyEvent> | ||
export type BrowserEvent = z.infer<typeof BrowserEventSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { BrowserEventSchema } from '@/schemas/recording' | ||
import { z } from 'zod' | ||
|
||
export const EventsCapturedMessage = z.object({ | ||
type: z.literal('events-captured'), | ||
events: z.array(BrowserEventSchema), | ||
}) | ||
|
||
export const MessagePayload = EventsCapturedMessage | ||
|
||
export const MessageEnvelope = z.object({ | ||
messageId: z.string(), | ||
payload: MessagePayload, | ||
}) | ||
|
||
export type EventsCapturedMessage = z.infer<typeof EventsCapturedMessage> | ||
export type MessagePayload = z.infer<typeof MessagePayload> | ||
export type MessageEnvelope = z.infer<typeof MessageEnvelope> |
Oops, something went wrong.