package com.wizzardo.http.websocket;

import com.wizzardo.http.framework.di.Injectable;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wizzardo on 08.12.15.
 */
@Injectable
public class DefaultWebSocketHandler extends WebSocketHandler {
    protected Set<WebSocketListener> listeners = Collections.newSetFromMap(new ConcurrentHashMap<>());

    @Override
    public void onConnect(WebSocketListener listener) {
        listeners.add(listener);
    }

    @Override
    public void onDisconnect(WebSocketListener listener) {
        listeners.remove(listener);
    }

    public void broadcast(String s) {
        Message message = new Message(s);
        Iterator<WebSocketListener> iterator = listeners.iterator();
        while (iterator.hasNext()) {
            WebSocketListener listener = iterator.next();
            try {
                listener.sendMessage(message);
            } catch (Exception e) {
                iterator.remove();
                e.printStackTrace();
            }
        }
    }

}

@@ -751,17 +751,13 @@
     
-Iterator%3C
+for (
 WebS
@@ -773,137 +773,32 @@
 ener
-%3E iterator = listeners.iterator();%0A        while (iterator.hasNext()) %7B%0A            WebSocketListener listener = iterator.next();
+ listener : listeners) %7B
 %0A   
@@ -915,22 +915,24 @@
     
-iterator.remov
+e.printStackTrac
 e();
@@ -948,34 +948,37 @@
         
-e.printStackTrace(
+onDisconnect(listener
 );%0A     

revert some changes