aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Client
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-04 21:38:50 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-04 22:03:40 +0200
commitae82027a9bd4e75582f9499d4006b18c29a4129c (patch)
tree92d23012158647d31fbdc0e6cdae7854ee773cbc /src/main/scala/Client
downloadscalevalapokalypsi-ae82027a9bd4e75582f9499d4006b18c29a4129c.tar.gz
scalevalapokalypsi-ae82027a9bd4e75582f9499d4006b18c29a4129c.zip
Added basic TCP networking for the server.
The client's networking is still very experimental and the actual protocol is not yet specified for either side.
Diffstat (limited to 'src/main/scala/Client')
-rw-r--r--src/main/scala/Client/Client.scala28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/main/scala/Client/Client.scala b/src/main/scala/Client/Client.scala
new file mode 100644
index 0000000..1b843ab
--- /dev/null
+++ b/src/main/scala/Client/Client.scala
@@ -0,0 +1,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() \ No newline at end of file