nim-webrtc/examples/pong.nim

30 lines
712 B
Nim
Raw Normal View History

2023-10-13 11:58:30 +00:00
import chronos, stew/byteutils
import ../webrtc/udp_connection
import ../webrtc/stun/stun_connection
import ../webrtc/dtls/dtls
import ../webrtc/sctp
proc sendPong(conn: SctpConn) {.async.} =
var i = 0
while true:
let msg = await conn.read()
echo "Received: ", string.fromBytes(msg.data)
await conn.write(("pong " & $i).toBytes)
i.inc()
proc main() {.async.} =
let laddr = initTAddress("127.0.0.1:4242")
let udp = UdpConn()
2023-10-13 13:04:02 +00:00
udp.init(laddr)
2023-10-13 11:58:30 +00:00
let stun = StunConn()
2023-10-13 13:04:02 +00:00
stun.init(udp, laddr)
2023-10-13 11:58:30 +00:00
let dtls = Dtls()
dtls.start(stun, laddr)
let sctp = Sctp.new(dtls, laddr)
2023-10-13 13:04:02 +00:00
sctp.listen(13)
2023-10-13 11:58:30 +00:00
while true:
let conn = await sctp.accept()
asyncSpawn conn.sendPong()
waitFor(main())