如何实现我当前的Scala服务器以使用Akka并发?

时间:2019-05-12 08:12:07

标签: scala akka akka-http

我目前有一个Scala回显服务器,如何在代码中实现Akka,以便它可以提供已实现的HTML文件?

什至不知道如何开始解决这个问题,我是否必须导入类似Akka的东西?还是要更改我的依赖关系?因此,我不使用Java和Scala导入,而是将其更改为Akka?

我的回声服务器

import java.net._
import java.io._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.Breaks

object EchoServer {
  def read_and_write(in: BufferedReader, out: BufferedWriter): Unit = {
    out.write(in.readLine())
    out.flush()
    in.close()
    out.close()
  }

  def serve(server: ServerSocket): Unit = {
    val s = server.accept()
    val in = new BufferedReader(new InputStreamReader(s.getInputStream))
    val out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream))

    read_and_write(in, out)

    s.close()
  }

  def getListOfFiles(dir: String): List[File] = {
    val file = new File(dir)
    if (file.exists && file.isDirectory) {
      file.listFiles.filter(_.isFile).toList
    } else {
      List[File]()
    }
  }

  def main(args: Array[String]) {
    val server = new ServerSocket(9999)
    var readString = "C:\\Users\\Desktop\\EchoServer\\src\\main\\htmlFiles"
    println("What file are you looking for with extension?")
    implicit val ec = ExecutionContext.global
      while (true) {
        Future {
        implicit val ec = ExecutionContext.global
        val userInput = scala.io.StdIn.readLine()
        val result = getListOfFiles(readString)
        val loop = new Breaks
        try {
          result.foreach { file =>
            if (file.getName.endsWith(".html") == (userInput.endsWith(".html"))) {
              if (file.getName.equals(userInput)) {
                println(file.getName + " file found")
              }
              else {
                println("file not found")
              }
            }
          }
        }
        catch {
          case e: FileNotFoundException => println("Couldn't find that file.")
        }
        serve(server)
      }
    }
  }
}

我已经实现了期货,甚至不确定它是否正确提供HTML文件。我如何将其更改为akka并发?

0 个答案:

没有答案