播放框架1.2.5 Websocket

Posted

技术标签:

【中文标题】播放框架1.2.5 Websocket【英文标题】:Play framework 1.2.5 Websocket 【发布时间】:2013-01-12 11:50:26 【问题描述】:

使用 EventStream 而不是 ArchivedEventStream,当我运行命令警报(通知)消息时,将转到除原始发件人之外的所有连接的套接字,我如何也可以发送到原始发件人。

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

事件模型

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();
               
        
    


【问题讨论】:

【参考方案1】:

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

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

【讨论】:

【参考方案2】:

需要注意的一个小问题

https://github.com/playframework/play1/pull/709

【讨论】:

以上是关于播放框架1.2.5 Websocket的主要内容,如果未能解决你的问题,请参考以下文章

播放框架1.2.5应用项目如何重命名

在播放框架 1.2.5 中对路由不区分大小写

播放框架 1.2.5 的路由查询

在播放框架 1.2.5 中处理来自本地 postgres 的数据

部署播放框架 1.2.5 应用程序时出现 LinkageError

在播放框架 1.2.5 中,扩展作业运行后台阻止请求/响应的类