Scala:如何将小型Web服务器嵌入Scala应用程序? [英] Scala: how to embed a small web server into a scala app?

查看:151
本文介绍了Scala:如何将小型Web服务器嵌入Scala应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个旨在作为社区资源来帮助人们学习Scala的小型教育项目,我正在寻找一种在后台线程中提供和处理网页的简单方法.

For a little educational project intended as a community resource to help people learn Scala I am looking for a simple way to serve and process web pages in a background thread.

最低背景要求:Scalatron是一款多人游戏,玩家将机器人程序(用Scala编写)相互对抗.它附带了逐步的Scala教程.当前,玩家需要在其本地计算机上使用IDE来编译机器人,然后将机器人复制到共享的网络目录中,从而将它们发布到游戏中.这很麻烦.对于下一个版本,最好提供基于浏览器的机器人编辑和发布,以最大程度地简化组织者和播放器的设置.

Minimal background: Scalatron is a multi-player game in which players pit bot programs (written in Scala) against each other. It is accompanied by a step-by-step Scala tutorial. Currently players need to use an IDE on their local machines to compile bots, which are then published into the game by copying them into a shared network directory. This is cumbersome. For the next version it would be nice to offer browser-based bot editing and publishing to maximally simplify the setup for both organizers and players.

我已经有一个后台线程正在工作,该线程将编译Scala源代码,并将其动态地发送到服务器上(无需使用完整的IDE).接下来,我必须运行一个小型Web服务器,以为玩家提供访问点并处理漫游器上载(这消除了网络共享).

I already got a background thread working that will compile Scala source code arriving on the server on the fly (obviating the need for a full IDE). Next I have to run a tiny web server to provide an access point for players and to handle bot uploads (this gets rid of the network share).

这些要求是非常基本的:最初,我设想为一个页面提供一个编辑框和一个转到"按钮(=上传到服务器,编译并发布到游戏中);我希望每5或10秒在所有用户中上传的漫游器不超过20个,并且所有用户上传的漫游器不超过1个.我需要为每个用户保持最小状态(只是一个名称),并且需要向用户返回编译器错误消息.请注意,游戏屏幕更新将不会显示在浏览器中,而是显示在连接到服务器计算机的投影仪上.主要是考虑到最简单的设置(双击游戏服务器.jar),我认为最好在现有游戏服务器的后台线程中运行Web服务器.

The requirements are very basic: initially I envisage serving a single page with an edit box and a "Go" button (= upload to server, compile & publish into game); I expect no more than 20 concurrent users with not more than one bot upload across all users every 5 or 10 seconds; I need to hold minimal state for each users (just a name) and I need to return compiler error messages to the user. Note that game screen updates would NOT be displayed in the browser but on a projector attached to the server machine. And primarily to allow for the simplest possible setup (double click on the game server .jar) I think it would be nice to run the web server in a background thread within the existing game server.

最合适的方法是什么?我应该使用现有的框架,例如播放"还是提升"?是否存在用于执行与此类似的操作的现有代码?计划在这样的后台线程中运行Web服务器甚至合理吗?任何建议都是值得的.

What is the most appropriate way to do this? Should I use some existing framework, like Play or Lift? Is there existing code for doing something very similar to this? Is it even reasonable to plan to run a web server in a background thread like this? Any advice is appreciated.

推荐答案

One of the most used small, embeddable web servers / servlet containers in the Java world is Jetty. Since Scala can easily use Java libraries, it should be easy to use this from Scala.

此页面显示了一些有关如何开始的简单示例(使用Java)作为嵌入式HTTP服务器.

This page shows some simple examples (in Java) on how to start it as an embedded HTTP server.

这篇关于Scala:如何将小型Web服务器嵌入Scala应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆