4

I'm writing a small MUD style game in Scala. I want to allow users to telnet in to the game (just like in the old days). This really is just a game. It's going to run on a private network. Security is not (yet) a consideration.

The "protocol" consists of lines of text (terminated with CR) sent by the user. The server will reply with lines of text of it's own before waiting for the next line of user's input. In effect this is a REPL style text interface.

Is there a handy library that will do the networking stuff for me? I just want it to open up a port and allow users to connect to the service and start sending and receiving text from the game.

There are plenty of full-featured server libraries (e.g. TwitterServer) which seem to be geared towards Thrift and HTTP. I'm actually after something much simpler. I just want to receive lines of text, and respond with lines of text.

Any ideas about how I might be able to achieve this with the greatest possible simplicity?

Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
  • UPDATE: I'd also be interested to see if there's an example of a super-simple websocket server. :-) – Salim Fadhley Jun 14 '16 at 21:35
  • 1
    This question is worth a look: https://stackoverflow.com/questions/6414942/scala-equivalent-of-python-echo-server-client-example – Brian Jun 14 '16 at 22:09

1 Answers1

8

Here you go:

  val acceptor = new ServerSocket(port)
  while(true) {
    val socket = acceptor.accept
    Future { serve(socket.getInputStream, socket.getOutputStream) } onComplete { socket.close() }
  } 
Dima
  • 39,570
  • 6
  • 44
  • 70