-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
out/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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> |
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 | ||
} |