Class: SockJS::Protocol::Frame
- Inherits:
-
Object
- Object
- SockJS::Protocol::Frame
- Defined in:
- lib/sockjs/protocol.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#escape(string) ⇒ Object
JSON Unicode Encoding =====================.
- #validate(desired_class, object) ⇒ Object
Instance Method Details
#escape(string) ⇒ Object
JSON Unicode Encoding
SockJS takes the responsibility of encoding Unicode strings for the user. The idea is that SockJS should properly deliver any valid string from the browser to the server and back. This is actually quite hard, as browsers do some magical character translations. Additionally there are some valid characters from JavaScript point of view that are not valid Unicode, called surrogates (JavaScript uses UCS-2, which is not really Unicode).
Dealing with unicode surrogates (0xD800-0xDFFF) is quite special. If possible we should make sure that server does escape decode them. This makes sense for SockJS servers that support UCS-2 (SockJS-node), but can’t really work for servers supporting unicode properly (Python).
The server can’t send Unicode surrogates over Websockets, also various u2xxxx chars get mangled. Additionally, if the server is capable of handling UCS-2 (ie: 16 bit character size), it should be able to deal with Unicode surrogates 0xD800-0xDFFF: en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Surrogates
32 33 34 35 36 |
# File 'lib/sockjs/protocol.rb', line 32 def escape(string) string.gsub(CHARS_TO_BE_ESCAPED) do |match| '\u%04x' % (match.ord) end end |
#validate(desired_class, object) ⇒ Object
38 39 40 41 42 |
# File 'lib/sockjs/protocol.rb', line 38 def validate(desired_class, object) unless object.is_a?(desired_class) raise TypeError.new("#{desired_class} object expected, but object is an instance of #{object.class} (object: #{object.inspect}).") end end |