Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SerVB committed Dec 31, 2019
0 parents commit 09e45ac
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out/
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/libraries/KotlinJavaRuntime.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/libraries/org_java_websocket_Java_WebSocket_1_4_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions chat-server.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="org.java-websocket:Java-WebSocket:1.4.0" level="project" />
</component>
</module>
58 changes: 58 additions & 0 deletions src/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import org.java_websocket.WebSocket
import org.java_websocket.handshake.ClientHandshake
import org.java_websocket.server.WebSocketServer
import java.net.InetSocketAddress

class ChatServer(port: Int) : WebSocketServer(InetSocketAddress(port)) {

private var nextId = 0

override fun onOpen(connection: WebSocket, handshake: ClientHandshake?) {
val id = ++nextId

println("#$id connected")
connection.setAttachment(id)
}

override fun onClose(connection: WebSocket, code: Int, reason: String?, remote: Boolean) {
println("#${connection.getAttachment<Int>()} disconnected")
}

override fun onMessage(connection: WebSocket, message: String) {
val toClientMessage = "${connection.getAttachment<Int>()}: $message"

connections.filter(WebSocket::isOpen).forEach { openedConnection ->
try {
openedConnection.send(toClientMessage)
} catch (t: Throwable) {
}
}
}

override fun onStart() {
println("server starts on port $port")
}

override fun onError(connection: WebSocket, e: Exception?) {
println("error: $e")
}
}

object Main {

@JvmStatic
fun main(vararg args: String) {
val server = ChatServer(DEFAULT_PORT).apply {
start()
}

Runtime.getRuntime().addShutdownHook(object : Thread() {

override fun run() {
server.stop()
}
})
}

private const val DEFAULT_PORT = 8885
}

0 comments on commit 09e45ac

Please sign in to comment.