wgram
v0.0.1
Published
## Overview
Readme
wgram
Overview
A browser-compatible implementation of Node's dgram UDP socket API that proxies over WebSocket. This allows web applications to communicate using UDP-like patterns through a WebSocket proxy.
Warning ⚠️
This works by running a WebSocket-to-UDP proxy server that translates WebSocket messages into UDP datagrams and vice versa. This is a BadIdea(tm), only use it in controlled environments or if you understand the risks.
Usage
install
npm install @playmint/wgramrun a wgram proxy
npx @playmint/wgramusage in browser
<script type="module">
import { createSocket } from '@playmint/wgram';
const socket = createSocket({
type: 'udp4',
websocketUrl: 'ws://localhost:8080' // URL of a wgram proxy (see below)
});
socket.bind(undefined, undefined, () => { // you don't get to pick the port or address
const { port, address } = socket.address();
console.log('Bound to', address, port);
});
socket.on('message', (msg, rinfo) => { // when someone sends a packet to the port/addr bound above
console.log('Message received:', new TextDecoder().decode(msg));
console.log('Remote info:', JSON.stringify(rinfo));
});
socket.send('Hello, UDP!', destPort, destAddress); // send a packet to a remote port/addr
</script>