send mouse drag over wrtc (#139)

* send mouse drag over wrtc

* add more debugging code

* wrtc data channel working
This commit is contained in:
Kurt Hutten
2023-06-23 09:56:37 +10:00
committed by GitHub
parent 2d3c73d46a
commit 7289965916
3 changed files with 42 additions and 2 deletions

View File

@ -138,6 +138,12 @@ export function App() {
}) })
} }
const engineCommandManager = useMemo(() => new EngineCommandManager(setMediaStream), []) const engineCommandManager = useMemo(() => new EngineCommandManager(setMediaStream), [])
useEffect(() => {
return () => {
engineCommandManager.tearDown()
}
}, [])
useEffect(() => { useEffect(() => {
const asyncWrap = async () => { const asyncWrap = async () => {
try { try {

View File

@ -27,7 +27,7 @@ export const Stream = () => {
const debounceSocketSend = throttle((message) => { const debounceSocketSend = throttle((message) => {
engineCommandManager?.sendSceneCommand(message) engineCommandManager?.sendSceneCommand(message)
}, 100) }, 16)
const handleMouseMove: MouseEventHandler<HTMLVideoElement> = ({ const handleMouseMove: MouseEventHandler<HTMLVideoElement> = ({
clientX, clientX,
clientY, clientY,

View File

@ -59,7 +59,7 @@ interface EngineCommand {
} }
} }
} }
ClosePath?: { ClosePath?: {
path_id: uuid path_id: uuid
} }
Extrude?: { Extrude?: {
@ -80,6 +80,7 @@ export class EngineCommandManager {
sourceRangeMap: SourceRangeMap = {} sourceRangeMap: SourceRangeMap = {}
socket?: WebSocket socket?: WebSocket
pc?: RTCPeerConnection pc?: RTCPeerConnection
lossyDataChannel?: RTCDataChannel
onHoverCallback: (id?: string) => void = () => {} onHoverCallback: (id?: string) => void = () => {}
onClickCallback: (selection: SelectionsArgs) => void = () => {} onClickCallback: (selection: SelectionsArgs) => void = () => {}
onCursorsSelectedCallback: (selections: CursorSelectionsArgs) => void = onCursorsSelectedCallback: (selections: CursorSelectionsArgs) => void =
@ -88,6 +89,9 @@ export class EngineCommandManager {
const url = 'wss://api.dev.kittycad.io/ws/modeling/commands' const url = 'wss://api.dev.kittycad.io/ws/modeling/commands'
this.socket = new WebSocket(url) this.socket = new WebSocket(url)
this.pc = new RTCPeerConnection() this.pc = new RTCPeerConnection()
this.pc.createDataChannel('unreliable_modeling_cmds', {
ordered: true,
})
this.socket.addEventListener('open', (event) => { this.socket.addEventListener('open', (event) => {
console.log('Connected to websocket, waiting for ICE servers') console.log('Connected to websocket, waiting for ICE servers')
}) })
@ -166,6 +170,23 @@ export class EngineCommandManager {
this.socket?.send(msg) this.socket?.send(msg)
}) })
.catch(console.log) .catch(console.log)
this.pc.addEventListener('datachannel', (event) => {
this.lossyDataChannel = event.channel
console.log('accepted lossy data channel', event.channel.label)
this.lossyDataChannel.addEventListener('open', (event) => {
console.log('lossy data channel opened', event)
})
this.lossyDataChannel.addEventListener('close', (event) => {
console.log('lossy data channel closed')
})
this.lossyDataChannel.addEventListener('error', (event) => {
console.log('lossy data channel error')
})
this.lossyDataChannel.addEventListener('message', (event) => {
console.log('lossy data channel message: ', event)
})
})
} }
// TODO talk to the gang about this // TODO talk to the gang about this
// the following message types are made up // the following message types are made up
@ -180,6 +201,13 @@ export class EngineCommandManager {
} }
}) })
} }
tearDown() {
// close all channels, sockets and WebRTC connections
console.log('tearing it all down')
this.lossyDataChannel?.close()
this.socket?.close()
this.pc?.close()
}
startNewSession() { startNewSession() {
this.artifactMap = {} this.artifactMap = {}
@ -263,6 +291,12 @@ export class EngineCommandManager {
console.log('socket not ready') console.log('socket not ready')
return return
} }
if ('CameraDragMove' in command.cmd && this.lossyDataChannel) {
console.log('sending lossy command', command, this.lossyDataChannel)
this.lossyDataChannel.send(JSON.stringify(command))
return
}
console.log('sending through TCP')
this.socket?.send(JSON.stringify(command)) this.socket?.send(JSON.stringify(command))
} }
sendModellingCommand({ sendModellingCommand({