ipcd/src/utils.cr

29 lines
694 B
Crystal

require "io/hexdump"
def to_message (user_type : Int, message : String)
payload = Bytes.new (6 + message.to_slice.size)
# true start
payload[0] = 1.to_u8
IO::ByteFormat::NetworkEndian.encode message.to_slice.size, (payload + 1)
# second part: user message
payload[5] = user_type.to_u8
(payload + 6).copy_from message.to_slice
return payload
end
def print_hexa(message : String, aroundmsg : String)
puts "#{aroundmsg} [["
m = IO::Memory.new(message)
io = IO::Hexdump.new(m, output: STDERR, read: true)
buffer = Bytes.new 4000
io.read (buffer) # reading = should print
puts "]] #{aroundmsg}"
end
# str = to_message(42, "coucou")
# print_hexa str, "message sent"
# ws.send str