diff --git a/src/Channel.kt b/src/Channel.kt index 69cc21d..1582d7d 100644 --- a/src/Channel.kt +++ b/src/Channel.kt @@ -28,7 +28,8 @@ class Channel : AsynchronousListener { override fun processClosed(e: Event) { this.clients.remove(e.client); - System.out.println("A client disconnected ("+this.clients.count()+" clients connected"); + ConnectChannel.notifyDisconnect(e.client); + System.out.println("A client disconnected ("+this.clients.count()+" clients connected)"); } override fun processConnection(c: Client) { @@ -40,13 +41,18 @@ class Channel : AsynchronousListener { try{ val json = JSONObject(e.message.getString()) + if(json.has("close")){ + return + } + if(e.client.data["IsLogged"] != true){ //json format : {username:xxx} - if(json.getString("name") != null){ - e.client.data["IsLogged"] = true; + if(json.has("name") && json.getString("name") != null){ e.client.data["Username"] = json.getString("name"); if(ConnectChannel.isConnected(e.client)){ + System.out.println("A client connected ("+this.clients.count()+" clients connected)"); + e.client.data["IsLogged"] = true; val jsonLogin = JSONObject(); jsonLogin.put("error",false); e.client.send(buildTextMessage(jsonLogin.toString())); @@ -54,7 +60,7 @@ class Channel : AsynchronousListener { val array = JSONArray(); synchronized(this.messages,{ - this.messages.forEach { + this.messages.toList().forEach { array.put(JSONArray(listOf(it.first.data["Username"],it.second))) } }) @@ -73,7 +79,7 @@ class Channel : AsynchronousListener { }else{ //tried to access chat without logging in - val jsonError = JSONObject(""); + val jsonError = JSONObject(); jsonError.put("error","You must send your credential before sending anything else"); e.client.send(buildTextMessage(jsonError.toString())); diff --git a/src/ConnectChannel.kt b/src/ConnectChannel.kt index 8973038..dc8181d 100644 --- a/src/ConnectChannel.kt +++ b/src/ConnectChannel.kt @@ -7,10 +7,10 @@ import seekdasky.kWebSocket.Message.InteropMessage object ConnectChannel : InteropListener { - var lastGuestIndex = 0; + private var lastGuestIndex = 0; - val notBoundYet = Stack>(50); - val connected = mutableMapOf(); + private val notBoundYet = Stack>(500); + private val connected = mutableMapOf(); override fun filter(e: InteropEvent): Boolean { @@ -27,7 +27,7 @@ object ConnectChannel : InteropListener { val json = JSONObject(e.message.string); if(json.getString("type") == "guest"){ - this.lastGuestIndex = ++this.lastGuestIndex % 10; + this.lastGuestIndex = ++this.lastGuestIndex % 500; val guestName = "Guest"+this.lastGuestIndex; val jsonReturn = JSONObject(); jsonReturn.put("error", false); @@ -53,17 +53,28 @@ object ConnectChannel : InteropListener { } fun isConnected(client : Client) : Boolean{ - if(!this.connected.containsKey(client)){ - for(p in this.notBoundYet) { - if (p.second == client.data.get("Username")) { - this.connected.put(client, p.first); - this.notBoundYet.remove(p); - return true; - } + for(c in this.connected.keys){ + if(c.data["Username"] == client.data["Username"]){ + return true; } - return false; - }else{ - return true; + } + + for(p in this.notBoundYet.toList()) { + if (p.second == client.data["Username"]) { + this.connected.put(client, p.first); + this.notBoundYet.remove(p); + return true; + } + } + + return false; + } + + fun notifyDisconnect(client : Client){ + val clientType = this.connected[client]; + if(clientType != null){ + this.notBoundYet.push(Pair(clientType,client.data["Username"].toString())); + this.connected.remove(client); } } } \ No newline at end of file diff --git a/src/Stack.kt b/src/Stack.kt index 48f4873..502cb15 100644 --- a/src/Stack.kt +++ b/src/Stack.kt @@ -1,4 +1,4 @@ -class Stack(size : Int):Iterator { +class Stack(size : Int){ var itCounter = 0; val size = size; @@ -30,21 +30,8 @@ class Stack(size : Int):Iterator { this.items.remove(item); } - override fun hasNext(): Boolean { - val hasNext = itCounter < count() - - // As soon as condition fails, reset the counter - if (!hasNext) itCounter = 0 - - return hasNext - } - - override fun next(): T { - if (hasNext()) { - return this.items[this.itCounter++] - } else { - throw NoSuchElementException("No such element") - } + fun toList() : List{ + return this.items.toList(); } } \ No newline at end of file