Akka(43): Http:SSE-Server Sent Event - 服务端主推消息
因为我了解Akka-http的主要目的不是为了有关Web-Server的编程,而是想实现一套系统集成的api,所以也需要考虑由服务端主动向客户端发送指令的应用场景。比如一个零售店管理平台的服务端在完成了某些数据更新后需要通知各零售门市客户端下载最新数据。虽然Akka-http也提供对websocket协议的支持,但websocket的网络连接是双向恒久的,适合频繁的问答交互式服务端与客户端的交流,消息结构也比较零碎。而我们面临的可能是批次型的大量数据库数据交换,只需要简单的服务端单向消息就行了,所以websocket不太合适,而Akka-http的SSE应该比较适合我们的要求。SSE模式的基本原理是服务端统一集中发布消息,各客户端持久订阅服务端发布的消息并从消息的内容中筛选出属于自己应该执行的指令,然后进行相应的处理。客户端接收SSE是在一个独立的线程里不断进行的,不会影响客户端当前的运算流程。当收到有用的消息后就会调用一个业务功能函数作为后台异步运算任务。
服务端的SSE发布是以Source[ServerSentEvent,NotUsed]来实现的。ServerSentEvent类型定义如下:
/**
* Representation of a server-sent event. According to the specification, an empty data field designates an event
* which is to be ignored which is useful for heartbeats.
*
* @param data data, may span multiple lines
* @param eventType optional type, must not contain \n or \r
* @param id optional id, must not contain \n or \r
* @param retry optional reconnection delay in milliseconds
*/
final case class ServerSentEvent(
data: String,
eventType: Option[String] = None,
id: Option[String] = None,
retry: Option[Int] = None) {...}
这个类型的参数代表事件消息的数据结构。用户可以根据实际需要充分利用这个数据结构来传递消息。服务端是通过complete以SeverSentEvent类为元素的Source来进行SSE的,如下:
import akka.http.scaladsl.marshalling.sse.EventStreamMarshalling._
complete {
Source
.tick(.seconds, .seconds, NotUsed)
.map( _ => processToServerSentEvent)
.keepAlive(.second, () => ServerSentEvent.heartbeat)
}
以上代码代表服务端定时运算processToServerSentEvent返回ServerSentEvent类型结果后发布给所有订阅的客户端。我们用一个函数processToServerSentEvent模拟重复运算的业务功能:
private def processToServerSentEvent: ServerSentEvent = {
Thread.sleep() //processing delay
ServerSentEvent(SyncFiles.fileToSync)
}
这个函数模拟发布事件数据是某种业务运算结果,在这里代表客户端需要下载文件名称。我们用客户端request来模拟设定这个文件名称:
object SyncFiles {
var fileToSync: String = ""
}
private def route = {
import Directives._
import akka.http.scaladsl.marshalling.sse.EventStreamMarshalling._
def syncRequests =
pathPrefix("sync") {
pathSingleSlash {
post {
parameter("file") { filename =>
complete {
SyncFiles.fileToSync = filename
s"set download file to : $filename"
}
}
}
}
}
客户端订阅SSE的方式如下:
import akka.http.scaladsl.unmarshalling.sse.EventStreamUnmarshalling._
import system.dispatcher Http()
.singleRequest(Get("http://localhost:8011/events"))
.flatMap(Unmarshal(_).to[Source[ServerSentEvent, NotUsed]])
.foreach(_.runForeach(se => downloadFiles(se.data)))
每当客户端收到SSE后即运行downloadFiles(filename)函数。downloadFiles函数定义:
def downloadFiles(file: String) = {
Thread.sleep() //process delay
if (file != "")
println(s"Try to download $file")
}
下面是客户端程序的测试运算步骤:
scala.io.StdIn.readLine()
println("do some thing ...")
Http().singleRequest(
HttpRequest(method=HttpMethods.POST,uri = "http://localhost:8011/sync/?file=Orders")
).onSuccess {
case msg => println(msg)
} scala.io.StdIn.readLine()
println("do some other things ...")
Http().singleRequest(
HttpRequest(method=HttpMethods.POST,uri = "http://localhost:8011/sync/?file=Items")
).onSuccess {
case msg => println(msg)
}
运算结果:
do some thing ...
HttpResponse( OK,List(Server: akka-http/10.0., Date: Fri, Dec :: GMT),HttpEntity.Strict(text/plain; charset=UTF-,set download file to : Orders),HttpProtocol(HTTP/1.1))
Try to download Orders
Try to download Orders do some other things ...
HttpResponse( OK,List(Server: akka-http/10.0., Date: Fri, Dec :: GMT),HttpEntity.Strict(text/plain; charset=UTF-,set download file to : Items),HttpProtocol(HTTP/1.1))
Try to download Orders
Try to download Orders
Try to download Items
Try to download Items Try to download Items Process finished with exit code
下面是本次讨论的示范源代码:
服务端:
import akka.NotUsed
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import scala.concurrent.duration.DurationInt
import akka.http.scaladsl.model.sse.ServerSentEvent object SSEServer { def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
Http().bindAndHandle(route, "localhost", ) scala.io.StdIn.readLine()
system.terminate()
} object SyncFiles {
var fileToSync: String = ""
}
private def route = {
import Directives._
import akka.http.scaladsl.marshalling.sse.EventStreamMarshalling._ def syncRequests =
pathPrefix("sync") {
pathSingleSlash {
post {
parameter("file") { filename =>
complete {
SyncFiles.fileToSync = filename
s"set download file to : $filename"
}
}
}
}
} def events =
path("events") {
get {
complete {
Source
.tick(.seconds, .seconds, NotUsed)
.map( _ => processToServerSentEvent)
.keepAlive(.second, () => ServerSentEvent.heartbeat)
}
}
} syncRequests ~ events
} private def processToServerSentEvent: ServerSentEvent = {
Thread.sleep() //processing delay
ServerSentEvent(SyncFiles.fileToSync)
}
}
客户端:
import akka.NotUsed
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding.Get
import akka.http.scaladsl.model.HttpMethods
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import akka.http.scaladsl.model.sse.ServerSentEvent
import akka.http.scaladsl.model._ object SSEClient { def downloadFiles(file: String) = {
Thread.sleep() //process delay
if (file != "")
println(s"Try to download $file")
} def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer() import akka.http.scaladsl.unmarshalling.sse.EventStreamUnmarshalling._
import system.dispatcher Http()
.singleRequest(Get("http://localhost:8011/events"))
.flatMap(Unmarshal(_).to[Source[ServerSentEvent, NotUsed]])
.foreach(_.runForeach(se => downloadFiles(se.data))) scala.io.StdIn.readLine()
println("do some thing ...")
Http().singleRequest(
HttpRequest(method=HttpMethods.POST,uri = "http://localhost:8011/sync/?file=Orders")
).onSuccess {
case msg => println(msg)
} scala.io.StdIn.readLine()
println("do some other things ...")
Http().singleRequest(
HttpRequest(method=HttpMethods.POST,uri = "http://localhost:8011/sync/?file=Items")
).onSuccess {
case msg => println(msg)
} scala.io.StdIn.readLine()
system.terminate()
}
}
我的博客即将同步至腾讯云+社区。邀大家一同入驻http://cloud.tencent.com/developer/support-plan
Akka(43): Http:SSE-Server Sent Event - 服务端主推消息的更多相关文章
- 1.使用SignalR实现页面即时刷新(服务端主动推送)
模块功能说明: 实现技术:sqlserver,MVC,WebAPI,ADO.NET,SignalR(服务器主动推送) 特殊车辆管理--->移动客户端采集数据存入数据库---->只要数据库数 ...
- 使用SignalR实现页面即时刷新(服务端主动推送)
模块功能说明: 实现技术:sqlserver,MVC,WebAPI,ADO.NET,SignalR(服务器主动推送) 特殊车辆管理--->移动客户端采集数据存入数据库---->只要数据库数 ...
- java Socket通信,客户端与服务端相互发消息
1.通信过程 网络分为应用层,http.ssh.telnet就是属于这一类,建立在传输层的基础上.其实就是定义了各自的编码解码格式,分层如下: 2.Socket连接 上述通信都要先在传输层有建立连接的 ...
- netty-2.客户端与服务端互发消息
(原) 第二篇,客户端与服务端互发消息 与第一篇的例子类似,这里服务端需要三个类,客户端也需要三个类. 服务端关键代码如下:MyServer与上一个例子中的TestServer 差多,这里只列举不同的 ...
- 使用SignalR从服务端主动推送警报日志到各种终端(桌面、移动、网页)
微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 使用SignalR从服务端主动推送警报日志到各种终端(桌面.移动.网页) 阅读导航 本文背景 ...
- SSE技术详解:使用 HTTP 做服务端数据推送应用的技术
SSE ( Server-sent Events )是 WebSocket 的一种轻量代替方案,使用 HTTP 协议. 严格地说,HTTP 协议是没有办法做服务器推送的,但是当服务器向客户端声明接下来 ...
- 在 Windows Server 上搭建 *** 服务端(转载加亲测)
转载自:https://diveng.io/build-shadowsocks-server-on-windows-server.html 下面的教程建议大家使用第一种方法安装,说是比较简单.我则使用 ...
- GraphQL-- 使用Apollo Server搭建Node服务端
一.关于Apollo Server Apollo Server是一种使用JS创建GraphQL服务端的一个方案.它的兼容性比较好,可以很好地和GraphQL客户端进行兼容.同时它可以 独立作为服务端进 ...
- SignalR 实现web浏览器客户端与服务端的推送功能
SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话. 换句话说,该对话可不受限制地进行单个无状态请求/响应数据交换:它将继 ...
随机推荐
- 工具:Entity Framework Profiler
使用方式: 1.项目中添加引用:EntityFrameworkProfiler.Appender 2.应用程序启动方法中添加下面一行代码: HibernatingRhinos.Profiler.App ...
- Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合
前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...
- R-CNN论文翻译——用于精确物体定位和语义分割的丰富特征层次结构
原文地址 我对深度学习应用于物体检测的开山之作R-CNN的论文进行了主要部分的翻译工作,R-CNN通过引入CNN让物体检测的性能水平上升了一个档次,但该文的想法比较自然原始,估计作者在写作的过程中已经 ...
- 如何优雅的设计 React 组件
作者:晓冬 本文原创,转载请注明作者及出处 如今的 Web 前端已被 React.Vue 和 Angular 三分天下,一统江山十几年的 jQuery 显然已经很难满足现在的开发模式.那么,为什么大家 ...
- CLR类型设计之泛型(一)
在讨论泛型之前,我们先讨论一下在没有泛型的世界里,如果我们想要创建一个独立于被包含类型的类和方法,我们需要定义objece类型,但是使用object就要面对装箱和拆箱的操作,装箱和拆箱会很损耗性能,我 ...
- 【Win 10 应用开发】UI Composition 札记(六):动画
动画在 XAML 中也有,而且基本上与 WPF 中的用法一样.不过,在 UWP 中,动画还有一种表现方式—— 通过 UI Composition 来创建. 基于 UI Composition 的动画, ...
- yaml在python中的应用简单整理
#简单介绍============================================================== YAML使用寄主语言的数据类型,这在多种语言中流传的时候可能会引 ...
- PHP设计模式之组合模式
当我们的一个对象可能代表一个单一的实体,或者一个组合的实体,但是仍然需要通过同样的方式被使用时,这种情形则适合使用组合模式的设计. 组合模式是一种结构型模式. 当看了书上的解释之后,并不是很理解,遂去 ...
- python 自动拉起进程脚本
cat /usr/local/ssdb/moniter_ssdb.py #!/usr/bin/env python import os import sys import commands #ssdb ...
- shell ping一个IP,延时大于5,输出延时大于5s,打印输出
# ping一个IP,延时大于5,输出延时大于5s,打印输出 #!/bin/bash ip=$* echo $ip num=`ping -c 10 ${ip}|grep icmp_seq|awk ' ...