By following example from em-websocket
gem, I've just create simple echo-server that running with Sinatra framework. The problem is I don't understand how to use some format to send message instead of plain text. For now, the code looks like:
EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080, debug: true) do |ws|
ws.onmessage { |msg|
ws.send msg
}
...
}
I would like to send message in some format, like a Hash:
ws.onmessage { |msg|
hash_message = {}
hash_message[:time] = Time.now.strftime("%T")
hash_message[:text] = msg
ws.send hash_message
}
And on client side, use it for building nice message box with time and text divs (time is just example in this case). e.g, use
ws.onmessage = (evt) ->
write_message(evt.data.time,
evt.data.text)
instead of
ws.onmessage = (evt) ->
write_message evt.data
It was fairly simple. Generate json string on client side:
parse this string on the server, and send it back to client (with added time):
and display this message on client when it get it