English 中文(简体)
如何在浏览器和服务器之间传输数据结构
原标题:How to transfer data structures between browser and server

在交互式网络应用程序方面,特别是那些利用网络套子等技术的图书馆,我们如何能够在客户浏览器和服务器之间传输数据结构(如地图、列表、数据集等)?迄今为止我遇到的例子只是传递字符串。

这是否取决于所利用的图书馆,或是否有更一般性的机制?

最佳回答

你可以发送三件东西在网络上(从客户角度):

  • Strings
  • ArrayBuffers
  • Blobs

如果您有一个复合的 Javascript 数据结构( 地图和阵列的等级), 那么您应该使用 JSON 将数据序列成字符串, 并将其发送到 WebSocket 连接中作为字符串 。

如果您对通过 WebSocket 连接发送二进制字节或文件数据感兴趣, 您仍然可以序列到字符串( 无效带宽), 或者您也可以以 ArrayBuffers 或 Blobs 格式发送数据 。

注 1: 当发送 ArrayBuffer 或 Blob 时, 您的服务器需要支持二进制框架, 在电线上出现二进制 WebSocket 框架 。

注意 2: 当服务器发送二进制框架时, 客户端可以选择返回对象类型。 这是 WebSocket 对象上的二进制类型属性, 可以设置为“ arraybuffer” 或“ blob ” 。

注 3: 如果浏览器只支持旧的 WebSocket Hexie 协议序列(例如iOS Safari), 那么就没有二进制数据, 您只能发送和接收字符串 。

问题回答

通常的方式是JSON或类似的。





相关问题
Can push technology / comet be faked?

Client has a dating site and would like to have a popup (either a nice javascript overlay or a new browser window popup. we re flexible.) displayed to users when another user is visiting their page. ...

Implement a Comet server in C#

I would like to know whether there is a way to write a comet server in C#. i have a C# code that generates data periodically, and I want to push these data to a java app. So would like to convert my C#...

Nonblocking webserver on .Net for Comet applications

I am trying to implement a Comet style (e.g. chat) application using IronPython. While I don t need to scale to twitter like dimensions, it is vital that the response time is lightening fast. All ...

How to enable active sockets in a Mochiweb application?

Does anyone know how to enable active instead of passive sockets in a Mochiweb application. Specifically, I am trying to adapt http://www.metabrew.com/article/a-million-user-comet-application-with-...

How to achieve Comet using Flash

How can Comet (aka Server Push) be used with Flash programs? What will be needed at server side for the same? Does this require any customised servers or will normal IIS or Apache do? Also is ...

热门标签