Skip to content

Instantly share code, notes, and snippets.

@faveoled
Created March 8, 2023 13:10
Show Gist options
  • Save faveoled/a6e417b058403e0771dbc03ac9ad48ab to your computer and use it in GitHub Desktop.
Save faveoled/a6e417b058403e0771dbc03ac9ad48ab to your computer and use it in GitHub Desktop.
Scala.js Node.js UDP client
import scala.scalajs.js
import typings.node.dgramMod as dgram
import typings.node.dgramMod.SocketType
import typings.node.nodeStrings.message
import typings.node.global.console
import typings.std.stdStrings.add
object Dgram {
def send(msg: String, host: String, port: Int): Unit = {
val client = dgram.createSocket(SocketType.udp4)
client.on("listening", (arg) => {
val address = client.address()
console.log(s"UDP Server listening on ${address.address}:${address.port}}")
})
client.on_message(message, (msgBuf, remote) => {
console.log(s"Incoming on ${remote.address}:${remote.port} - ${msgBuf.toString()}")
})
client.send(msg, 0, msg.length(), port, host, (err, bytes) => {
if (err != null) {
console.log(s"Error sending: ${err}")
}
console.log(s"Sent ${bytes} UDP message bytes to ${host}:${port}")
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment