aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Client/Client.scala
blob: 1b843aba61ff55c85a55f0b5e9a5740f47a2e336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package o1game.Client

import java.lang.Thread.sleep
import java.net.Socket
import scala.io.Source
import scala.sys.process.stdout
import o1game.constants.*

class Client(ip: String, port: Int):
	private val socket = Socket(ip, port)
	private val input = socket.getInputStream
	private val output = socket.getOutputStream
	private val buffer: Array[Byte] = Array.ofDim(MAX_MSG_SIZE)
	private var bufferIndex = 0

	def startClient(): Unit =
		while true do
			sleep(POLL_INTERVAL)

			while input.available() != 0 do
				val bytesRead = input.read(buffer)
				if bytesRead != -1 then
					print(buffer.take(bytesRead).toVector.map(_.toChar).mkString)
					stdout.flush()

			bufferIndex = s"Houston, I think this shouldn't be so hard.\n".toVector.map(_.toByte).copyToArray(buffer)
			output.write(buffer, 0, bufferIndex)
			output.flush()