create: InteropConnection
This commit is contained in:
parent
b141c304ef
commit
77a15f9958
|
@ -1,2 +1,3 @@
|
|||
.idea
|
||||
out
|
||||
/.gtm/
|
||||
|
|
|
@ -18,7 +18,7 @@ class Channel : AsynchronousListener {
|
|||
constructor(serv : Server , chanName : String){
|
||||
this.channelName = chanName;
|
||||
this.clients = mutableListOf();
|
||||
this.messages = Stack(20);
|
||||
this.messages = Stack(50);
|
||||
this.serv = serv;
|
||||
}
|
||||
|
||||
|
@ -40,42 +40,40 @@ class Channel : AsynchronousListener {
|
|||
try{
|
||||
val json = JSONObject(e.message.getString())
|
||||
|
||||
if(json.has("close")){
|
||||
if(json.getBoolean("close")){
|
||||
e.client.close("Server closing");
|
||||
this.processClosed(e);
|
||||
}
|
||||
}else if(e.client.data["IsLogged"] != true){
|
||||
if(e.client.data["IsLogged"] != true){
|
||||
//json format : {username:xxx}
|
||||
if(json.getString("name") != null){
|
||||
e.client.data["IsLogged"] = true;
|
||||
e.client.data["Username"] = json.getString("name");
|
||||
|
||||
val jsonLogin = JSONObject();
|
||||
jsonLogin.put("error",false);
|
||||
e.client.send(buildTextMessage(jsonLogin.toString()));
|
||||
if(ConnectChannel.isConnected(e.client)){
|
||||
val jsonLogin = JSONObject();
|
||||
jsonLogin.put("error",false);
|
||||
e.client.send(buildTextMessage(jsonLogin.toString()));
|
||||
|
||||
|
||||
val array = JSONArray();
|
||||
synchronized(this.messages,{
|
||||
this.messages.forEach {
|
||||
array.put(JSONArray(listOf(it.first.data["Username"],it.second)))
|
||||
}
|
||||
})
|
||||
val array = JSONArray();
|
||||
synchronized(this.messages,{
|
||||
this.messages.forEach {
|
||||
array.put(JSONArray(listOf(it.first.data["Username"],it.second)))
|
||||
}
|
||||
})
|
||||
|
||||
val jsonMessage = JSONObject();
|
||||
jsonMessage.put("error",false);
|
||||
jsonMessage.put("msg",array);
|
||||
val jsonMessage = JSONObject();
|
||||
jsonMessage.put("error",false);
|
||||
jsonMessage.put("msg",array);
|
||||
|
||||
e.client.send(buildTextMessage(jsonMessage.toString()));
|
||||
e.client.send(buildTextMessage(jsonMessage.toString()));
|
||||
}else{
|
||||
val jsonError = JSONObject();
|
||||
jsonError.put("error","Invalid credentials");
|
||||
|
||||
|
||||
System.out.println("A client connected ("+this.clients.count()+" clients connected)");
|
||||
e.client.send(buildTextMessage(jsonError.toString()));
|
||||
}
|
||||
|
||||
}else{
|
||||
//tried to access chat without logging in
|
||||
val jsonError = JSONObject("");
|
||||
jsonError.put("error",true);
|
||||
jsonError.put("error","You must send your credential before sending anything else");
|
||||
|
||||
e.client.send(buildTextMessage(jsonError.toString()));
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
import org.json.JSONObject
|
||||
import seekdasky.UTF8Encoding.encode
|
||||
import seekdasky.kWebSocket.Client
|
||||
import seekdasky.kWebSocket.InteropEvent
|
||||
import seekdasky.kWebSocket.Listeners.InteropListener
|
||||
import seekdasky.kWebSocket.Message.InteropMessage
|
||||
|
||||
object ConnectChannel : InteropListener {
|
||||
|
||||
var lastGuestIndex = 0;
|
||||
|
||||
val notBoundYet = Stack<Pair<String,String>>(50);
|
||||
val connected = mutableMapOf<Client, String>();
|
||||
|
||||
|
||||
override fun filter(e: InteropEvent): Boolean {
|
||||
try{
|
||||
val json = JSONObject(e.message.string);
|
||||
return json.has("type") && json.has("name");
|
||||
}catch (e : Exception){
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override fun processEvent(e: InteropEvent) {
|
||||
try {
|
||||
val json = JSONObject(e.message.string);
|
||||
|
||||
if(json.getString("type") == "guest"){
|
||||
this.lastGuestIndex = ++this.lastGuestIndex % 10;
|
||||
val guestName = "Guest"+this.lastGuestIndex;
|
||||
val jsonReturn = JSONObject();
|
||||
jsonReturn.put("error", false);
|
||||
jsonReturn.put("name", guestName);
|
||||
|
||||
this.notBoundYet.push(Pair("guest",guestName));
|
||||
|
||||
|
||||
e.client.send(InteropMessage(encode(jsonReturn.toString())));
|
||||
}else{
|
||||
val jsonReturn = JSONObject();
|
||||
jsonReturn.put("error", false);
|
||||
jsonReturn.put("name", json.getString("name"));
|
||||
this.notBoundYet.push(Pair(json.getString("type"),json.getString("name")));
|
||||
|
||||
e.client.send(InteropMessage(encode(jsonReturn.toString())));
|
||||
}
|
||||
}catch (ex : Exception){
|
||||
val jsonReturn = JSONObject();
|
||||
jsonReturn.put("error",true);
|
||||
e.client.send(InteropMessage(encode(jsonReturn.toString())));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,8 +3,10 @@ import seekdasky.kWebSocket.Server
|
|||
|
||||
fun main(args: Array<String>){
|
||||
val server = Server("0.0.0.0",9999,null);
|
||||
server.startInteropServer("localhost",9998);
|
||||
|
||||
server.addListener(ChannelDispatcher(server));
|
||||
server.addListener(Channel(server,"/chat"));
|
||||
server.addInteropListener(ConnectChannel);
|
||||
server.startServer();
|
||||
|
||||
server.block();
|
||||
|
|
|
@ -26,6 +26,10 @@ class Stack<T>(size : Int):Iterator<T> {
|
|||
}
|
||||
}
|
||||
|
||||
fun remove(item : T){
|
||||
this.items.remove(item);
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
val hasNext = itCounter < count()
|
||||
|
||||
|
|
Loading…
Reference in New Issue