initial support for osc (buggy)

This commit is contained in:
2023-11-22 12:12:36 +01:00
parent bbd7ccdeaa
commit fa67fdc2e5
8 changed files with 610 additions and 1 deletions

52
src/IO/OSC.ts Normal file
View File

@ -0,0 +1,52 @@
export let socket = new WebSocket('ws://localhost:3000')
export interface OSCMessage {
address: string
message: object
timetag: number
}
// @ts-ignore
socket.onopen = function(event) {
console.log("Connected to WebSocket Server")
// Send an OSC-like message
socket.send(JSON.stringify({
address: '/test',
args: [1, 2, 3]
}))
socket.onerror = function(error) {
console.log("Websocket Error:", error);
}
socket.onmessage = function(event) {
console.log("Received: ", event.data)
}
}
// export function sendToServer(message: OSCMessage) {
// socket.send(JSON.stringify(message));
// }
export function sendToServer(message: OSCMessage) {
// Check if the WebSocket is open
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify(message));
} else {
// Reconnect if the WebSocket is not open
console.log('WebSocket is not open. Attempting to reconnect...');
// Close the existing socket if necessary
if (socket.readyState === WebSocket.CONNECTING || socket.readyState === WebSocket.OPEN) {
socket.close();
}
// Create a new WebSocket connection
socket = new WebSocket('ws://localhost:3000');
// Send the message once the socket is open
socket.onopen = () => {
socket.send(JSON.stringify(message));
};
}
}