32 lines
709 B
Crystal
32 lines
709 B
Crystal
|
require "io/hexdump"
|
||
|
|
||
|
def to_message (user_type : Int, message : String)
|
||
|
payload = Bytes.new (6 + message.size)
|
||
|
|
||
|
# true start
|
||
|
payload[0] = 1.to_u8
|
||
|
payload[1] = 0.to_u8
|
||
|
payload[2] = 0.to_u8
|
||
|
payload[3] = 0.to_u8
|
||
|
payload[4] = message.size.to_u8
|
||
|
|
||
|
# 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
|