Created
March 8, 2023 13:10
-
-
Save faveoled/a6e417b058403e0771dbc03ac9ad48ab to your computer and use it in GitHub Desktop.
Scala.js Node.js UDP client
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
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