简体   繁体   English

如何获得JavaScript和Scala之间的实时通信

[英]How to get real-time communication between JavaScript and Scala

I am trying to find best solution to communicate from Scala to JavaScript in real-time. 我正在尝试找到最佳的解决方案,以实现从Scala到JavaScript的实时通信。
So far tested couple ideas, but given library must handle around 1000 requests per second, and not every solution is best. 到目前为止,已经测试了几个主意,但是给定的库必须每秒处理大约1000个请求,并且并不是每个解决方案都是最佳的。
Below attached one of the methods. 下面附上一种方法。 if (modulo == 0) statement needs some fast library to push events to frontend. if (modulo == 0)语句需要一些快速库来将事件推送到前端。

What do you think about vert.x and its pub sub library? 您如何vert.x及其发布子库? https://github.com/vert-x/vertx-examples/tree/master/src/raw/scala/pubsub . https://github.com/vert-x/vertx-examples/tree/master/src/raw/scala/pubsub
And could this library handle 1000 request per second? 这个库每秒可以处理1000个请求吗? Same question with this https://github.com/vert-x/vertx-examples/tree/master/src/raw/scala/websockets I was trying this push messaging library https://www.scaledrone.com/ but it failed at the beginning (just 10 request per seconds). 与此https://github.com/vert-x/vertx-examples/tree/master/src/raw/scala/websockets相同的问题我正在尝试此推送消息库https://www.scaledrone.com/但它一开始失败(每秒仅10个请求)。

Or maybe I am expecting to much, and it is not easy to achieve so many request in easy way. 也许我期望很高,以简单的方式实现这么多的请求并不容易。 I am using Redis and it has pub sub protocol. 我正在使用Redis,它具有pub子协议。 So maybe there is some easy way to push data from Redis to JavaScript at the frontend? 因此,也许有一些简单的方法可以在前端将数据从Redis推送到JavaScript?

private def checkQueue(r: RedisClient, numbers: List[Int]): Unit = {
    val d1 = new Date()
    val format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val now = new Date()
    for (nr <- numbers) {
        val ranges = r.hkeys("user_" + nr + ":soldier:queue_time")
        ranges match {
            case Some(ss) => for (range <- ss) {
                val added_time = r.hget("user_" + nr + ":soldier:queue_time", range)
                val saved = format.parse(added_time.get)
                val diff = (now.getTime - saved.getTime) / 1000 // diff in sec
                val interval = r.hget("user_" + nr + ":soldier:interval", range)
                val modulo = diff % interval.get.toInt
                if (modulo == 0) {
                    val queue_amount = r.hget("user_" + nr + ":soldier:queue_amount", range)
                    if (queue_amount.get.toInt >= 1) {
                        r.hincrby("user_" + nr + ":soldier:amount", range, 1)
                        r.hincrby("user_" + nr + ":soldier:queue_amount", range, -1)
                    }
                }
            }
            case None =>
        }
    }
    val d2 = new Date()
    println("loop time: " + (d2.getTime - d1.getTime) + " milliseconds")
}
So maybe there is some easy way to push data from Redis to JavaScript at the frontend?

Check Webdis it provides HTTP interface to Redis. 检查Webdis,它为Redis提供HTTP接口。 Including Pubsub feature: 包括Pubsub功能:

Pub/Sub using Transfer-Encoding: chunked, works with JSONP as well. Webdis can be used as a Comet server.

You can also use Play to write your own Comet , Stream , or Websocket server. 您还可以使用Play编写自己的CometStreamWebsocket服务器。 You can also check this project for inspiration. 您也可以检查该项目的灵感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM