forked from chookspace/chookchat
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
7eba84b6ec | |||
![]() |
0a029fcc23 | ||
![]() |
eea257d617 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -5,6 +5,4 @@ server/.gradle
|
|||||||
server/uploads
|
server/uploads
|
||||||
server/userDatabase
|
server/userDatabase
|
||||||
server/chatHistory
|
server/chatHistory
|
||||||
client-cli/build
|
server/roomChats
|
||||||
client-cli/.gradle
|
|
||||||
client-cli/.chookpen.profile
|
|
||||||
|
13
client-python/README.md
Normal file
13
client-python/README.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
## Chookchat Python Client
|
||||||
|
|
||||||
|
This is an example client for Chookchat, written in Python. It is very simplistic. It connects to the Websocket, and prints recieved messages.
|
||||||
|
|
||||||
|
This would be a good baseline for a GUI client with tkinter, or a bot for Chookchat linking it to other services.
|
||||||
|
|
||||||
|
Not much else to say.
|
||||||
|
|
||||||
|
### Running
|
||||||
|
|
||||||
|
First, `pip3 install websocket-client rel`. If you need, create a virtual environment for Python.
|
||||||
|
|
||||||
|
Then, run with `python3 client.py`
|
68
client-python/client.py
Normal file
68
client-python/client.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import websocket
|
||||||
|
import json
|
||||||
|
import _thread
|
||||||
|
import time
|
||||||
|
import rel
|
||||||
|
from google import genai
|
||||||
|
|
||||||
|
username = "(insert username here)"
|
||||||
|
token = "(insert token here)"
|
||||||
|
|
||||||
|
def joinRoom(roomName):
|
||||||
|
ws.send(json.dumps({
|
||||||
|
"type": "joinRoom",
|
||||||
|
"username": username,
|
||||||
|
"token": token,
|
||||||
|
"room": roomName,
|
||||||
|
"content": ""
|
||||||
|
}))
|
||||||
|
|
||||||
|
def sendMessage(content):
|
||||||
|
ws.send(json.dumps({
|
||||||
|
"type": "message",
|
||||||
|
"username": username,
|
||||||
|
"token": token,
|
||||||
|
"content": content
|
||||||
|
}))
|
||||||
|
|
||||||
|
def sendTyping(content):
|
||||||
|
ws.send(json.dumps({
|
||||||
|
"type": "typing",
|
||||||
|
"username": username,
|
||||||
|
"token": token,
|
||||||
|
"content": content
|
||||||
|
}))
|
||||||
|
|
||||||
|
def on_message(ws, message):
|
||||||
|
print("Message received: " + message)
|
||||||
|
if message == "ping":
|
||||||
|
ws.send("pong")
|
||||||
|
|
||||||
|
def on_error(ws, error):
|
||||||
|
print("Error:", error)
|
||||||
|
|
||||||
|
def on_close(ws, close_status_code, close_msg):
|
||||||
|
print(f"Connection closed: {close_status_code} - {close_msg}")
|
||||||
|
|
||||||
|
def on_open(ws):
|
||||||
|
print("Opening connection to Chookchat...")
|
||||||
|
joinRoom("general")
|
||||||
|
ws.send(json.dumps({
|
||||||
|
"type": "connect",
|
||||||
|
"username": username,
|
||||||
|
"token": token,
|
||||||
|
"content": username + " joined the room!"
|
||||||
|
}))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
websocket.enableTrace(True)
|
||||||
|
ws = websocket.WebSocketApp("wss://bobcompass.online/api/websocket",
|
||||||
|
on_open=on_open,
|
||||||
|
on_message=on_message,
|
||||||
|
on_error=on_error,
|
||||||
|
on_close=on_close
|
||||||
|
)
|
||||||
|
|
||||||
|
ws.run_forever(dispatcher=rel, reconnect=5, ping_interval=30, ping_timeout=10)
|
||||||
|
rel.signal(2, rel.abort)
|
||||||
|
rel.dispatch()
|
1073
client-web/index.css
1073
client-web/index.css
File diff suppressed because it is too large
Load Diff
@@ -1,61 +1,171 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Chookchat</title>
|
<title>Chookchat</title>
|
||||||
<link rel="preconnect" href="https://rsms.me/">
|
<link rel="preconnect" href="https://rsms.me/">
|
||||||
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
|
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
|
||||||
<link type="text/css" rel="stylesheet" href="index.css">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||||
<link type="text/css" rel="stylesheet" href="gradient.css">
|
<link type="text/css" rel="stylesheet" href="index.css">
|
||||||
<link rel="shortcut icon" type="image/jpg" href="favicon.ico"/>
|
<link rel="shortcut icon" type="image/jpg" href="favicon.ico"/>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
|
||||||
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body class="gradient">
|
<body>
|
||||||
<div id="login">
|
<div id="login-container">
|
||||||
<div class="section">
|
<div class="login-card">
|
||||||
<div class="box">
|
<div class="login-header">
|
||||||
<h3>Chookchat</h3>
|
<h2>Chookchat</h2>
|
||||||
<input type="text" id="username" placeholder="Username"><br>
|
</div>
|
||||||
<input type="password" id="password" placeholder="Password"><br>
|
<div class="login-form">
|
||||||
<button class="bluebutton" onclick="connect()">Log in</button>
|
<div class="input-group">
|
||||||
<button class="greenbutton" onclick="register()">Register</button>
|
<i class="fa-solid fa-user"></i>
|
||||||
<button class="redbutton" onclick="showConfig()">Show Server Config</button>
|
<input type="text" id="username" placeholder="Username">
|
||||||
|
</div>
|
||||||
|
<div class="input-group">
|
||||||
|
<i class="fa-solid fa-lock"></i>
|
||||||
|
<input type="password" id="password" placeholder="Password">
|
||||||
|
</div>
|
||||||
|
<div class="login-buttons">
|
||||||
|
<button class="btn btn-primary" onclick="connect()">Log in</button>
|
||||||
|
<button class="btn btn-success" onclick="register()">Register</button>
|
||||||
|
</div>
|
||||||
|
<div class="server-options">
|
||||||
|
<button class="btn btn-text" onclick="showConfig()">Server Settings</button>
|
||||||
<div id="serverStatus"></div>
|
<div id="serverStatus"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="box" style="display: none;" id="serverconfig">
|
<div id="serverconfig" class="server-config">
|
||||||
<input type="text" id="serverUrl" value="bobcompass.online" placeholder="Server URL"><br>
|
<div class="input-group">
|
||||||
<input type="text" id="serverPort" value="443" placeholder="Server Port"><br>
|
<i class="fa-solid fa-server"></i>
|
||||||
<input type="checkbox" id="securityStatus" checked>
|
<input type="text" id="serverUrl" value="localhost" placeholder="Server URL">
|
||||||
<label for="securityStatus">Use HTTPS/WSS</label>
|
</div>
|
||||||
|
<div class="input-group">
|
||||||
|
<i class="fa-solid fa-network-wired"></i>
|
||||||
|
<input type="text" id="serverPort" value="7070" placeholder="Server Port">
|
||||||
|
</div>
|
||||||
|
<div class="checkbox-group">
|
||||||
|
<input type="checkbox" id="securityStatus" checked>
|
||||||
|
<label for="securityStatus">Use HTTPS/WSS</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="hidden" id="messaging">
|
</div>
|
||||||
<div class="messaging-container">
|
|
||||||
<div id="meet"></div>
|
<div id="app" class="hidden">
|
||||||
<div class="box">
|
<div class="sidebar">
|
||||||
<div id="users" class="suttle"></div>
|
<div class="sidebar-header">
|
||||||
<button id="meeting" class="bluebutton" onclick="startMeeting()">📞</button>
|
<h3>Chookchat</h3>
|
||||||
<div id="messagebox" class="box" style="height: 600px;"><div></div></div>
|
</div>
|
||||||
<div id="typing" class="suttle"></div>
|
<div id="room-list" class="room-list">
|
||||||
<div id="upload" class="suttle" style="display: none;"><input type="file" id="fileupload"><button class="bluebutton" onclick="uploadFile()">Upload</button></input></div>
|
<div class="room-category">
|
||||||
<div class="input-container">
|
<div class="room-category-header">
|
||||||
<button onclick="showFileUpload()" class="bluebutton">📁</button>
|
<span>Rooms</span>
|
||||||
<button onclick="showEggs()" class="bluebutton">🥚</button>
|
<button class="btn-icon" onclick="promptCreateRoom()">
|
||||||
|
<i class="fa-solid fa-plus"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<!-- Rooms will be dynamically added here -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="user-profile">
|
||||||
|
<div class="user-avatar">
|
||||||
|
<i class="fa-solid fa-user"></i>
|
||||||
|
</div>
|
||||||
|
<div class="user-info">
|
||||||
|
<span id="current-username">Username</span>
|
||||||
|
<span class="user-status">Online</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="chat-container">
|
||||||
|
<div class="chat-header">
|
||||||
|
<div class="current-room">
|
||||||
|
<i class="fa-solid fa-hashtag"></i>
|
||||||
|
<span id="current-room-name">general</span>
|
||||||
|
</div>
|
||||||
|
<div class="header-actions">
|
||||||
|
<button class="btn-icon" onclick="startMeeting()" title="Start Call">
|
||||||
|
<i class="fa-solid fa-phone"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="chat-content">
|
||||||
|
<div id="messagebox" class="messages">
|
||||||
|
<!-- Messages will be dynamically added here -->
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="typing" class="typing-indicator"></div>
|
||||||
|
|
||||||
|
<div class="message-input-container">
|
||||||
|
<div class="message-actions">
|
||||||
|
<button class="btn-icon" onclick="showFileUpload()" title="Upload File">
|
||||||
|
<i class="fa-solid fa-paperclip"></i>
|
||||||
|
</button>
|
||||||
|
<button class="btn-icon" onclick="showEggs()" title="Eggs">
|
||||||
|
<i class="fa-solid fa-egg"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="message-input-wrapper">
|
||||||
<input type="text" id="messageInput" placeholder="Send a message..." autofocus>
|
<input type="text" id="messageInput" placeholder="Send a message..." autofocus>
|
||||||
<button onclick="sendMessage()" class="bluebutton">Send</button>
|
<button class="send-button" onclick="sendMessage()">
|
||||||
|
<i class="fa-solid fa-paper-plane"></i>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="eggs" style="display: none;">
|
|
||||||
<div id="eggs-list" class="eggs-list">
|
|
||||||
<button class="egg-item" onclick="eggNotepad()">📝 Notepad</button>
|
|
||||||
</div>
|
|
||||||
<!-- Eggs Start Here -->
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="index.js"></script>
|
|
||||||
</body>
|
<div class="users-panel">
|
||||||
|
<div class="users-panel-header">
|
||||||
|
<h3>Members</h3>
|
||||||
|
</div>
|
||||||
|
<div id="users" class="users-list">
|
||||||
|
<!-- Users will be dynamically added here -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="eggs-panel" class="eggs-panel hidden">
|
||||||
|
<div class="eggs-header">
|
||||||
|
<h3>Eggs</h3>
|
||||||
|
<button class="btn-icon" onclick="closeEggs()">
|
||||||
|
<i class="fa-solid fa-times"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="eggs-list" class="eggs-list">
|
||||||
|
<button class="egg-item" onclick="eggNotepad()">
|
||||||
|
<i class="fa-solid fa-note-sticky"></i>
|
||||||
|
<span>Notepad</span>
|
||||||
|
</button>
|
||||||
|
<!-- More eggs can be added here -->
|
||||||
|
</div>
|
||||||
|
<div id="egg-content" class="egg-content">
|
||||||
|
<!-- Egg content will be displayed here -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="meet" class="meet-container hidden"></div>
|
||||||
|
|
||||||
|
<div id="upload-modal" class="modal hidden">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h3>Upload File</h3>
|
||||||
|
<button class="btn-icon" onclick="hideFileUpload()">
|
||||||
|
<i class="fa-solid fa-times"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<input type="file" id="fileupload" accept="image/*,text/*">
|
||||||
|
<div class="upload-preview" id="upload-preview"></div>
|
||||||
|
<button class="btn btn-primary" onclick="uploadFile()">Upload</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="index.js"></script>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
1145
client-web/index.js
1145
client-web/index.js
File diff suppressed because it is too large
Load Diff
@@ -1 +1 @@
|
|||||||
address:localhost;port:7070;security:false;serviceName:fnkjdsnfiewnifdwsocidw;
|
address:localhost;port:7070;security:false;serviceName:chookchat;
|
||||||
|
@@ -73,17 +73,147 @@ object config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
object WsSessionManager {
|
object WsSessionManager {
|
||||||
val peopleOnline = mutableListOf("")
|
val peopleOnline = mutableListOf<String>()
|
||||||
val sessionsList = mutableListOf("")
|
val sessionsList = mutableListOf<String>()
|
||||||
val sessions = ConcurrentHashMap<WsContext, String>()
|
val sessions = ConcurrentHashMap<WsContext, String>()
|
||||||
val sessionIds = ConcurrentHashMap<String, WsContext>()
|
val sessionIds = ConcurrentHashMap<String, WsContext>()
|
||||||
val userSessions = ConcurrentHashMap<String, String>()
|
val userSessions = ConcurrentHashMap<String, String>()
|
||||||
|
|
||||||
|
val roomList = mutableListOf<String>()
|
||||||
|
val userRooms = ConcurrentHashMap<String, String>()
|
||||||
|
val roomUsers = ConcurrentHashMap<String, MutableList<String>>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
createRoom("general")
|
||||||
|
|
||||||
fixedRateTimer("websocket-ping", period = 5000) {
|
fixedRateTimer("websocket-ping", period = 5000) {
|
||||||
sendPing()
|
sendPing()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createRoom(roomName: String): Boolean {
|
||||||
|
if (roomList.contains(roomName)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
roomList.add(roomName)
|
||||||
|
roomUsers[roomName] = mutableListOf()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun joinRoom(username: String, roomName: String): Boolean {
|
||||||
|
if (!roomList.contains(roomName)) {
|
||||||
|
createRoom(roomName)
|
||||||
|
}
|
||||||
|
|
||||||
|
val currentRoom = userRooms[username]
|
||||||
|
if (currentRoom != null) {
|
||||||
|
roomUsers[currentRoom]?.remove(username)
|
||||||
|
|
||||||
|
val leftMessage = JSONObject().apply {
|
||||||
|
put("type", "roomLeave")
|
||||||
|
put("username", "system")
|
||||||
|
put("room", currentRoom)
|
||||||
|
put("content", "$username left the room")
|
||||||
|
}
|
||||||
|
broadcastToRoom(currentRoom, leftMessage.toString())
|
||||||
|
|
||||||
|
broadcastRoomUsers(currentRoom)
|
||||||
|
}
|
||||||
|
|
||||||
|
userRooms[username] = roomName
|
||||||
|
roomUsers[roomName]?.add(username)
|
||||||
|
|
||||||
|
broadcastRoomUsers(roomName)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getRoomUsers(roomName: String): List<String> {
|
||||||
|
return roomUsers[roomName] ?: listOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun broadcastRoomUsers(roomName: String) {
|
||||||
|
val usersInRoom = roomUsers[roomName] ?: listOf()
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "roomUsers")
|
||||||
|
put("username", "system")
|
||||||
|
put("room", roomName)
|
||||||
|
put("content", usersInRoom.joinToString(", "))
|
||||||
|
}
|
||||||
|
broadcastToRoom(roomName, processedData.toString(), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun broadcastOnlineUsers() {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "users")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", peopleOnline.joinToString(", "))
|
||||||
|
}
|
||||||
|
broadcast(processedData.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun broadcastToRoom(roomName: String, message: String, cleanupDeadSessions: Boolean = true) {
|
||||||
|
val deadSessions = mutableListOf<WsContext>()
|
||||||
|
|
||||||
|
sessions.keys.forEach { ctx ->
|
||||||
|
try {
|
||||||
|
if (ctx.session.isOpen) {
|
||||||
|
val sessionId = sessions[ctx]
|
||||||
|
if (sessionId != null) {
|
||||||
|
val username = userSessions.entries.find { it.value == sessionId }?.key
|
||||||
|
if (username != null && userRooms[username] == roomName) {
|
||||||
|
ctx.send(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
deadSessions.add(ctx)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Error broadcasting to session: ${e.message}")
|
||||||
|
deadSessions.add(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cleanupDeadSessions) {
|
||||||
|
deadSessions.forEach { removeSessionWithoutBroadcast(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeSessionWithoutBroadcast(ctx: WsContext) {
|
||||||
|
try {
|
||||||
|
val sessionId = sessions[ctx]
|
||||||
|
if (sessionId != null) {
|
||||||
|
userSessions.entries.find { it.value == sessionId }?.let { entry ->
|
||||||
|
val username = entry.key
|
||||||
|
val room = userRooms[username]
|
||||||
|
|
||||||
|
if (room != null) {
|
||||||
|
roomUsers[room]?.remove(username)
|
||||||
|
}
|
||||||
|
|
||||||
|
peopleOnline.remove(username)
|
||||||
|
userSessions.remove(username)
|
||||||
|
userRooms.remove(username)
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionsList.remove(sessionId)
|
||||||
|
sessions.remove(ctx)
|
||||||
|
sessionIds.remove(sessionId)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Error removing session without broadcast: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun handleUserLogin(username: String) {
|
||||||
|
if (!peopleOnline.contains(username)) {
|
||||||
|
peopleOnline.add(username)
|
||||||
|
if (!userRooms.containsKey(username)) {
|
||||||
|
joinRoom(username, "general")
|
||||||
|
}
|
||||||
|
broadcastOnlineUsers()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun sendPing() {
|
private fun sendPing() {
|
||||||
val deadSessions = mutableListOf<WsContext>()
|
val deadSessions = mutableListOf<WsContext>()
|
||||||
|
|
||||||
@@ -99,31 +229,13 @@ object WsSessionManager {
|
|||||||
deadSessions.add(ctx)
|
deadSessions.add(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up any dead sessions
|
|
||||||
deadSessions.forEach { removeSession(it) }
|
deadSessions.forEach { removeSession(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun broadcastOnlineUsers() {
|
|
||||||
val processedData = JSONObject().apply {
|
|
||||||
put("type", "users")
|
|
||||||
put("username", "system")
|
|
||||||
put("content", peopleOnline.joinToString(", "))
|
|
||||||
}
|
|
||||||
broadcast(processedData.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
fun handleUserLogin(username: String) {
|
|
||||||
if (!peopleOnline.contains(username)) {
|
|
||||||
peopleOnline.add(username)
|
|
||||||
broadcastOnlineUsers()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addSession(ctx: WsContext) {
|
fun addSession(ctx: WsContext) {
|
||||||
try {
|
try {
|
||||||
val sessionId = UUID.randomUUID().toString()
|
val sessionId = UUID.randomUUID().toString()
|
||||||
sessionsList.add(sessionId) // Changed from += to add()
|
sessionsList.add(sessionId)
|
||||||
sessions[ctx] = sessionId
|
sessions[ctx] = sessionId
|
||||||
sessionIds[sessionId] = ctx
|
sessionIds[sessionId] = ctx
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -135,10 +247,18 @@ object WsSessionManager {
|
|||||||
try {
|
try {
|
||||||
val sessionId = sessions[ctx]
|
val sessionId = sessions[ctx]
|
||||||
if (sessionId != null) {
|
if (sessionId != null) {
|
||||||
// Find and remove the username associated with this session
|
|
||||||
userSessions.entries.find { it.value == sessionId }?.let { entry ->
|
userSessions.entries.find { it.value == sessionId }?.let { entry ->
|
||||||
peopleOnline.remove(entry.key)
|
val username = entry.key
|
||||||
userSessions.remove(entry.key)
|
val room = userRooms[username]
|
||||||
|
|
||||||
|
if (room != null) {
|
||||||
|
roomUsers[room]?.remove(username)
|
||||||
|
broadcastRoomUsers(room)
|
||||||
|
}
|
||||||
|
|
||||||
|
peopleOnline.remove(username)
|
||||||
|
userSessions.remove(username)
|
||||||
|
userRooms.remove(username)
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionsList.remove(sessionId)
|
sessionsList.remove(sessionId)
|
||||||
@@ -174,48 +294,202 @@ object WsSessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up any dead sessions
|
|
||||||
deadSessions.forEach { removeSession(it) }
|
deadSessions.forEach { removeSession(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSessionCount(): Int = sessions.size
|
fun getSessionCount(): Int = sessions.size
|
||||||
|
|
||||||
|
fun getUserRoom(username: String): String? {
|
||||||
|
return userRooms[username]
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getRooms(): List<String> {
|
||||||
|
return roomList
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun extractMessageContent(inputData: String, ctx: WsContext): String {
|
fun extractMessageContent(inputData: String, ctx: WsContext): String {
|
||||||
val jsonInputData = JSONObject(inputData)
|
val jsonInputData = JSONObject(inputData)
|
||||||
|
|
||||||
if (jsonInputData.getString("type") == "connect") {
|
if (jsonInputData.getString("type") == "connect") {
|
||||||
val username = jsonInputData.getString("username")
|
val username = jsonInputData.getString("username")
|
||||||
WsSessionManager.associateUserWithSession(username, ctx)
|
WsSessionManager.associateUserWithSession(username, ctx)
|
||||||
WsSessionManager.handleUserLogin(username)
|
WsSessionManager.handleUserLogin(username)
|
||||||
|
|
||||||
val processedData = JSONObject().apply {
|
val processedData = JSONObject().apply {
|
||||||
put("type", "connect")
|
put("type", "connect")
|
||||||
put("username", "system")
|
put("username", "system")
|
||||||
put("content", "${jsonInputData.getString("username")} just joined the room!")
|
put("content", "${jsonInputData.getString("username")} just joined the chat!")
|
||||||
}
|
}
|
||||||
return(processedData.toString())
|
return processedData.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (jsonInputData.getString("type") == "joinRoom") {
|
||||||
|
val username = jsonInputData.getString("username")
|
||||||
|
val roomName = jsonInputData.getString("room")
|
||||||
|
|
||||||
|
if (!jsonInputData.has("token")) {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "Authentication required")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
val success = WsSessionManager.joinRoom(username, roomName)
|
||||||
|
if (success) {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "roomJoin")
|
||||||
|
put("username", "system")
|
||||||
|
put("room", roomName)
|
||||||
|
put("content", "$username just joined the room!")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
} else {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "Failed to join room: $roomName")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonInputData.getString("type") == "getUsersInRoom") {
|
||||||
|
val username = jsonInputData.getString("username")
|
||||||
|
val roomName = jsonInputData.getString("room")
|
||||||
|
|
||||||
|
if (!jsonInputData.has("token")) {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "Authentication required")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
val usersInRoom = WsSessionManager.getRoomUsers(roomName)
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "roomUsers")
|
||||||
|
put("username", "system")
|
||||||
|
put("room", roomName)
|
||||||
|
put("content", usersInRoom.joinToString(", "))
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonInputData.getString("type") == "createRoom") {
|
||||||
|
val username = jsonInputData.getString("username")
|
||||||
|
val roomName = jsonInputData.getString("room")
|
||||||
|
|
||||||
|
if (!jsonInputData.has("token")) {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "Authentication required")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
val success = WsSessionManager.createRoom(roomName)
|
||||||
|
if (success) {
|
||||||
|
WsSessionManager.joinRoom(username, roomName)
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "roomCreated")
|
||||||
|
put("username", "system")
|
||||||
|
put("room", roomName)
|
||||||
|
put("content", "Room '$roomName' created and joined!")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
} else {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "Room '$roomName' already exists!")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val username = jsonInputData.getString("username")
|
||||||
|
val room = WsSessionManager.getUserRoom(username) ?: "general"
|
||||||
|
|
||||||
val processedData = JSONObject().apply {
|
val processedData = JSONObject().apply {
|
||||||
put("type", jsonInputData.getString("type"))
|
put("type", jsonInputData.getString("type"))
|
||||||
put("username", jsonInputData.getString("username"))
|
put("username", username)
|
||||||
|
put("room", room)
|
||||||
put("content", jsonInputData.getString("content"))
|
put("content", jsonInputData.getString("content"))
|
||||||
}
|
}
|
||||||
return(processedData.toString())
|
|
||||||
|
return processedData.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleSentMessage(inputData: String): String {
|
fun handleSentMessage(inputData: String): String {
|
||||||
println("API request recieved: $inputData")
|
println("API request received: $inputData")
|
||||||
var jsonInputData: JSONObject
|
var jsonInputData: JSONObject
|
||||||
try {jsonInputData = JSONObject(inputData)} catch (error: JSONException){return(error.toString())}
|
try {
|
||||||
|
jsonInputData = JSONObject(inputData)
|
||||||
|
} catch (error: JSONException) {
|
||||||
|
return error.toString()
|
||||||
|
}
|
||||||
|
|
||||||
val username = jsonInputData.getString("username")
|
val username = jsonInputData.getString("username")
|
||||||
val token = jsonInputData.getString("token")
|
val token = jsonInputData.getString("token")
|
||||||
val content = jsonInputData.getString("content")
|
val content = jsonInputData.getString("content")
|
||||||
|
val type = jsonInputData.getString("type")
|
||||||
|
|
||||||
|
if (jsonInputData.has("type")) {
|
||||||
|
val type = jsonInputData.getString("type")
|
||||||
|
if (type == "joinRoom" || type == "createRoom") {
|
||||||
|
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
||||||
|
var userLine = ""
|
||||||
|
|
||||||
|
userDatabaseParser.forEachLine { line ->
|
||||||
|
if (line.contains(username)) {
|
||||||
|
userLine = line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
userDatabaseParser.close()
|
||||||
|
|
||||||
|
if (userLine == "") {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "unknown-account")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
var tokenInDatabase = ""
|
||||||
|
var currentStage = 0
|
||||||
|
for (char in userLine) {
|
||||||
|
if (char == ':') {
|
||||||
|
currentStage++
|
||||||
|
}
|
||||||
|
if (currentStage == 1) {
|
||||||
|
tokenInDatabase += char
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokenInDatabase = tokenInDatabase.replace(":", "")
|
||||||
|
|
||||||
|
if (token != tokenInDatabase) {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "invalid-token")
|
||||||
|
}
|
||||||
|
return processedData.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Success"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
||||||
var lineNumber = 1
|
var lineNumber = 1
|
||||||
var userLine = ""
|
var userLine = ""
|
||||||
|
|
||||||
// Search the user database to find required information about the user
|
|
||||||
userDatabaseParser.forEachLine { line ->
|
userDatabaseParser.forEachLine { line ->
|
||||||
if (line.contains(username)) {
|
if (line.contains(username)) {
|
||||||
userLine = line
|
userLine = line
|
||||||
@@ -230,7 +504,7 @@ fun handleSentMessage(inputData: String): String {
|
|||||||
put("username", "system")
|
put("username", "system")
|
||||||
put("content", "unknown-account")
|
put("content", "unknown-account")
|
||||||
}
|
}
|
||||||
return(processedData.toString())
|
return processedData.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
var usernameInDatabase = ""
|
var usernameInDatabase = ""
|
||||||
@@ -255,133 +529,54 @@ fun handleSentMessage(inputData: String): String {
|
|||||||
tokenInDatabase = tokenInDatabase.replace(":", "")
|
tokenInDatabase = tokenInDatabase.replace(":", "")
|
||||||
saltInDatabase = saltInDatabase.replace(":", "")
|
saltInDatabase = saltInDatabase.replace(":", "")
|
||||||
banStatus = banStatus.replace(":", "")
|
banStatus = banStatus.replace(":", "")
|
||||||
|
|
||||||
if (banStatus == "1") {
|
if (banStatus == "1") {
|
||||||
val processedData = JSONObject().apply {
|
val processedData = JSONObject().apply {
|
||||||
put("type", "error")
|
put("type", "error")
|
||||||
put("username", "system")
|
put("username", "system")
|
||||||
put("content", "banned")
|
put("content", "banned")
|
||||||
}
|
}
|
||||||
return(processedData.toString())
|
return processedData.toString()
|
||||||
}
|
}
|
||||||
val tokenWithSalt = (md5(token + saltInDatabase))
|
|
||||||
/*println(saltInDatabase)
|
|
||||||
println(tokenWithSalt)
|
|
||||||
if (tokenWithSalt != tokenInDatabase) {*/
|
|
||||||
if (token != tokenInDatabase) {
|
if (token != tokenInDatabase) {
|
||||||
val processedData = JSONObject().apply {
|
val processedData = JSONObject().apply {
|
||||||
put("type", "error")
|
put("type", "error")
|
||||||
put("username", "system")
|
put("username", "system")
|
||||||
put("content", "invalid-token")
|
put("content", "invalid-token")
|
||||||
}
|
}
|
||||||
return(processedData.toString())
|
return processedData.toString()
|
||||||
}
|
}
|
||||||
// Make the message to respond to the client
|
|
||||||
val chatHistoryView = File("chatHistory")
|
|
||||||
var fullMessage = ""
|
|
||||||
if (content != "") {
|
if (content != "") {
|
||||||
fullMessage = "${chatHistoryView.readText()}$username: $content"
|
if (type != "message") {
|
||||||
// Add the client's message to the chat history
|
return "Success"
|
||||||
|
}
|
||||||
|
val room = WsSessionManager.getUserRoom(username) ?: "general"
|
||||||
|
|
||||||
|
val roomDirectory = File("roomChats")
|
||||||
|
if (!roomDirectory.exists()) {
|
||||||
|
roomDirectory.mkdir()
|
||||||
|
}
|
||||||
|
|
||||||
|
val roomChatHistory = File("roomChats/$room.txt")
|
||||||
|
roomChatHistory.appendText("$username: $content ${System.lineSeparator()}")
|
||||||
|
|
||||||
val chatHistory = File("chatHistory")
|
val chatHistory = File("chatHistory")
|
||||||
chatHistory.appendText("$username: $content ${System.lineSeparator()}")
|
chatHistory.appendText("$username: $content [Room: $room] ${System.lineSeparator()}")
|
||||||
return("Success")
|
|
||||||
|
return "Success"
|
||||||
} else {
|
} else {
|
||||||
return("No data provided")
|
return "No data provided"
|
||||||
}
|
}
|
||||||
return("Chookchat")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createAccount(inputData: String): String {
|
fun getRoomChatHistory(roomName: String): String {
|
||||||
println("Account creation request recieved: $inputData")
|
val roomChatFile = File("roomChats/$roomName.txt")
|
||||||
// Parse data sent to the server by client
|
if (roomChatFile.exists()) {
|
||||||
var username = ""
|
return roomChatFile.readText()
|
||||||
var token = ""
|
|
||||||
var message = ""
|
|
||||||
var dataType = ""
|
|
||||||
var isParsingData = 0
|
|
||||||
for (char in inputData) {
|
|
||||||
val character = char
|
|
||||||
if (character == ':') {
|
|
||||||
isParsingData = 1
|
|
||||||
} else if (isParsingData == 1) {
|
|
||||||
if (character == '}') {
|
|
||||||
isParsingData = 0
|
|
||||||
dataType = ""
|
|
||||||
} else if (character != '{') {
|
|
||||||
if (dataType == "username") {
|
|
||||||
username += character
|
|
||||||
} else if (dataType == "token") {
|
|
||||||
token += character
|
|
||||||
} else if (dataType == "message") {
|
|
||||||
message += character
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
dataType += character
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
return ""
|
||||||
var lineNumber = 1
|
|
||||||
var userExists = 0
|
|
||||||
|
|
||||||
// Search the user database to find required information about the user
|
|
||||||
var response = ""
|
|
||||||
userDatabaseParser.forEachLine { line ->
|
|
||||||
if (line.contains(username)) {
|
|
||||||
val processedData = JSONObject().apply {
|
|
||||||
put("type", "error")
|
|
||||||
put("username", "system")
|
|
||||||
put("content", "username-taken")
|
|
||||||
}
|
|
||||||
response = processedData.toString()
|
|
||||||
}
|
|
||||||
lineNumber++
|
|
||||||
}
|
|
||||||
if (response != "") {
|
|
||||||
return(response)
|
|
||||||
}
|
|
||||||
userDatabaseParser.close()
|
|
||||||
if (username == "") {
|
|
||||||
val processedData = JSONObject().apply {
|
|
||||||
put("type", "error")
|
|
||||||
put("username", "system")
|
|
||||||
put("content", "no-username")
|
|
||||||
}
|
|
||||||
return(processedData.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
if (token == "") {
|
|
||||||
val processedData = JSONObject().apply {
|
|
||||||
put("type", "error")
|
|
||||||
put("username", "system")
|
|
||||||
put("content", "no-token")
|
|
||||||
}
|
|
||||||
return(processedData.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
val userDatabaseFile = File("userDatabase")
|
|
||||||
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
|
|
||||||
val processedData = JSONObject().apply {
|
|
||||||
put("type", "success")
|
|
||||||
put("username", "system")
|
|
||||||
put("content", "success")
|
|
||||||
}
|
|
||||||
return(processedData.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
fun handleServerCommand(command: String): String {
|
|
||||||
val commandArgs = mutableListOf("")
|
|
||||||
commandArgs.drop(1)
|
|
||||||
var currentStage = 0
|
|
||||||
|
|
||||||
for (char in command) {
|
|
||||||
if (char == ' ') {
|
|
||||||
currentStage ++
|
|
||||||
commandArgs += ""
|
|
||||||
} else {
|
|
||||||
commandArgs[currentStage] += char
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return("I'm not sure how to ${commandArgs.toString()}")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun buildHTML(): String {
|
fun buildHTML(): String {
|
||||||
@@ -442,7 +637,6 @@ fun buildJS(): String {
|
|||||||
editedJS += "$line\n"
|
editedJS += "$line\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//editedJS += js
|
|
||||||
for (line in eggs) {
|
for (line in eggs) {
|
||||||
val eggJSFile = File("eggs/$line/index.js")
|
val eggJSFile = File("eggs/$line/index.js")
|
||||||
if (eggJSFile.exists()) {
|
if (eggJSFile.exists()) {
|
||||||
@@ -458,19 +652,136 @@ fun buildJS(): String {
|
|||||||
return("dingus")
|
return("dingus")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun handleServerCommand(command: String): String {
|
||||||
|
val commandArgs = mutableListOf("")
|
||||||
|
commandArgs.drop(1)
|
||||||
|
var currentStage = 0
|
||||||
|
|
||||||
|
for (char in command) {
|
||||||
|
if (char == ' ') {
|
||||||
|
currentStage ++
|
||||||
|
commandArgs += ""
|
||||||
|
} else {
|
||||||
|
commandArgs[currentStage] += char
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return("I'm not sure how to ${commandArgs.toString()}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createAccount(inputData: String): String {
|
||||||
|
println("Account creation request recieved: $inputData")
|
||||||
|
var username = ""
|
||||||
|
var token = ""
|
||||||
|
var message = ""
|
||||||
|
var dataType = ""
|
||||||
|
var isParsingData = 0
|
||||||
|
for (char in inputData) {
|
||||||
|
val character = char
|
||||||
|
if (character == ':') {
|
||||||
|
isParsingData = 1
|
||||||
|
} else if (isParsingData == 1) {
|
||||||
|
if (character == '}') {
|
||||||
|
isParsingData = 0
|
||||||
|
dataType = ""
|
||||||
|
} else if (character != '{') {
|
||||||
|
if (dataType == "username") {
|
||||||
|
username += character
|
||||||
|
} else if (dataType == "token") {
|
||||||
|
token += character
|
||||||
|
} else if (dataType == "message") {
|
||||||
|
message += character
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dataType += character
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
||||||
|
var lineNumber = 1
|
||||||
|
var userExists = 0
|
||||||
|
|
||||||
|
var response = ""
|
||||||
|
userDatabaseParser.forEachLine { line ->
|
||||||
|
if (line.contains(username)) {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "username-taken")
|
||||||
|
}
|
||||||
|
response = processedData.toString()
|
||||||
|
}
|
||||||
|
lineNumber++
|
||||||
|
}
|
||||||
|
if (response != "") {
|
||||||
|
return(response)
|
||||||
|
}
|
||||||
|
userDatabaseParser.close()
|
||||||
|
if (username == "") {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "no-username")
|
||||||
|
}
|
||||||
|
return(processedData.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token == "") {
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "error")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "no-token")
|
||||||
|
}
|
||||||
|
return(processedData.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
val userDatabaseFile = File("userDatabase")
|
||||||
|
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "success")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", "success")
|
||||||
|
}
|
||||||
|
return(processedData.toString())
|
||||||
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
WsSessionManager.peopleOnline.removeAt(0)
|
WsSessionManager.peopleOnline.clear()
|
||||||
WsSessionManager.sessionsList.removeAt(0)
|
WsSessionManager.sessionsList.clear()
|
||||||
|
WsSessionManager.roomList.clear()
|
||||||
|
|
||||||
|
WsSessionManager.createRoom("general")
|
||||||
|
|
||||||
|
val roomDirectory = File("roomChats")
|
||||||
|
if (!roomDirectory.exists()) {
|
||||||
|
roomDirectory.mkdir()
|
||||||
|
}
|
||||||
|
|
||||||
val app = Javalin.create { config ->
|
val app = Javalin.create { config ->
|
||||||
config.staticFiles.add("/public")
|
config.staticFiles.add("/public")
|
||||||
}.get("/") { ctx ->
|
}
|
||||||
|
.get("/") { ctx ->
|
||||||
ctx.html(buildHTML())
|
ctx.html(buildHTML())
|
||||||
//ctx.redirect("/index.html")
|
|
||||||
}
|
}
|
||||||
.get("/index.js") { ctx ->
|
.get("/index.js") { ctx ->
|
||||||
ctx.result(buildJS())
|
ctx.result(buildJS())
|
||||||
}
|
}
|
||||||
.get("/api/createaccount/{content}") { ctx -> ctx.result(createAccount(ctx.pathParam("content")))}
|
.get("/api/createaccount/{content}") { ctx ->
|
||||||
|
ctx.result(createAccount(ctx.pathParam("content")))
|
||||||
|
}
|
||||||
|
.get("/api/rooms") { ctx ->
|
||||||
|
val rooms = WsSessionManager.getRooms()
|
||||||
|
val roomsJson = JSONArray(rooms)
|
||||||
|
val processedData = JSONObject().apply {
|
||||||
|
put("type", "roomsList")
|
||||||
|
put("username", "system")
|
||||||
|
put("content", roomsJson.toString())
|
||||||
|
}
|
||||||
|
ctx.result(processedData.toString())
|
||||||
|
}
|
||||||
|
.get("/api/room/{roomName}/history") { ctx ->
|
||||||
|
val roomName = ctx.pathParam("roomName")
|
||||||
|
ctx.result(getRoomChatHistory(roomName))
|
||||||
|
}
|
||||||
.post("/api/upload") { ctx ->
|
.post("/api/upload") { ctx ->
|
||||||
val uploadedFiles = ctx.uploadedFiles()
|
val uploadedFiles = ctx.uploadedFiles()
|
||||||
if (uploadedFiles.isEmpty()) {
|
if (uploadedFiles.isEmpty()) {
|
||||||
@@ -485,18 +796,28 @@ fun main(args: Array<String>) {
|
|||||||
val newFilename = "${baseFilename}_${uuid}${if (fileExtension.isNotEmpty()) ".$fileExtension" else ""}"
|
val newFilename = "${baseFilename}_${uuid}${if (fileExtension.isNotEmpty()) ".$fileExtension" else ""}"
|
||||||
val filePath = Paths.get("uploads", newFilename)
|
val filePath = Paths.get("uploads", newFilename)
|
||||||
Files.copy(uploadedFile.content(), filePath)
|
Files.copy(uploadedFile.content(), filePath)
|
||||||
|
|
||||||
|
val room = if (ctx.formParam("room") != null) ctx.formParam("room") else "general"
|
||||||
|
|
||||||
val processedData = JSONObject().apply {
|
val processedData = JSONObject().apply {
|
||||||
put("type", "fileStatus")
|
put("type", "fileStatus")
|
||||||
put("username", "system")
|
put("username", "system")
|
||||||
put("content", "success")
|
put("content", "success")
|
||||||
}
|
}
|
||||||
ctx.result(processedData.toString())
|
ctx.result(processedData.toString())
|
||||||
|
|
||||||
val processedData2 = JSONObject().apply {
|
val processedData2 = JSONObject().apply {
|
||||||
put("type", "file")
|
put("type", "file")
|
||||||
put("username", "system")
|
put("username", "system")
|
||||||
|
put("room", room)
|
||||||
put("content", "https://maxwellj.xyz/chookchat/uploads/$newFilename")
|
put("content", "https://maxwellj.xyz/chookchat/uploads/$newFilename")
|
||||||
}
|
}
|
||||||
WsSessionManager.broadcast(processedData2.toString())
|
|
||||||
|
if (room != null && room != "general") {
|
||||||
|
WsSessionManager.broadcastToRoom(room, processedData2.toString())
|
||||||
|
} else {
|
||||||
|
WsSessionManager.broadcast(processedData2.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.ws("/api/websocket") { ws ->
|
.ws("/api/websocket") { ws ->
|
||||||
ws.onConnect { ctx ->
|
ws.onConnect { ctx ->
|
||||||
@@ -508,24 +829,42 @@ fun main(args: Array<String>) {
|
|||||||
ws.onMessage { ctx ->
|
ws.onMessage { ctx ->
|
||||||
when (ctx.message()) {
|
when (ctx.message()) {
|
||||||
"pong" -> {}
|
"pong" -> {}
|
||||||
else -> {
|
else -> {
|
||||||
println(ctx.message())
|
println(ctx.message())
|
||||||
val successState = handleSentMessage(ctx.message())
|
val successState = handleSentMessage(ctx.message())
|
||||||
if (successState != "Success") {
|
if (successState != "Success") {
|
||||||
try {
|
try {
|
||||||
ctx.send(successState)
|
ctx.send(successState)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
println("Error sending error message: ${e.message}")
|
println("Error sending error message: ${e.message}")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val messageContent = extractMessageContent(ctx.message(), ctx)
|
||||||
|
|
||||||
|
try {
|
||||||
|
val jsonMessage = JSONObject(messageContent)
|
||||||
|
if (jsonMessage.has("room")) {
|
||||||
|
val room = jsonMessage.getString("room")
|
||||||
|
if (WsSessionManager.roomList.contains(room)) {
|
||||||
|
WsSessionManager.broadcastToRoom(room, messageContent)
|
||||||
|
} else {
|
||||||
|
WsSessionManager.createRoom(room)
|
||||||
|
WsSessionManager.broadcastToRoom(room, messageContent)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
WsSessionManager.broadcastToRoom("general", messageContent)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Error in broadcasting message: ${e.message}")
|
||||||
|
WsSessionManager.broadcastToRoom("general", messageContent)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
val messageContent = extractMessageContent(ctx.message(), ctx)
|
|
||||||
WsSessionManager.broadcast(messageContent)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.start(7070)
|
.start(7070)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (args[0] == "-i") {
|
if (args[0] == "-i") {
|
||||||
println("Type a command for the server")
|
println("Type a command for the server")
|
||||||
@@ -539,4 +878,3 @@ fun main(args: Array<String>) {
|
|||||||
println("Interactive mode disabled, add -i to enable")
|
println("Interactive mode disabled, add -i to enable")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user