create: InteropConnection
This commit is contained in:
parent
b141c304ef
commit
77a15f9958
|
@ -1,2 +1,3 @@
|
||||||
.idea
|
.idea
|
||||||
out
|
out
|
||||||
|
/.gtm/
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Channel : AsynchronousListener {
|
||||||
constructor(serv : Server , chanName : String){
|
constructor(serv : Server , chanName : String){
|
||||||
this.channelName = chanName;
|
this.channelName = chanName;
|
||||||
this.clients = mutableListOf();
|
this.clients = mutableListOf();
|
||||||
this.messages = Stack(20);
|
this.messages = Stack(50);
|
||||||
this.serv = serv;
|
this.serv = serv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,17 +40,13 @@ class Channel : AsynchronousListener {
|
||||||
try{
|
try{
|
||||||
val json = JSONObject(e.message.getString())
|
val json = JSONObject(e.message.getString())
|
||||||
|
|
||||||
if(json.has("close")){
|
if(e.client.data["IsLogged"] != true){
|
||||||
if(json.getBoolean("close")){
|
|
||||||
e.client.close("Server closing");
|
|
||||||
this.processClosed(e);
|
|
||||||
}
|
|
||||||
}else if(e.client.data["IsLogged"] != true){
|
|
||||||
//json format : {username:xxx}
|
//json format : {username:xxx}
|
||||||
if(json.getString("name") != null){
|
if(json.getString("name") != null){
|
||||||
e.client.data["IsLogged"] = true;
|
e.client.data["IsLogged"] = true;
|
||||||
e.client.data["Username"] = json.getString("name");
|
e.client.data["Username"] = json.getString("name");
|
||||||
|
|
||||||
|
if(ConnectChannel.isConnected(e.client)){
|
||||||
val jsonLogin = JSONObject();
|
val jsonLogin = JSONObject();
|
||||||
jsonLogin.put("error",false);
|
jsonLogin.put("error",false);
|
||||||
e.client.send(buildTextMessage(jsonLogin.toString()));
|
e.client.send(buildTextMessage(jsonLogin.toString()));
|
||||||
|
@ -68,14 +64,16 @@ class Channel : AsynchronousListener {
|
||||||
jsonMessage.put("msg",array);
|
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");
|
||||||
|
|
||||||
|
e.client.send(buildTextMessage(jsonError.toString()));
|
||||||
System.out.println("A client connected ("+this.clients.count()+" clients connected)");
|
}
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
//tried to access chat without logging in
|
//tried to access chat without logging in
|
||||||
val jsonError = JSONObject("");
|
val jsonError = JSONObject("");
|
||||||
jsonError.put("error",true);
|
|
||||||
jsonError.put("error","You must send your credential before sending anything else");
|
jsonError.put("error","You must send your credential before sending anything else");
|
||||||
|
|
||||||
e.client.send(buildTextMessage(jsonError.toString()));
|
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>){
|
fun main(args: Array<String>){
|
||||||
val server = Server("0.0.0.0",9999,null);
|
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.startServer();
|
||||||
|
|
||||||
server.block();
|
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 {
|
override fun hasNext(): Boolean {
|
||||||
val hasNext = itCounter < count()
|
val hasNext = itCounter < count()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue