Ringo 是一个 JavaScript 平台
ECMA JavaScript 规范将该语言描述为面向对象的编程语言,用于在主机环境中执行计算和处理计算对象。每个用 JavaScript 编写的应用程序都需要一个主机环境,它提供特定于环境的对象和 API 来执行 I / O。 Ringo 为 JavaScript 提供了这样一个环境,并附带一组模块以使应用程序开发更容易。由于其作为通用编程语言的特性,JavaScript 可以用来解决各种各样的问题,而 Ringo 可以帮助您这么做。利用 Ringo,编写命令行工具,复杂的 Web 应用程序甚至基于 Java UI 技术的 GUI 应用程序都很容易。
脚本语言如 JavaScript 需要一个引擎来解释和执行程序。 Ringo 没有自己的引擎。相反,它使用 Mozilla Rhino,一种 Java 中的 JavaScript 实现。犀牛的最初发展始于 Netscape 时代,并一直持续到现在。基本思想是将 JavaScript 程序编译为 Java 字节码,Java 字节码可以由 Java 虚拟机(JVM)执行。犀牛还提供了对 Java 标准类库和其他每个 Java 类的轻松访问。这使得将现有的 Java 库集成到新的 JavaScript 应用程序变得很容易。例如:Ringo 不是编写自己的 I / O 系统,而是使用现有的 Java I / O 类,并将它们封装起来以提供从 JavaScript 更容易的访问。
Ringo 在服务器或专用机器上执行 JavaScript,而不是在 Web 浏览器上下文中执行。如果您已经从基于 HTML 的应用程序中了解 JavaScript,则这是主要区别。没有什么像一个窗口对象,你没有一个 DOM 来操纵 HTML 对象。尽管如此,很多事情会像你从浏览器中知道的那样。您可以使用 console.log() 调试到控制台,但也有专用的日志记录模块可用于更复杂的日志记录。
Ringo 最大的优势之一就是模块系统。 Ringo 并没有自己构建代码,而是拥有一个易于使用的模块系统。它基于 CommonJS 模块,这是用于保持代码可互换的服务器端 JavaScript 环境的规范。如果您了解 Node.js 的模块,您还知道如何在 Ringo 中编写模块。一个模块封装了 JavaScript 方法和变量,并将它们与其他模块隔离。
模块 ringo/httpserver
该模块提供了启动和控制 HTTP Web 服务器的方法。 它是 Jetty Web 服务器的包装器,并且支持 WebSocket 协议。
Example
// starts the current module via module.id as web application
require("ringo/httpserver").main(module.id);
// starts the module "./app/actions" as web application
require("ringo/httpserver").main(module.resolve('./app/actions'));
See
Simple HTTP-Server example
Async HTTP-Server example
Eventsource example
Websocket example
Jetty – Servlet Engine and Http Server
Functions
Class Context
Instance Methods
- addServlet(servletPath, servlet, initParams)
- addWebSocket(path, onConnect, onCreate, initParams)
- serveApplication(app, engine)
- serveStatic(dir)
Class Server
Instance Methods
- destroy()
- getContext(path, virtualHosts, options)
- getDefaultContext()
- getJetty()
- isRunning()
- start()
- stop()
Class WebSocket
Instance Methods
- close()
- isOpen()
- send(message)
- sendBinary(byteArray, offset, length)
- sendBinaryAsync(byteArray, offset, length)
- sendString(message)
- sendStringAsync(message)
Context
不通过此模块导出为构造函数。
See
Server.prototype.getContext
Server.prototype.getDefaultContext
Context.prototype. addServlet (servletPath, servlet, initParams)
将此上下文中的请求路径映射到给定的 servlet。
Parameters
String | servletPath | the servlet path |
Servlet | servlet | a java object implementing the javax.servlet.Servlet interface. |
Object | initParams | optional object containing servlet init parameters |
Context.prototype. addWebSocket (path, onConnect, onCreate, initParams)
在这种情况下开始接受 WebSocket 连接。
Example
var context = server.getDefaultContext();
context.addWebSocket("/chat", function (socket) {
// reacts on an incoming message fromt the client
socket.onmessage = function(msg) {
// ...
};
// client closed the connection
socket.onclose = function() {
// ...
};
// sends a string to the client
socket.sendString("...");
});
Parameters
String | path | The URL path on which to accept WebSocket connections |
Function | onConnect | A function called for each new WebSocket connection with the WebSocket object and the session as arguments. |
Function | onCreate | Optional function called before a WebSocket instance is created. This function receives the request and response objects as arguments. Only if the function returns |
Object | initParams | Optional object containing servlet initialization parameters |
See
Context.prototype. serveApplication (app, engine)
将此上下文映射到 JSGI 应用程序。
Example
var server = new Server({ ... config ... });
// 1st way: app argument is a JSGI application function
server.getDefaultContext().serveApplication(function(req) {
return {
status: 200,
headers: {},
body: ["Hello World!"]
};
});
// 2nd way: app argument is an object
server.getDefaultContext().serveApplication({
appModule: module.resolve("./myWebapp"),
// myWebapp exports a function called 'app'
appName: "app"
});
// since serveApplication() doesn't start the server:
server.start();
Parameters
Function|Object | app | a JSGI application, either as a function or an object with the properties
|
RhinoEngine | engine | optional RhinoEngine instance for multi-engine setups |
See
Context.prototype. serveStatic (dir)
将该上下文映射到包含静态资源的目录。
Parameters
String | dir | the directory from which to serve static resources |
Server (options)
使用给定的选项创建一个 Jetty HTTP 服务器。这些选项可以定义要与默认 jetty.xml 一起使用的属性,也可以定义自定义配置文件。
Parameters
Object | options | A javascript object with any of the following properties (default values in parentheses):
For convenience, the constructor supports the definition of a JSGI application and static resource mapping in the options object using the following properties:
|
Server.prototype. destroy ()
销毁 HTTP 服务器,释放其资源。
Server.prototype. getContext (path, virtualHosts, options)
获取给定路径和虚拟主机的 servlet 应用程序 context,如果它不存在则创建它。
Parameters
String | path | the context root path such as |
String|Array | virtualHosts | optional single or multiple virtual host names. A virtual host may start with a |
Object | options | may have the following properties:
|
Returns
Context | a Context object |
See
Server.prototype. getDefaultContext ()
获取服务器的默认上下文。默认 context 是创建服务器时创建的上下文。
Returns
Context | the default Context |
See
Server.prototype. getJetty ()
获取 Jetty 服务器实例
Returns
org.eclipse.jetty.server.Server | the Jetty Server instance |
Server.prototype. isRunning ()
检查此服务器当前是否在运行。
Returns
Boolean | true if the server is running, false otherwise. |
Server.prototype. start ()
启动 HTTP 服务器。
Server.prototype. stop ()
停止 HTTP 服务器。
WebSocket
不通过此模块导出为构造函数。
WebSocket.prototype. close ()
关闭 WebSocket 连接。
WebSocket.prototype. isOpen ()
检查 WebSocket 是否打开。
Returns
Boolean | true if the connection is open |
WebSocket.prototype. send (message)
不推荐使用!
通过 WebSocket 发送一个字符串。
Parameters
String | message | a string |
See
WebSocket.prototype. sendBinary (byteArray, offset, length)
通过 WebSocket 发送一个字节数组。该方法阻塞,直到消息被发送。
Parameters
ByteArray | byteArray | The byte array to send |
Number | offset | Optional offset (defaults to zero) |
Number | length | Optional length (defaults to the length of the byte array) |
WebSocket.prototype. sendBinaryAsync (byteArray, offset, length)
通过 WebSocket 发送一个字节数组。这种方法不会等待消息被传输。
Parameters
ByteArray | byteArray | The byte array to send |
Number | offset | Optional offset (defaults to zero) |
Number | length | Optional length (defaults to the length of the byte array) |
Returns
java.util.concurrent.Future |
WebSocket.prototype. sendString (message)
通过 WebSocket 发送一个字符串。这种方法阻塞直到消息已经被发送
Parameters
String | message | a string |
WebSocket.prototype. sendStringAsync (message)
通过 WebSocket 发送一个字符串。这种方法不会等待消息被传输。
Parameters
String | message | a string |
destroy ()
守护程序生命周期函数由init脚本调用。释放服务器实例占用的任何资源。如果应用程序导出一个名为 destroy 的函数,它将作为参数与服务器一起调用。
Returns
Server | the Server instance. |
init (appPath)
守护程序生命周期函数由 init 脚本调用。使用 appPath 中的应用程序创建一个新的服务器。如果应用程序导出一个名为 init 的函数,它将以新服务器作为参数进行调用。
Parameters
String | appPath | optional application file name or module id. If undefined, the first command line argument will be used as application. If there are no command line arguments, module |
Returns
Server | the Server instance. |
main (appPath)
主要功能是从命令行启动 HTTP 服务器。它会自动添加一个关闭挂钩,以停止并销毁 JVM 终端处的服务器。
Example
// starts the current module via module.id as web application
require("ringo/httpserver").main(module.id);
// starts the module "./app/actions" as web application
require("ringo/httpserver").main(module.resolve('./app/actions'));
Parameters
String | appPath | optional application file name or module id. |
Returns
Server | the Server instance. |
start ()
守护程序生命周期函数由 init 脚本调用。启动由 init() 创建的服务器。如果应用程序导出一个名为 start 的函数,它将在服务器启动后立即作为参数调用。
Returns
Server | the Server instance. |
stop ()
守护程序生命周期函数由 init 脚本调用。停止由start() 启动的服务器。
Returns
Server | the Server instance. If the application exports a function called |