allow connection from outside of the interop channels

This commit is contained in:
SeekDaSky 2017-12-08 03:26:46 +01:00
parent ecaed684fc
commit b944b060c1
2 changed files with 46 additions and 2 deletions

View File

@ -2,17 +2,59 @@ import Collections.Stack
import org.json.JSONObject
import seekdasky.UTF8Encoding.encode
import seekdasky.kWebSocket.Client
import seekdasky.kWebSocket.Event
import seekdasky.kWebSocket.InteropEvent
import seekdasky.kWebSocket.Listeners.AsynchronousListener
import seekdasky.kWebSocket.Listeners.InteropListener
import seekdasky.kWebSocket.Message.InteropMessage
import seekdasky.kWebSocket.Message.buildTextMessage
object ConnectChannel : InteropListener {
object ConnectChannel : InteropListener, AsynchronousListener() {
private var lastGuestIndex = 0;
private val notBoundYet = Stack<Pair<String, String>>(500);
private val connected = mutableMapOf<Client, String>();
override fun filter(c: Client): Boolean {
return c.URL == "/connect";
}
override fun processClosed(e: Event) {
return;
}
override fun processConnection(c: Client) {
return
}
override fun processEvent(e: Event) {
try {
val json = JSONObject(e.message.toString());
if(json.getString("type") == "guest"){
this.lastGuestIndex = ++this.lastGuestIndex % 500;
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(buildTextMessage(jsonReturn.toString()));
}else{
val jsonReturn = JSONObject();
jsonReturn.put("error", "Admin or User login is disabled when using the websocket API, please use the REST API to access you account");
e.client.send(buildTextMessage(jsonReturn.toString()));
}
}catch (ex : Exception){
val jsonReturn = JSONObject();
jsonReturn.put("error",true);
e.client.send(buildTextMessage(jsonReturn.toString()));
}
}
override fun filter(e: InteropEvent): Boolean {
try{

View File

@ -15,7 +15,9 @@ fun main(args: Array<String>){
server.addListener(Channel(server,"/chat"));
server.addListener(emergencyChannel);
server.addListener(eventChannel);
server.addListener(eventChannel)
server.addListener(ConnectChannel);
server.addInteropListener(PostMessage(emergencyChannel,eventChannel));
server.addInteropListener(DelMessage(emergencyChannel,eventChannel));
server.addInteropListener(UpdMessage(emergencyChannel,eventChannel));