Compare commits

...

2 Commits

Author SHA1 Message Date
Philippe Pittoli 4ba5aa93f3 Add a simple example. 2023-05-27 22:11:56 +02:00
Philippe Pittoli 556bdb58dd More options for websocat. 2023-05-27 22:11:20 +02:00
2 changed files with 57 additions and 2 deletions

View File

@ -1,8 +1,12 @@
WS_SERVICE ?= 127.0.0.1:8080
WS_PORT ?= 8080
WS_SERVICE ?= 127.0.0.1:$(WS_PORT)
WS_TARGET_SERVICE ?= pong
WS_TARGET ?= unix:/tmp/.libipc-run/$(WS_TARGET_SERVICE)
init-websocket-tcpd:
@# '-b' binary, '-E' quit on end-of-file, 'ws-l' websocket URI to listen
@# each connection is redirected to last parameter
websocat -b -E ws-l:$(WS_SERVICE) unix:/tmp/.libipc-run/pong
websocat -b -E ws-l:$(WS_SERVICE) $(WS_TARGET)
init-websocket-client:
@# websocat -b -E tcp-l:127.0.0.1:9000 ws://127.0.0.1:9999

View File

@ -0,0 +1,51 @@
# WARNING: This code is for DEBUG only.
# Listen to a UNIX socket, connect to another one.
require "ipc"
ipc = IPC.new
ipc.timer 5000 # timer event every 5 seconds
fd = ipc.connect "pong" # 'pong' service
ipc.loop do |event|
case event.type
when LibIPC::EventType::MessageRx
m = event.message
if m.nil?
puts "No message"
else
received = String.new(m.to_unsafe, m.size)
pp! received
ipc.schedule event.fd, m, m.size
end
when LibIPC::EventType::MessageTx
puts "A message has been sent"
when LibIPC::EventType::Connection
puts "A client just connected #JOY"
when LibIPC::EventType::Disconnection
puts "A client just disconnected #SAD"
when LibIPC::EventType::Timer
STDOUT.write "\rTimer!".to_slice
when LibIPC::EventType::External
else
puts "Unexpected: #{event.type}"
exit 1
end
end
# Read an IPC network packet and remove the first 4 bytes.
buffer = Bytes.new 1_000_000
while true
len = STDIN.read buffer
break if len == 0
STDOUT.write buffer[4.. len -1]
end
# Input to IPC network packets.
buffer = Bytes.new 1_000_000
while true
len = STDIN.read buffer
break if len == 0
STDOUT.write_bytes len, IO::ByteFormat::BigEndian
STDOUT.write buffer[0.. len -1]
end