-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
038b14d
commit 021ccdd
Showing
6 changed files
with
175 additions
and
80 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
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,88 @@ | ||
let height; | ||
let width; | ||
let wsURL; | ||
let rotate; | ||
let draw; | ||
let latestX; | ||
let latestY; | ||
// Constants for the maximum values from the WebSocket messages | ||
const MAX_X_VALUE = 15725; | ||
const MAX_Y_VALUE = 20966; | ||
|
||
onmessage = (event) => { | ||
const data = event.data; | ||
|
||
switch (data.type) { | ||
case 'init': | ||
height = event.data.height; | ||
width = event.data.width; | ||
wsURL = event.data.wsURL; | ||
rotate = event.data.rotate; | ||
initiateEventsListener(); | ||
break; | ||
} | ||
}; | ||
|
||
|
||
async function initiateEventsListener() { | ||
const RETRY_DELAY_MS = 3000; // Delay before retrying the connection (in milliseconds) | ||
ws = new WebSocket(wsURL); | ||
draw = true; | ||
ws.onmessage = (event) => { | ||
const message = JSON.parse(event.data); | ||
if (message.Type === 3) { | ||
if (message.Code === 24) { | ||
draw = false; | ||
postMessage({ type: 'clear' }); | ||
// clearLaser(); | ||
} else if (message.Code === 25) { | ||
draw = true; | ||
|
||
} | ||
} | ||
if (message.Type === 3) { | ||
// Code 3: Update and draw laser pointer | ||
if (rotate) { | ||
if (message.Code === 1) { // Horizontal position | ||
latestX = scaleValue(message.Value, MAX_X_VALUE, width); | ||
} else if (message.Code === 0) { // Vertical position | ||
latestY = height - scaleValue(message.Value, MAX_Y_VALUE, height); | ||
} | ||
} else { | ||
if (message.Code === 1) { // Horizontal position | ||
latestX = width - scaleValue(message.Value, MAX_X_VALUE, width); | ||
} else if (message.Code === 0) { // Vertical position | ||
latestY = scaleValue(message.Value, MAX_Y_VALUE, height); | ||
} | ||
} | ||
if (draw) { | ||
postMessage({ type: 'update', X: latestX, Y: latestY }); | ||
//drawLaser(latestX, latestY); | ||
} | ||
} | ||
} | ||
|
||
ws.onerror = () => { | ||
postMessage({ | ||
type: 'error', | ||
message: error.message | ||
}); | ||
console.error('WebSocket error occurred. Attempting to reconnect...'); | ||
//setTimeout(connectWebSocket, 3000); // Reconnect after 3 seconds | ||
}; | ||
|
||
ws.onclose = () => { | ||
postMessage({ | ||
type: 'error', | ||
message: error.message | ||
}); | ||
console.log('WebSocket connection closed. Attempting to reconnect...'); | ||
//setTimeout(connectWebSocket, 3000); // Reconnect after 3 seconds | ||
}; | ||
} | ||
|
||
// Function to scale the incoming value to the canvas size | ||
function scaleValue(value, maxValue, canvasSize) { | ||
return (value / maxValue) * canvasSize; | ||
} | ||
|
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type gzipResponseWriter struct { | ||
io.Writer | ||
http.ResponseWriter | ||
} | ||
|
||
func (w gzipResponseWriter) Write(b []byte) (int, error) { | ||
return w.Writer.Write(b) | ||
} | ||
|
||
func gzMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
w.Header().Set("Content-Encoding", "gzip") | ||
gz, _ := gzip.NewWriterLevel(w, 1) | ||
defer gz.Close() | ||
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w} | ||
next.ServeHTTP(gzr, r) | ||
}) | ||
} |
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