简体   繁体   English

为什么Web套接字不使用SOAP?

[英]Why doesn’t Web Sockets use SOAP?

First off, I intend no hostility nor neglegence, just want to know people's thoughts. 首先,我打算没有敌意也不打算,只想知道别人的想法。 I am looking into bi-directional communication between client and server; 我正在研究客户端和服务器之间的双向通信; client being a web application. 客户端是一个Web应用程序。 At this point I have a few options: MS-proprietary duplex binding, from what I hear unreliable and unnatural: comet, and web sockets (for supported browsers). 在这一点上,我有几个选择:MS专有的双工绑定,从我听到的不可靠和不自然:彗星和网络套接字(对于支持的浏览器)。

I know this question has been asked in other ways here, but I have a more specific question to the approach. 我知道这个问题在其他方面已经被问过,但我对这个方法有一个更具体的问题。 Considering web sockets are client-side, the client code sits in JavaScript. 考虑到Web套接字是客户端的,客户端代码位于JavaScript中。 Is it really the intention to build a large chunk of an application directly in JavaScript? 是否真的打算直接在JavaScript中构建一大块应用程序? Why didn't W3C do this in web services? 为什么W3C在Web服务中没有这样做? Wouldn't it be easier if we were to be able to use SOAP to provide a contract and define events along with the existing messaging involved? 如果我们能够使用SOAP提供合同并定义事件以及涉及的现有消息传递,那会不会更容易? Just feels like the short end of the stick so far. 到目前为止,感觉就像棒子的短端。

Why not make it simple and take advantage of JS dynamic nature and leave the bulk of code where it belongs....on the server? 为什么不简单地利用JS动态特性并将大量代码留在它所属的位置....在服务器上?

Instead of 代替

mysocket.send("AFunction|withparameters|segmented");

we could say 我们可以说

myServerObject.AFunction("that", "makessense");

and instead of 而不是

...
mysocket.onmessage = function() { alert("yay! an ambiguous message"); }
...

we could say 我们可以说

...
myServerObject.MeaningfulEvent = function(realData) { alert("Since I have realistic data....");  alert("Hello " + realData.FullName); }
...

HTML 5 took forever to take hold....did we waste a large amount of effort in the wrong direction? HTML 5永远占据了......我们是否在错误的方向上浪费了大量的精力? Thoughts? 思考?

Sounds to me like you've not yet fully grasped the concepts around Websockets. 听起来像你还没有完全掌握Websockets的概念。 For example you say: 例如你说:

Considering web sockets are client-side 考虑Web套接字是客户端的

This is not the case, sockets have 2 sides, you could think of these as a Server and a Client, however once the connection is established the distinction blurs - you could then also think of the client and the server as "peers" - each can write or read in to the pipe that connects them (the socket connection) at any time. 事实并非如此,套接字有两面,您可以将它们视为服务器和客户端,但是一旦建立连接,区别就会模糊 - 您可以将客户端和服务器视为“对等” - 每个可以随时写入或读入连接它们的管道(套接字连接)。 I suspect you'd benefit from learning a little more about HTTP works on top of TCP - WebSockets is similar / analogous to HTTP in this way. 我怀疑你会从在TCP上学习更多关于HTTP的工作中受益 - WebSockets就像这样与HTTP类似/类似。

Regarding SOAP / WSDL, from the point of view of a conversation surrounding TCP / WebSocket / HTTP you can think of all SOAP / WSDL conversations as being identical to HTTP (ie normal web page traffic). 关于SOAP / WSDL,从围绕TCP / WebSocket / HTTP的对话的角度来看,您可以将所有SOAP / WSDL对话视为与HTTP相同(即正常的网页流量)。

Finally, remember the stacked nature of network programming, for instance SOAP/WSDL looks like this: 最后,请记住网络编程的堆叠特性,例如SOAP / WSDL如下所示:

SOAP/WSDL
--------- (sits atop)
HTTP
--------- (sits atop)
TCP

And WebSockets look like this WebSockets看起来像这样

WebSocket
--------- (sits atop)
TCP

HTH. HTH。

JavaScript allows clients to communicate via HTTP with XMLHttpRequest. JavaScript允许客户端通过HTTP与XMLHttpRequest进行通信。 WebSockets extends this functionality to allow JavaScript to make arbitrary network I/O (not just HTTP), which is a logical extension and allows all sorts of applications that need to use TCP traffic (but might not be using the HTTP protocol) to be ported to JavaScript. WebSockets扩展了此功能,允许JavaScript生成任意网络I / O(不仅仅是HTTP),这是一个逻辑扩展,允许需要使用TCP流量(但可能不使用HTTP协议)的各种应用程序被移植到JavaScript。 I think it is rather logical that, as applications continue to move to the cloud, that HTML and JavaScript support everything that is available on the desktop. 我认为,随着应用程序继续迁移到云,HTML和JavaScript支持桌面上可用的所有内容,这是合乎逻辑的。

While a server can do non-HTTP network I/O on behalf of a JavaScript client and make that communication available over HTTP, this is not always the most appropriate or efficient thing to do. 虽然服务器可以代表JavaScript客户端执行非HTTP网络I / O并通过HTTP提供该通信,但这并不总是最合适或最有效的事情。 For example, it would not make sense to add an additional round-trip cost when attempting to make an online SSH terminal. 例如,在尝试建立在线SSH终端时添加额外的往返成本是没有意义的。 WebSockets makes it possible for JavaScript to talk directly to the SSH server. WebSockets使JavaScript可以直接与SSH服务器通信。

As for the syntax, part of it is based on XMLHttpRequest. 至于语法,它的一部分基于XMLHttpRequest。 As has been pointed out in the other posting, WebSockets is a fairly low-level API that can be wrapped in a more understandable one. 正如在其他帖子中指出的那样,WebSockets是一个相当低级的API,可以包含在一个更容易理解的API中。 It is more important that WebSockets support all the necessary applications than that it have the most elegant syntax (sometimes focusing on the syntax can lead to more restrictive functionality). 更重要的是,WebSockets支持所有必需的应用程序,而不是它具有最优雅的语法(有时专注于语法会导致更多限制性功能)。 Library authors can always make this very general API more manageable to other application developers. 库作者总是可以使这个非常通用的API对其他应用程序开发人员更易于管理。

As you noted WebSockets has low overhead . 正如您所指出的,WebSockets的开销很低 The overhead is similar to normal TCP sockets: just two bytes more per frame compared to hundreds for AJAX/Comet. 开销类似于普通的TCP套接字:每帧只有两个字节,而AJAX / Comet只有几百个字节。

Why low-level instead of some sort of built-in RPC functionality? 为什么低级而不是某种内置RPC功能? Some thoughts: 一些想法:

  • It's not that hard to take an existing RPC protocol and layer it on a low-level socket protocol. 使用现有的RPC协议并将其分层放在低级套接字协议上并不难。 You can't go the opposite direction and build a low-level connection if the RPC overhead is assumed. 如果假定RPC开销,则不能反向并构建低级连接。

  • WebSockets support is fairly trivial to add to multiple languages on the server side. WebSockets支持在服务器端添加到多种语言是相当简单的 The payload is just a UTF-8 string and pretty much every language has built-in efficient support for that. 有效载荷只是一个UTF-8字符串,几乎每种语言都有内置的有效支持。 An RPC mechanism not so much. RPC机制不是那么多。 How do you handle data type conversions between Javascript and the target language? 如何处理Javascript和目标语言之间的数据类型转换? Do you need to add type hinting on the Javascript side? 你需要在Javascript端添加类型提示吗? What about variable length arguments and/or argument lists? 可变长度参数和/或参数列表怎么样? Do you build these mechanisms if the language doesn't have a good answer? 如果语言没有很好的答案,你会建立这些机制吗? Etc. 等等。

  • Which RPC mechanism would it be modeled after? 以后会建模哪种RPC机制? Would you choose an existing one (SOAP, XML-RPC, JSON-RPC, Java RMI, AMF, RPyC, CORBA) or an entirely new one? 您会选择现有的(SOAP,XML-RPC,JSON-RPC,Java RMI,AMF,RPyC,CORBA)还是全新的?

  • Once client support is fairly universal, then many services that have normal TCP socket will add WebSockets support (because it's fairly trivial to add). 一旦客户端支持相当普遍,那么许多具有正常TCP套接字的服务将添加WebSockets支持(因为添加它是相当简单的)。 The same is not true if WebSockets was RPC based. 如果WebSockets是基于RPC的,则情况并非如此。 Some existing services might add an RPC layer, but for the most part WebSockets services would be created from scratch. 某些现有服务可能会添加RPC层,但大多数情况下,WebSockets服务都是从头开始创建的。

For my noVNC project (VNC client using just Javascript, Canvas, WebSockets) the low-overhead nature of WebSockets is critical for achieving reasonable performance. 对于我的noVNC项目(仅使用Javascript,Canvas,WebSockets的VNC客户端),WebSockets的低开销性质对于实现合理的性能至关重要。 Until VNC servers include WebSockets support, noVNC includes wsproxy which is a generic WebSockets to TCP socket proxy. 在VNC服务器包含WebSockets支持之前,noVNC包括wsproxy,它是TCP套接字代理的通用WebSockets。

If you are thinking about implementing an interactive web application and you haven't decided on server-side language, then I suggest looking at Socket.IO which is a library for node (server-side Javascript using Google's V8 engine). 如果您正在考虑实现交互式Web应用程序并且尚未决定使用服务器端语言,那么我建议您查看Socket.IO ,它是节点库(使用Google V8引擎的服务器端Javascript)。

In addition to all the advantages of node (same language on both sides, very efficient, power libraries, etc), Socket.IO gives you several things: 除了节点的所有优点(双方都是相同语言,非常高效,电源库等)之外,Socket.IO还提供了以下几点:

  • Provides both client and server framework library for handling connections. 提供用于处理连接的客户端和服务器框架库。

  • Detects the best transport supported by both client and server. 检测客户端和服务器支持的最佳传输。 Transports include (from best to worst): native WebSockets, WebSockets using flash emulation, various AJAX models. 传输包括(从最好到最差):本机WebSockets,使用Flash仿真的WebSockets,各种AJAX模型。

  • Consistent interface no matter what transport is used. 无论使用何种传输方式,都具有一致的接口。

  • Automatic encode/decode of Javascript datatypes. 自动编码/解码Javascript数据类型。

It wouldn't be that hard to create a RPC mechanism on top of Socket.IO since both side are the same language with the same native types. 在Socket.IO之上创建RPC机制并不困难,因为双方都是具有相同本机类型的相同语言。

WebSocket makes Comet and all other HTTP push type techniques legible by allowing requests to originate from the server. WebSocket通过允许来自服务器的请求使Comet和所有其他HTTP推送类型技术清晰可读。 It is kind of a sandboxed socket and gives us limited functionality. 它是一种沙盒插座,为我们提供了有限的功能。

However, the API is general enough for framework and library authors to improve on the interface in whichever way they desire. 但是,API足以让框架和库作者以他们想要的方式改进界面。 For example, you could write some RPC or RMI styled service on top of WebSockets that allows sending objects over the wire. 例如,您可以在WebSockets之上编写一些RPC或RMI样式的服务,允许通过线路发送对象。 Now internally they are being serialized in some unknown format, but the service user doesn't need to know and doesn't care. 现在在内部,它们以某种未知格式被序列化,但服务用户不需要知道也不关心。

So thinking from a spec authors POV, going from 因此,从规范作者POV开始思考

mysocket.send("AFunction|withparameters|segmented");

to

myServerObject.AFunction("that", "makessense");

is comparatively easy and requires writing a small wrapper around WebSockets so that serialization and deserialization happens opaquely to the application. 相对容易,并且需要在WebSockets周围编写一个小包装器,以便序列化和反序列化对应用程序不透明地发生。 But going in the reverse direction means the spec authors need to make a much more complex API which makes for a weaker foundation for writing code on top of it. 但是反过来意味着规范作者需要制作一个更复杂的API,这使得在其上编写代码的基础变弱。

I ran into the same problem where I needed to do something like call('AFunction', 'foo', 'bar') rather than serialize/de-serialize every interaction. 我遇到了同样的问题,我需要做一些像call('AFunction', 'foo', 'bar')而不是序列化/反序列化每次交互。 My preference was also to leave the bulk of code on the server and just use the Javascript to handle the view. 我的偏好也是将大量代码留在服务器上,只使用Javascript来处理视图。 WebSockets were a better fit because of its natural support for bi-directional communication. WebSockets因其对双向通信的自然支持而更适合。 To simplify my app development, I build a layer on top of WebSockets to make remote method calls (like RPC ). 为了简化我的应用程序开发,我在WebSockets之上构建了一个层来进行远程方法调用(如RPC )。

I have published the RMI/RPC library at http://sourceforge.net/projects/rmiwebsocket/ . 我已经在http://sourceforge.net/projects/rmiwebsocket/上发布了RMI/RPC库。 Once the communication is setup between the web-page and the servlet, you can execute calls in either direction. 在Web页面和servlet之间建立通信后,您可以在任一方向上执行调用。 The server uses reflection to call the appropriate method in the server-side object and client uses Javascript's 'call' method to call the appropriate function in the client-side object. 服务器使用反射来调用服务器端对象中的适当方法,客户端使用Javascript的“调用”方法来调用客户端对象中的相应函数。 The library uses Jackson to take care of the serialization/deserialization of various Java types to/from JSON. 该库使用Jackson来处理JSON中各种Java类型的序列化/反序列化。

WebSocket JSR was negotiated by a number of parties (Oracle, Apache, Eclipse, etc) all with very different agendas. WebSocket JSR由许多方(Oracle,Apache,Eclipse等)协商,所有方都有着截然不同的议程。 It's just as well they stopped at message transport level and left higher level constructs out. 它也是在消息传输级别停止并且离开更高级别的构造。 If what you need is a Java to JavaScript RMI, check out the FERMI Framework . 如果您需要的是Java to JavaScript RMI,请查看FERMI框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM