Skip to content

Instantly share code, notes, and snippets.

@rgchris rgchris/ping.reb
Last active Jan 5, 2017

Embed
What would you like to do?
Port example: Send block to Server, Responds with reversed block.
#!/usr/local/bin/ren-c
Rebol [
Title: "Ping Scheme"
Date: 5-Jan-2017
Author: "Christopher Ross-Gill"
Home: https://github.com/revault/rebol-wiki/wiki/Port-Examples#tcp-ping-pong-messages
]
sys/make-scheme [
Title: "Ping Client"
Name: 'ping
Actor: [
Write: func [port data /local subport][
wait open subport: make port! [
scheme: 'tcp
host: port/spec/host
port-id: port/spec/port-id
awake: func [event][
switch event/type [
lookup [
open event/port
false
]
connect [
write event/port to binary! mold data
false
]
wrote [
read event/port
false
]
done [event/port/data: read event/port true]
]
]
]
wait 0.01 ; surely this shouldn't be needed
any [
attempt [load subport/data]
[fail]
]
]
]
]
probe write ping://127.0.0.1:8000 [foo bar]
#!/usr/local/bin/ren-c
Rebol [
Title: "Pong Scheme"
Date: 5-Jan-2017
Author: "Christopher Ross-Gill"
Home: https://github.com/revault/rebol-wiki/wiki/Port-Examples#tcp-ping-pong-messages
]
sys/make-scheme [
Title: "Pong Server"
Name: 'pong
Actor: [
open: func [port][
print ["Opening Server:" port/locals: rejoin [tcp://: port/spec/port-id]]
port/locals: open port/locals
port/locals/awake: func [event /local client][
if event/type = 'accept [
client: first event/port
client/awake: func [event /local message] [
;probe event/type
switch event/type [
read [
message: probe attempt [load event/port/data]
clear event/port/data
write event/port to binary! mold probe either message [
compose/only [ok (reverse compose [(message)])]
][
[fail]
]
return false
]
wrote [
print "Server sent pong to client"
close event/port
return true
]
close [
close event/port
return true
]
]
false
]
read client
]
false
]
port
]
close: func [port][
close port/locals
]
]
]
wait open pong://:8000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.