scala/akka/play 中的简单 Websocket

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17761453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:31:36  来源:igfitidea点击:

Simple Websocket in scala / akka / play

scalawebsocketakkaplayframework-2.1

提问by ticofab

I would like to create a simple Websocket application using Scala / Akka / Play.

我想使用 Scala/Akka/Play 创建一个简单的 Websocket 应用程序。

What I see from the examples (such as the webchator the recent talk at the Scala Days), is a blend between JavaScript / Coffeescript, html templates and so on.

我从示例(例如网络聊天最近在 Scala Days 上的演讲)中看到的是 JavaScript/Coffeescript、html 模板等之间的混合。

The clients of my Websocket application will be also native mobile apps (Android, iOS), so I need to think "out of the browser".

我的 Websocket 应用程序的客户端也将是本机移动应用程序(Android、iOS),所以我需要“跳出浏览器”思考。

How can I create a websocket application that can simply push a "hello" string?

如何创建一个可以简单地推送“hello”字符串的 websocket 应用程序?

回答by mor

There are two parts in a WebSocket connection: the server and the client. You could just make the server part using Play2 and implement a client with Android (see websocket-android-phonegap), iOS (see Unitt), javascript app...

WebSocket 连接有两部分:服务器和客户端。您可以使用 Play2 制作服务器部分并使用 Android(请参阅websocket-android-phonegap)、iOS(请参阅Unitt)、javascript 应用程序实现客户端...

Here is an example for a verybasic websocket connection taken from http://blog.tksfz.org/2012/10/12/websockets-echo-using-play-scala-and-actors-part-i/:

这是从http://blog.tksfz.org/2012/10/12/websockets-echo-using-play-scala-and-actors-part-i/获取的非常基本的 websocket 连接的示例:

package controllers

import play.api.mvc._

object Application extends Controller {
  def index = WebSocket.using[String] {
    val out = Enumerator.imperative[String]()
    val in = Iteratee.foreach[String] {
      msg =>
        out.push(msg)
    }
    (in, out)
  }
}

You only have to set a route that points to your controller conf/routes:

你只需要设置一个指向你的控制器的路由conf/routes

GET /connect  Application.index

The server is ready to run. You can then connect to your WebSocket with a javascript application, an Android application, etc... The client side is another matter.

服务器已准备好运行。然后,您可以使用 javascript 应用程序、Android 应用程序等连接到您的 WebSocket。客户端是另一回事。

If you use chrome or chromium, just open your javascript console in developer tools and you can connect to your server just like that:

如果您使用 chrome 或 Chromium,只需在开发人员工具中打开您的 javascript 控制台,您就可以像这样连接到您的服务器:

ws = new WebSocket('ws://localhost:9000/connect')
ws.onmessage = function( message ) { console.log( message ); };
ws.send('test')

It will send you back your message and log it in the javascript console whenever you send one.

每当您发送消息时,它都会将您的消息发回并将其记录在 javascript 控制台中。

You could also just use the http://www.websocket.org/echo.htmlecho test and feed it your 'ws://localhost:9000/connect'url.

您也可以只使用http://www.websocket.org/echo.html回声测试并将其提供给您的'ws://localhost:9000/connect'网址。

回答by ndeverge

Either you could use a pain old Socket, but you'll have problems with port forwarding (ie through firewalls) or you can could use Websocket client library, such as this one in Java :http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html

要么你可以使用旧的 Socket,但你会遇到端口转发问题(即通过防火墙),或者你可以使用 Websocket 客户端库,例如 Java 中的这个:http: //www.eclipse.org/jetty /documentation/current/jetty-websocket-client-api.html