This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
2019-08-01 00:44:38 +02:00
|
|
|
require "io/hexdump"
|
|
|
|
|
|
|
|
def to_message (user_type : Int, message : String)
|
2019-11-04 08:56:57 +01:00
|
|
|
payload = Bytes.new (6 + message.to_slice.size)
|
2019-08-01 00:44:38 +02:00
|
|
|
|
|
|
|
# true start
|
|
|
|
payload[0] = 1.to_u8
|
2019-11-04 08:56:57 +01:00
|
|
|
IO::ByteFormat::NetworkEndian.encode message.to_slice.size, (payload + 1)
|
2019-08-01 00:44:38 +02:00
|
|
|
|
|
|
|
# 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
|