Skip to content

Commit

Permalink
udp error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
foomoon committed Dec 14, 2023
1 parent eee5dab commit dc53f5d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 1 addition & 3 deletions dist/lib/light.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// <reference types="node" />
/// <reference types="node" />
import { AxiosInstance, AxiosResponse } from "axios";
import * as udp from "node:dgram";
import { Frame } from "./frame.js";
import { Movie } from "./movie.js";
import { rgbColor, hsvColor, deviceMode, timer, coordinate, layout } from "./interfaces.js";
Expand All @@ -17,7 +15,7 @@ export declare class Light {
token: AuthenticationToken | undefined;
activeLoginCall: boolean;
nleds: number | undefined;
udpClient: udp.Socket;
udpClient: any;
/**
* Creates an instance of Light.
*
Expand Down
4 changes: 3 additions & 1 deletion dist/lib/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Light {
this.udpClient = udp.createSocket("udp4");
}
else {
this.udpClient = new udp.Socket();
this.udpClient = null;
}
}
autoEndLoginCall() {
Expand Down Expand Up @@ -488,6 +488,8 @@ export class Light {
return __awaiter(this, void 0, void 0, function* () {
if (!this.token)
throw errNoToken;
if (!this.udpClient)
throw new Error("UDP not supported in browser");
// Generate the header
let tokenArray = this.token.getTokenDecoded();
let udpHeader = Buffer.alloc(tokenArray.length + 4);
Expand Down
8 changes: 5 additions & 3 deletions src/lib/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class Light {
token: AuthenticationToken | undefined;
activeLoginCall: boolean;
nleds: number | undefined;
udpClient: udp.Socket;
udpClient: any; //udp.Socket;
/**
* Creates an instance of Light.
*
Expand All @@ -80,7 +80,7 @@ export class Light {
if (typeof window === "undefined") {
this.udpClient = udp.createSocket("udp4");
} else {
this.udpClient = new udp.Socket();
this.udpClient = null;
}
}
async autoEndLoginCall(): Promise<void> {
Expand Down Expand Up @@ -476,6 +476,8 @@ export class Light {
async sendRealTimeFrameUDP(frame: Frame) {
if (!this.token) throw errNoToken;

if (!this.udpClient) throw new Error("UDP not supported in browser");

// Generate the header
let tokenArray = this.token.getTokenDecoded();
let udpHeader = Buffer.alloc(tokenArray.length + 4);
Expand All @@ -490,7 +492,7 @@ export class Light {
const data = Buffer.alloc(udpHeader.length + frame.getNLeds() * 3);
data.fill(udpHeader);
data.fill(frame.toOctet(), udpHeader.length);
this.udpClient.send(data, 7777, this.ipaddr, (error) => {
this.udpClient.send(data, 7777, this.ipaddr, (error: any) => {
if (error) {
console.warn(error);
}
Expand Down

0 comments on commit dc53f5d

Please sign in to comment.