[脚本语言JavaScript]Ringo JS-模块 ringo/httpserver​​​​​​​

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

Class Server

Instance Methods

Class WebSocket

Instance Methods


Context

不通过此模块导出为构造函数。

See

Server.prototype.getContext
Server.prototype.getDefaultContext


Context.prototype. addServlet (servletPath, servlet, initParams)

将此上下文中的请求路径映射到给定的 servlet。

Parameters

StringservletPath

the servlet path

Servletservlet

a java object implementing the javax.servlet.Servlet interface.

ObjectinitParams

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

Stringpath

The URL path on which to accept WebSocket connections

FunctiononConnect

A function called for each new WebSocket connection with the WebSocket object and the session as arguments.

FunctiononCreate

Optional function called before a WebSocket instance is created. This function receives the request and response objects as arguments. Only if the function returns true the upgrade request is accepted and a WebSocket instance is created. Use this function for eg. authorization or authentication checks.

ObjectinitParams

Optional object containing servlet initialization parameters

See

WebSocket


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|Objectapp

a JSGI application, either as a function or an object with the properties

  • appModule - the application module containing the application function
  • appName - the property's name that is exported by the application module and which is a valid JSGI application.
defining the application.
RhinoEngineengine

optional RhinoEngine instance for multi-engine setups

See

Full example on Github


Context.prototype. serveStatic (dir)

将该上下文映射到包含静态资源的目录。

Parameters

Stringdir

the directory from which to serve static resources


Server (options)

使用给定的选项创建一个 Jetty HTTP 服务器。这些选项可以定义要与默认 jetty.xml 一起使用的属性,也可以定义自定义配置文件。

Parameters

Objectoptions

A javascript object with any of the following properties (default values in parentheses):

  • jettyConfig ('config/jetty.xml')
  • port (8080); overrides port of the default jetty.xml
  • host (localhost); overrides host of the default jetty.xml
  • sessions (true)
  • security (true)
  • statistics (false)
  • cookieName (null)
  • cookieDomain (null)
  • cookiePath (null)
  • httpOnlyCookies (false)
  • secureCookies (false)

For convenience, the constructor supports the definition of a JSGI application and static resource mapping in the options object using the following properties:

  • virtualHost (undefined)
  • mountpoint ('/')
  • staticDir ('static')
  • staticMountpoint ('/static')
  • appModule ('main')
  • appName ('app')

Server.prototype. destroy ()

销毁 HTTP 服务器,释放其资源。


Server.prototype. getContext (path, virtualHosts, options)

获取给定路径和虚拟主机的 servlet 应用程序 context,如果它不存在则创建它。

Parameters

Stringpath

the context root path such as "/" or "/app"

String|ArrayvirtualHosts

optional single or multiple virtual host names. A virtual host may start with a "*." wildcard.

Objectoptions

may have the following properties:

  • sessions: true to enable sessions for this context, false otherwise
  • security: true to enable security for this context, false otherwise
  • statistics: true to enable statistics for this context, false otherwise
  • cookieName: optional cookie name
  • cookieDomain: optional cookie domain
  • cookiePath: optional cookie path
  • httpOnlyCookies: true to enable http-only session cookies
  • secureCookies: true to enable secure session cookies

Returns

Context

a Context object

See

Context


Server.prototype. getDefaultContext ()

获取服务器的默认上下文。默认 context 是创建服务器时创建的上下文。

Returns

Context

the default Context

See

Context


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

Stringmessage

a string

See

sendString


WebSocket.prototype. sendBinary (byteArray, offset, length)

通过 WebSocket 发送一个字节数组。该方法阻塞,直到消息被发送。

Parameters

ByteArraybyteArray

The byte array to send

Numberoffset

Optional offset (defaults to zero)

Numberlength

Optional length (defaults to the length of the byte array)


WebSocket.prototype. sendBinaryAsync (byteArray, offset, length)

通过 WebSocket 发送一个字节数组。这种方法不会等待消息被传输。

Parameters

ByteArraybyteArray

The byte array to send

Numberoffset

Optional offset (defaults to zero)

Numberlength

Optional length (defaults to the length of the byte array)

Returns

java.util.concurrent.Future 

WebSocket.prototype. sendString (message)

通过 WebSocket 发送一个字符串。这种方法阻塞直到消息已经被发​​送

Parameters

Stringmessage

a string


WebSocket.prototype. sendStringAsync (message)

通过 WebSocket 发送一个字符串。这种方法不会等待消息被传输。

Parameters

Stringmessage

a string


destroy ()

守护程序生命周期函数由init脚本调用。释放服务器实例占用的任何资源。如果应用程序导出一个名为 destroy 的函数,它将作为参数与服务器一起调用。

Returns

Server

the Server instance.


init (appPath)

守护程序生命周期函数由 init 脚本调用。使用 appPath 中的应用程序创建一个新的服务器。如果应用程序导出一个名为 init 的函数,它将以新服务器作为参数进行调用。

Parameters

StringappPath

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 main in the current working directory is used.

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

StringappPath

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 stop, it will be invoked with the server as argument immediately before it is stopped.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值