113 lines
4.0 KiB
Kotlin
113 lines
4.0 KiB
Kotlin
import Collections.Stack
|
|
import org.json.JSONArray
|
|
import org.json.JSONObject
|
|
import seekdasky.kWebSocket.Client
|
|
import seekdasky.kWebSocket.Event
|
|
import seekdasky.kWebSocket.Listeners.AsynchronousListener
|
|
import seekdasky.kWebSocket.Message.*;
|
|
import seekdasky.kWebSocket.Server
|
|
|
|
class Channel : AsynchronousListener {
|
|
|
|
val channelName : String;
|
|
var clients : MutableList<Client>;
|
|
val messages : Stack<Pair<Client, String>>;
|
|
val serv : Server;
|
|
|
|
constructor(serv : Server , chanName : String){
|
|
this.channelName = chanName;
|
|
this.clients = mutableListOf();
|
|
this.messages = Stack(50);
|
|
this.serv = serv;
|
|
}
|
|
|
|
override fun filter(c: Client): Boolean {
|
|
return this.channelName == c.URL;
|
|
}
|
|
|
|
override fun processClosed(e: Event) {
|
|
this.clients.remove(e.client);
|
|
ConnectChannel.notifyDisconnect(e.client);
|
|
System.out.println("A client disconnected ("+this.clients.count()+" clients connected)");
|
|
}
|
|
|
|
override fun processConnection(c: Client) {
|
|
this.clients.add(c);
|
|
c.data["IsLogged"] = false;
|
|
}
|
|
|
|
override fun processEvent(e: Event) {
|
|
try{
|
|
val json = JSONObject(e.message.getString())
|
|
|
|
if(json.has("close")){
|
|
return
|
|
}
|
|
|
|
if(e.client.data["IsLogged"] != true){
|
|
//json format : {username:xxx}
|
|
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()));
|
|
|
|
|
|
val array = JSONArray();
|
|
synchronized(this.messages,{
|
|
this.messages.toList().forEach {
|
|
array.put(JSONArray(listOf(it.first.data["Username"],it.second)))
|
|
}
|
|
})
|
|
|
|
val jsonMessage = JSONObject();
|
|
jsonMessage.put("error",false);
|
|
jsonMessage.put("msg",array);
|
|
|
|
e.client.send(buildTextMessage(jsonMessage.toString()));
|
|
}else{
|
|
val jsonError = JSONObject();
|
|
jsonError.put("error","Invalid credentials");
|
|
|
|
e.client.send(buildTextMessage(jsonError.toString()));
|
|
}
|
|
|
|
}else{
|
|
//tried to access chat without logging in
|
|
val jsonError = JSONObject();
|
|
jsonError.put("error","You must send your credential before sending anything else");
|
|
|
|
e.client.send(buildTextMessage(jsonError.toString()));
|
|
|
|
}
|
|
|
|
return;
|
|
}else if(json.has("message")){
|
|
synchronized(this.messages,{
|
|
this.messages.push(Pair(e.client,json.getString("message")))
|
|
});
|
|
|
|
val array = JSONArray();
|
|
array.put(JSONArray(listOf<Any?>(e.client.data["Username"],json.getString("message"))))
|
|
|
|
val jsonMessage = JSONObject();
|
|
jsonMessage.put("error",false);
|
|
jsonMessage.put("msg",array);
|
|
for(c in this.clients){
|
|
if(c.data["IsLogged"] == true){
|
|
c.send(buildTextMessage(jsonMessage.toString()));
|
|
}
|
|
}
|
|
}else{
|
|
System.out.println("unknown JSON: "+json.toString());
|
|
}
|
|
}catch (e : Exception){
|
|
//System.out.println("Something went wrong (probably JSON parsing error");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |