2013-01-12 72 views
0

取而代之使用EventStream代替ArchivedEventStream,当我运行命令alert(notification)消息时,转到除原始发件人之外的所有连接的套接字,我怎么也可以发送给原始发件人。播放框架1.2.5 Websocket

这里是我的模型和控制器,使用的WebSocket

EventModel

public class EventModel { 

// ~~~~~~~~~ Let's chat! 

final EventStream<EventModel.Event> events = new EventStream<EventModel.Event>(100); 

/** 
* Get the event 
*/ 

public EventStream<EventModel.Event> getEventStream() { 
    return events; 
} 
/** 
* A user say something on the room 
*/ 

public void _alert(Notification notification){ 
    if(notification == null) return; 
    events.publish(new EventModel.NotificationEvent(notification)); 
} 


// ~~~~~~~~~ Events 

public static abstract class Event { 

    final public String type; 
    final public Long timestamp; 
    public boolean sended; 

    public Event(String type) { 
     this.sended = false; 
     this.type = type; 
     this.timestamp = System.currentTimeMillis(); 
    } 

} 

public static class NotificationEvent extends EventModel.Event{ 
    public final Notification notification; 
    public NotificationEvent(Notification notification) { 
     super("notification"); 
     this.notification = notification; 
    } 

    public User getReceiver(){ 
     return notification.receiver; 
    } 
} 


// EventModel factory 

static EventModel instance = null; 
public static EventModel get() { 
    if(instance == null) { 
     instance = new EventModel(); 
    } 
    return instance; 
} 

//Alert notification 
public static void alert(Notification notification){ 
    get()._alert(notification); 
} 

} 

这里是控制器

public class MyWebSocket extends RootController { 

public static class WebSocket extends WebSocketController { 

    public static void echo(Long userId) { 
     //Security 
     User user = User.findById(userId); 

     EventModel eventCentre = EventModel.get(); 

     // Socket connected, join the chat room 
     EventStream<EventModel.Event> eventStrean = eventCentre.getEventStream(); 

     // Loop while the socket is open 
     while(inbound.isOpen()) { 

      // Wait for an event (either something coming on the inbound socket channel, or ChatRoom messages) 

      Either<WebSocketEvent,EventModel.Event> e = await(Promise.waitEither(
       inbound.nextEvent(), 
       eventStrean.nextEvent() 
      )); 

      //Handle if get any notification 
      for(EventModel.NotificationEvent event: ClassOf(EventModel.NotificationEvent.class).match(e._2)) { 

       if(!event.getReceiver().equals(user)) continue; 
       outbound.send(event.notification.toJson()); 
      }  

      // Case: The socket has been closed 
      for(WebSocketClose closed: SocketClosed.match(e._1)) { 
       disconnect(); 
      } 
     } 
    } 
} 
} 

回答

0

发送到原始发件人以及删除或注释掉这行:

if(!event.getReceiver().equals(user)) continue;