为什么 Javascript 对 Websocket 很重要? [英] Why Javascript is IMPORTANT for Websocket?

查看:39
本文介绍了为什么 Javascript 对 Websocket 很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个奇怪的问题,但我真的很困惑,因为下载时 这个来自 Tornado 的例子 我想:好吧,我运行它,它会工作!但问题是,它确实有效,因为我在离线模式下进行了测试,并且缓存中没有 jQuery!

It seems a bizarre question but i'm really confused, because when downloaded this example from Tornado i thought that: okey, i run it, and it will work! but the problem, it dident work, because i tested in in Offline mode, and dident have jQuery in cache!

并发现 来自 SO:

Ajax - 创建到服务器的连接发送一些数据(简化为 get/post),并接收响应.

Ajax - create connection to server send some data (simplified as get / post), and receive response.

长轮询 - 创建到服务器的连接,发送一些数据,保持连接并有时从服务器接收一些数据.短时间保持连接,并定期重新连接.在服务器端,它仍然像网页一样处理.

Long poll - create connection to server, send some data, keep connection and receive sometimes from server some data. Connection is kept for short time, and does periodical reconnection. On server side it still dealt like Web Page.

WebSockets - 创建到服务器的连接,并根据需要保持.服务器或客户端可以轻松地制动它.双向发送数据.WebSockets 通常对每条消息使用掩码,以便对数据进行简单的加密.

WebSockets - create connection to server, and keep is as long as needed. Server or client can easily brake it. Bidirectional sending of data. WebSockets usually uses masking for each message so data is simply encrypted.

那么,为什么即使是与服务器端相关的东西,引擎盖下也总是有 Javascript?

So then, why there is always Javascript under the hood even it's something related to server side?

推荐答案

JavaScript 仅在浏览器中对 WebSocket 很重要,因为浏览器已经在 J​​avaScript 中实现了 WebSocket API(参见 RFC 6455).因此,如果您想从 HTML5 页面中访问 WebSocket,则需要编写 JavaScript.但是,您也可以使用 Java Applet 在 Java 客户端中执行 WebSocket.(尽管小程序已经失宠.)此外,可以从本机应用程序(包括移动 iOS)执行 WebSocket.

JavaScript is important to WebSocket only in browsers because the browsers have implemented the WebSocket API (See RFC 6455) in JavaScript. So if you want to access WebSocket from within an HTML5 page, you need to write JavaScript. However, you can also do WebSocket in a client in Java by using a Java Applet. (Although applets have fallen out of favor.) Additionally, it is possible to do WebSocket from native applications, including mobile iOS.

许多 WebSocket 服务器平台尝试支持多种类型的客户端.例如,Kaazing 不仅提供 JavaScript 客户端,还提供 .NET、Silverlight、Java 和 Objective-C 客户端.WebSocket 的基本思想是,您只需编写一次服务器逻辑,然后就可以收获"您在各种不同客户端中所做的工作.

Many WebSocket server platforms try to support multiple types of clients. For example, Kaazing provides clients not only in JavaScript, but also in .NET, Silverlight, Java and Objective-C. The basic idea with WebSocket is that you write your server logic once, and you can then "harvest" what you did in various different clients.

请记住,如果您打算使用 WebSocket,您将需要一种不同于基本 HTML 标签的编程语言,因为您需要处理通过 WebSocket 连接传入的数据.这些数据可以通过许多不同的协议获得,例如 AMQP、STOMP、socket.IO、WAMP 和许多其他协议.对于每种类型的协议,您实际上需要一个不同的库来处理协议.

Just keep in mind that if you are going to do WebSocket, you will need a programming language other than basic HTML tags because you need to process the data coming in over the WebSocket connection. That data can come via many different protocols, such as AMQP, STOMP, socket.IO, WAMP and many others. For each type of protocol, you will actually need a different library that can handle processing the protocol.

这篇关于为什么 Javascript 对 Websocket 很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆