switch to udp

This commit is contained in:
Daniel Bulant 2026-05-04 12:51:58 +02:00
parent 7c51ee47b5
commit 3977f101ad
No known key found for this signature in database
2 changed files with 54 additions and 57 deletions

View file

@ -2,41 +2,45 @@ import type {
PartySocketEvent, PartySocketEvent,
PartyState, PartyState,
} from "../api/src/party-types"; } from "../api/src/party-types";
let ws: WebSocket | null = null; let ws: WebSocket | null = new WebSocket("ws://localhost:3000/api/dev-socket/ws");
Bun.listen({ const socket = await Bun.udpSocket({
hostname: "0.0.0.0",
port: 7070, port: 7070,
socket: { socket: {
data(socket, data) { data(socket, buf, port, addr) {
// in1={} in2={} in3={} in4={} angle={} console.log(`message from ${addr}:${port}:`);
console.log("recv", data) console.log(buf.toString());
}, // message received from client },
open(socket) {
console.log("Connected");
ws = new WebSocket("ws://localhost:3000/api/dev-socket/ws");
ws.onmessage = e => {
const data = JSON.parse(e.data) as PartySocketEvent;
switch (data.type) {
case "party_status":
const { party: { data: { currentQuestion } } } = data;
console.log(currentQuestion)
let text = currentQuestion?.text
if (text) {
ws?.send(text)
}
break;
}
}
}, // socket opened
close(socket, error) {
ws?.close();
ws = null;
}, // socket closed
drain(socket) {}, // socket ready for more data
error(socket, error) {}, // error handler
}, },
}); });
ws.onerror = e => {
console.error(e)
}
ws.onopen = () => {
console.log("WebSocket open")
}
ws.onmessage = e => {
const data = JSON.parse(e.data) as PartySocketEvent;
console.log(data)
switch (data.type) {
case "party_status":
const { party } = data;
if (!party) return;
const partyData = party.data;
if (!partyData) return;
const { currentQuestion } = partyData
console.log(currentQuestion)
let text = currentQuestion?.text
if (text) {
ws?.send(text)
}
break;
}
}
console.log("Started on :7070") console.log("Started on :7070")

View file

@ -3,9 +3,11 @@
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use core::convert::Infallible; use core::convert::Infallible;
use core::net::SocketAddrV4;
use core::ops::Deref; use core::ops::Deref;
use core::str::FromStr; use core::str::FromStr;
use embassy_net::tcp::ConnectError; use embassy_net::tcp::ConnectError;
use embassy_net::udp::{PacketMetadata, UdpMetadata, UdpSocket};
use embedded_io::ReadReady; use embedded_io::ReadReady;
use ag_lcd::{Blink, Cursor, Display, LcdDisplay}; use ag_lcd::{Blink, Cursor, Display, LcdDisplay};
@ -166,31 +168,22 @@ async fn main(spawner: Spawner) {
let in4 = Input::new(p.PIN_21, embassy_rp::gpio::Pull::Up); let in4 = Input::new(p.PIN_21, embassy_rp::gpio::Pull::Up);
let mut rx_buffer = [0; 4096]; let mut rx_buffer = [0; 4096];
let mut rx_meta = [PacketMetadata::EMPTY; 16];
let mut tx_buffer = [0; 4096]; let mut tx_buffer = [0; 4096];
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); let mut tx_meta = [PacketMetadata::EMPTY; 16];
socket.set_timeout(Some(Duration::from_secs(10)));
let mut socket = UdpSocket::new(
stack,
&mut rx_meta,
&mut rx_buffer,
&mut tx_meta,
&mut tx_buffer,
);
socket.bind(7070).unwrap();
led.set_low();
info!("Connecting...");
uwrite!(lcd, "con2.");
let host_addr = embassy_net::Ipv4Address::from_str("192.168.12.1").unwrap(); let host_addr = embassy_net::Ipv4Address::from_str("192.168.12.1").unwrap();
if let Err(e) = socket.connect((host_addr, 7070)).await { let target = UdpMetadata::from(SocketAddrV4::new(host_addr, 7070));
lcd.clear(); uwrite!(lcd, "udp.");
uwrite!(lcd, "conerr");
Timer::after(Duration::from_micros(100)).await;
lcd.set_position(0, 1);
let emsg = match e {
ConnectError::ConnectionReset => "rst",
ConnectError::InvalidState => "inv",
ConnectError::TimedOut => "tout",
ConnectError::NoRoute => "nroute",
};
uwrite!(lcd, "{}", emsg);
warn!("connect error: {:?}", e);
} else {
uwrite!(lcd, "conok");
}
info!("Connected to {:?}", socket.remote_endpoint());
let delay = Duration::from_millis(100); let delay = Duration::from_millis(100);
let mut buffer = WriteBuffer::<1024>::new(); let mut buffer = WriteBuffer::<1024>::new();
@ -213,12 +206,12 @@ async fn main(spawner: Spawner) {
); );
} }
{ {
use embedded_io_async::Write; let _ = socket.send_to(&*buffer, target).await;
let _ = socket.write_all(&*buffer).await; buffer.0.clear();
} }
if socket.read_ready().unwrap_or(false) { if socket.may_recv() {
let mut rx_buffer = [0; 4096]; let mut rx_buffer = [0; 4096];
let n = socket.read(&mut rx_buffer).await.unwrap_or(0); let (n, ep) = socket.recv_from(&mut rx_buffer).await.unwrap();
if n > 0 { if n > 0 {
lcd.clear(); lcd.clear();
lcd.home(); lcd.home();