-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Adding DDP over WS, moving duplicate WS-connection to common.js #4997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4090af3
DDP over WS, adding WS connection to common.js
DedeHai 7cf935c
add better safety check for DDP, bugfix, reformating (tabs)
DedeHai 492d6b2
fix rabbit suggestions
DedeHai f8727a8
use getLoc() instead of redefining its code.
DedeHai 3cc4c8b
added protocol byte, fixed bugs
DedeHai 900babd
typo
DedeHai 5e128cb
add safety check and pass ws to parent
DedeHai 4183e60
typo
DedeHai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -116,3 +116,60 @@ function uploadFile(fileObj, name) { | |
| fileObj.value = ''; | ||
| return false; | ||
| } | ||
| // connect to WebSocket, use parent WS or open new | ||
| function connectWs(onOpen) { | ||
| try { | ||
| if (top.window.ws && top.window.ws.readyState === WebSocket.OPEN) { | ||
| if (onOpen) onOpen(); | ||
| return top.window.ws; | ||
| } | ||
| } catch (e) {} | ||
|
|
||
| getLoc(); // ensure globals (loc, locip, locproto) are up to date | ||
| let url = loc ? getURL('/ws').replace("http","ws") : "ws://"+window.location.hostname+"/ws"; | ||
| let ws = new WebSocket(url); | ||
| ws.binaryType = "arraybuffer"; | ||
| if (onOpen) { ws.onopen = onOpen; } | ||
| return ws; | ||
| } | ||
|
|
||
| // send led colors to ESP using WebSocket and DDP protocol (RGB) | ||
|
||
| // ws: WebSocket object | ||
| // start: start pixel index | ||
| // len: number of pixels to send | ||
| // colors: Uint8Array with RGB values (3*len bytes) | ||
| function sendDDP(ws, start, len, colors) { | ||
| let maxDDPpx = 472; // must fit into one WebSocket frame of 1428 bytes, DDP header is 10+1 bytes -> 472 RGB pixels | ||
| //let maxDDPpx = 172; // ESP8266: must fit into one WebSocket frame of 528 bytes -> 172 RGB pixels TODO: add support for ESP8266? | ||
| if (!ws || ws.readyState !== WebSocket.OPEN) return false; | ||
| // send in chunks of maxDDPpx | ||
| for (let i = 0; i < len; i += maxDDPpx) { | ||
| let cnt = Math.min(maxDDPpx, len - i); | ||
| let off = (start + i) * 3; // DDP pixel offset in bytes | ||
| let dLen = cnt * 3; | ||
| let cOff = i * 3; // offset in color buffer | ||
| let pkt = new Uint8Array(11 + dLen); // DDP header is 10 bytes, plus 1 byte for WLED websocket protocol indicator | ||
| pkt[0] = 0x02; // DDP protocol indicator for WLED websocket. Note: below DDP protocol bytes are offset by 1 | ||
| pkt[1] = 0x40; // flags: 0x40 = no push, 0x41 = push (i.e. render), note: this is DDP protocol byte 0 | ||
| pkt[2] = 0x00; // reserved | ||
| pkt[3] = 0x01; // 1 = RGB (currently only supported mode) | ||
| pkt[4] = 0x01; // destination id (not used but 0x01 is default output) | ||
| pkt[5] = (off >> 24) & 255; // DDP protocl 4-7 is offset | ||
| pkt[6] = (off >> 16) & 255; | ||
| pkt[7] = (off >> 8) & 255; | ||
| pkt[8] = off & 255; | ||
| pkt[9] = (dLen >> 8) & 255; // DDP protocol 8-9 is data length | ||
| pkt[10] = dLen & 255; | ||
| pkt.set(colors.subarray(cOff, cOff + dLen), 11); | ||
| if(i + cnt >= len) { | ||
| pkt[1] = 0x41; //if this is last packet, set the "push" flag to render the frame | ||
| } | ||
| try { | ||
| ws.send(pkt.buffer); | ||
| } catch (e) { | ||
| console.error(e); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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
netmindz marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
softhack007 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.