javascript-WebSockets和文本编码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-WebSockets和文本编码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2269字,纯文字阅读大概需要4分钟。
内容图文
![javascript-WebSockets和文本编码](/upload/InfoBanner/zyjiaocheng/685/df1bea30d8664ef3b5c7324efb4ca641.jpg)
我读:
The WebSocket API accepts a DOMString object, which is encoded as
UTF-8 on the wire, or one of ArrayBuffer, ArrayBufferView, or Blob
objects for binary transfers.
DOMString是UTF-16编码的字符串.那么在网上使用UTF-8编码是否正确?
解决方法:
是的,这是正确的.
UTF-16可能会或可能不会在内存中使用,这只是您所使用的任何框架的实现细节.对于JavaScript,字符串为UTF-16.
对于WebSocket通信,必须在线使用UTF-8来存储文本数据(当今大多数Internet协议都使用UTF-8).这是由WebSocket protocol specification决定的:
After a successful handshake, clients and servers transfer data back and forth in conceptual units referred to in this specification as “messages”. On the wire, a message is composed of one or more frames. The WebSocket message does not necessarily correspond to a particular network layer framing, as a fragmented message may be coalesced or split by an intermediary.
A frame has an associated type. Each frame belonging to the same message contains the same type of data. Broadly speaking, there are types for textual data (which is interpreted as UTF-8 [RFC3629] text), binary data (whose interpretation is left up to the application), and control frames (which are not intended to carry data for the application but instead for protocol-level signaling, such as to signal that the connection should be closed). This version of the protocol defines six frame types and leaves ten reserved for future use.
…
Data frames (e.g., non-control frames) are identified by opcodes where the most significant bit of the opcode is 0. Currently defined opcodes for data frames include 0x1 (Text), 0x2 (Binary). Opcodes 0x3-0x7 are reserved for further non-control frames yet to be defined.
Data frames carry application-layer and/or extension-layer data. The opcode determines the interpretation of the data:
Text
The “Payload data” is text data encoded as UTF-8. Note that a particular text frame might include a partial UTF-8 sequence; however, the whole message MUST contain valid UTF-8. Invalid UTF-8 in reassembled messages is handled as described in Section 8.1.
Binary
The “Payload data” is arbitrary binary data whose interpretation is solely up to the application layer.
您将确保从UTF-16到UTF-8到UTF-16的少量开销,但是在现代计算机上开销很小,并且UTF之间的转换是无损的.
内容总结
以上是互联网集市为您收集整理的javascript-WebSockets和文本编码全部内容,希望文章能够帮你解决javascript-WebSockets和文本编码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。