Skip to content

Instantly share code, notes, and snippets.

@gchiu
Last active February 19, 2017 20:02
Show Gist options
  • Select an option

  • Save gchiu/588d21599eb4fc09829d16f0945b73da to your computer and use it in GitHub Desktop.

Select an option

Save gchiu/588d21599eb4fc09829d16f0945b73da to your computer and use it in GitHub Desktop.
UDP and HTTP server in Ren-c
Rebol [
file: %a1.reb
notes: {ssdp with http server in ren-c
httpd.reb is here https://gist.github.com/rgchris/73510e7d643eb0a6b9fa69b849cd9880
}
]
serve-udp: open udp://:1900
serve-udp/awake: func [evt][
print ["multicast event:" evt/type " on " evt/port/spec/port-id " at " now/precise]
switch/default evt/type [
read [
print ["read server data:" newline to-string evt/port/data]
; process UDP notifications here
clear evt/port/data
; keep reading the port and not exit the awake function
read evt/port
; return true
]
error [
; do we need to exit on error?
return false
]
][ return false]
]
set-udp-multicast serve-udp 239.255.255.250 0.0.0.0
set-udp-ttl serve-udp 1
do %httpd.reb
serve-http: open [
Scheme: 'httpd
Port-ID: 8001
Awake: func [
request [object!]
][
print ["http request at " now/precise]
; process all http events here, eg.
make object! compose [
Status: 200
Type: "text/html"
Content: reword "<h1>OK! $action :: $target</h1>" compose [
action (request/action)
target (request/target)
]
]
]
]
wait-ports: copy []
append wait-ports serve-http
append wait-ports serve-udp
attempt [browse http://127.0.0.1:8001/try/this/path]
print "Listening ..."
read serve-udp ; seems to be necessary
forever [
print ["start wait at " now/precise]
wait wait-ports
]
@gchiu
Copy link
Copy Markdown
Author

gchiu commented Feb 19, 2017

By not exiting the serve-udp awake function, we can take the read serve-udp out of the forever loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment