Xitrum学习笔记09 - 异步响应

上传人:nt****6 文档编号:35163177 上传时间:2018-03-10 格式:PDF 页数:10 大小:582.47KB
返回 下载 相关 举报
Xitrum学习笔记09 - 异步响应_第1页
第1页 / 共10页
Xitrum学习笔记09 - 异步响应_第2页
第2页 / 共10页
Xitrum学习笔记09 - 异步响应_第3页
第3页 / 共10页
Xitrum学习笔记09 - 异步响应_第4页
第4页 / 共10页
Xitrum学习笔记09 - 异步响应_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《Xitrum学习笔记09 - 异步响应》由会员分享,可在线阅读,更多相关《Xitrum学习笔记09 - 异步响应(10页珍藏版)》请在金锄头文库上搜索。

1、博 客 园 新 随 笔 联 系 订 阅 管 理 随 笔 -35 章 -0 评 论 -0 秦 Xitrum 学 习 笔 记 09 - 异 步 响 应 Xitrum 不 会 动 发 送 默 认 响 应 , 必 须 调 respondXXX 法发送响应。如果没有调 respondXXX , Xitrum 会 保 持 HTTP 连 接 , 可 以 过 后 调 respondXXX 。 调 channel.isOpen 来 检 查 HTTP 连 接 是 否还处于打开状态。还可以使 addConnectionClosedListener 法 , 定 义连接关闭之后的动作。 addConnectionClo

2、sedListener / The connection has been closed / Unsubscribe from events, release resources etc. 因 为 异 步 性 质 , 响 应 不 会 刻 被 发 送 。 respondXXX 返回 ty.channel.ChannelFuture ,可以它来 执 响 应 被 发 送 后 的 动 作 。 例 如 : import ty.channel.ChannelFuture, ChannelFutureListener val future = respondText(“Hello“) future.addL

3、istener(new ChannelFutureListener def operationComplete(future: ChannelFuture) future.getChannel.close() ) 公 告 昵 称 : 秦 园 龄 : 1 年 2 个 粉 丝 : 2 关 注 : 1 + 加 关 注 三 四 五 六 26 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 搜 索 找 找 看 歌 搜 索 常 链 接 我

4、的 随 笔 我 的 评 论 我 的 参 与 最 新 评 论 我 的 标 签 Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD或 者 更 简 单 的 respondText(“Hello“).addListener(ChannelFutureListener.CLOSE) WebSocket import scala.runtime.ScalaRunTime import xitrum.annotation.WEBSOCKET import xitrum.WebSocketAction, WebS

5、ocketBinary, WebSocketText, WebSocketPing, WebSocketPong WEBSOCKET(“echo“) class EchoWebSocketActor extends WebSocketAction def execute() / Here you can extract session data, request headers etc. / but do not use respondText, respondView etc. / To respond, use respondWebSocketXXX like below. log.deb

6、ug(“onOpen“) context.become case WebSocketText(text) = log.info(“onTextMessage: “ + text) respondWebSocketText(text.toUpperCase) case WebSocketBinary(bytes) = log.info(“onBinaryMessage: “ + ScalaRunTime.stringOf(bytes) respondWebSocketBinary(bytes) case WebSocketPing =log.debug(“onPing“) case WebSoc

7、ketPong =log.debug(“onPong“) 我 的 标 签 Xitrum(2) 随 笔 分 类 AngularJS(2) hadoop HBase-1.2(8) Storm-1.1.0(6) Xitrum(18) Xitrum-Scalate-Jade(1) 随 笔 档 案 2017 年 10 (1) 2017 年 9 (8) 2017 年 8 (5) 2017 年 7 (1) 2017 年 5 (20) 阅 读 排 榜 1. Xitrum 学 习 笔 记 01 - 简 介 (188) 2. Xitrum 学 习 笔 记 04 - RESTful APIs(142) 3. Xit

8、rum 学 习 笔 记 03 - Action 和 View(92) 4. Storm HBase 集 成 (80) 5. Xitrum 学 习 笔 记 19 - 部 署 到 产 环 境 (76) Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD override def postStop() log.debug(“onClose“) super.postStop() 个 Actor 在 由 请 求 的 时 候 会 被 创 建 , 它 在 以 下 情 况下回被停:连接关闭; WebSocket

9、close frame 被 接 收 或 发 送 。 来 发 送 WebSocket frames 的 法 : respondWebSocketText respondWebSocketBinary respondWebSocketPing respondWebSocketClose 当 Xitrum 接 收 ping frame 时 , 会 动 发 送 pong frame , 所以没有 respondWebSocketPong 的法 获 取 WebSocket action 的 URL : / Probably you want to use this in Scalate view etc

10、. val url = absWebSocketUrlEchoWebSocketActor SockJS SockJS 是 个 浏 览 器 端 的 JavaScript 库 , 为 不 持 WebSocket 的浏览器提供类似于 WebSocket 的对象。 SockJS 会 先 尝 试 使 WebSocket , 如 果 失 败 则会使类似于 WebSocket 的对象。 如 果 要 在 所 有 的 浏 览 器 都 是 WebSocket API ,则应该使 SockJS 避免直接使 WebSocket Create PDF in your applications with the Pdf

11、crowd HTML to PDF API PDFCROWD var sock = new SockJS(http:/ sock.onopen = function() console.log(open); ; sock.onmessage = function(e) console.log(message, e.data); ; sock.onclose = function() console.log(close); ; Xitrum 通 过 在 View 模 板 中 调 jsDefaults 包含了 SockJS 的 JavaScript 个 Actor 在 出 现 新 的 SockJS

12、 session 时 被 创 建 ,在 SockJS session 关闭时被停。 使 respondSockJsText 和 respondSockJsClose 发送 SockJS frame 。 代 码 例 : import xitrum.Action, SockJsAction, SockJsText import xitrum.annotation.SOCKJS SOCKJS(“echo“) class EchoSockJsActor extends SockJsAction def execute() / To respond, use respondSockJsXXX like

13、below log.info(“onOpen“) context.become Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD case SockJsText(text) = log.info(“onMessage: “ + text) respondSockJsText(text) override def postStop() log.info(“onClose“) super.postStop() 分 块 响 应 要 发 送 分 块 响 应 1. 调 setChunked 2. 多 次

14、调 respondXXX 3. 最 后 , 调 respondLastChunk 分 块 响 应 例 , 创 建 个 很 的 CSV 件 / “Cache-Control“ header will be automatically set to: / “no-store, no-cache, must-revalidate, max-age=0“ / / Note that “Pragma: no-cache“ is linked to requests, not responses: / http:/ setChunked() val generator = new MyCsvGenerat

15、or generator.onFirstLine line = Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD val future = respondText(header, “text/csv“) future.addListener(new ChannelFutureListener def operationComplete(future: ChannelFuture) if (future.isSuccess) generator.next() generator.onNextLine line =val future = respondText(line) future.addListener(new ChannelFutureListener def operationComplete(future: ChannelFuture) if (future.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号