Playframework 1.x Play framework 1.2.5 Websocket
当我运行命令警报(通知)消息转到除原始发件人之外的所有连接的套接字时,使用事件流代替ArchivedEventStream,我还可以如何发送到原始发件人 这是我的模型和控制器,使用WebSocket 事件模型Playframework 1.x Play framework 1.2.5 Websocket,playframework-1.x,Playframework 1.x,当我运行命令警报(通知)消息转到除原始发件人之外的所有连接的套接字时,使用事件流代替ArchivedEventStream,我还可以如何发送到原始发件人 这是我的模型和控制器,使用WebSocket 事件模型 public class EventModel { // ~~~~~~~~~ Let's chat! final EventStream<EventModel.Event> events = new EventStream<EventModel.Event>(
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);
}
}
公共类事件模型{
//~~~~~~~~~~~~我们聊聊吧!
最终事件流事件=新事件流(100);
/**
*获取事件
*/
public EventStream getEventStream(){
返回事件;
}
/**
*用户在房间里说了些什么
*/
公共无效警报(通知){
如果(通知==null)返回;
发布(新的EventModel.NotificationEvent(通知));
}
//~~~~~~~~~~~~活动
公共静态抽象类事件{
最终公共字符串类型;
最终公共长时间戳;
发送公共布尔值;
公共事件(字符串类型){
this.sended=false;
this.type=type;
this.timestamp=System.currentTimeMillis();
}
}
公共静态类NotificationEvent扩展了EventModel.Event{
公开最后通知;
公共通知事件(通知){
超级(“通知”);
this.notification=通知;
}
公共用户getReceiver(){
返回通知接收人;
}
}
//事件模型工厂
静态EventModel实例=null;
公共静态事件模型get(){
if(实例==null){
实例=新的EventModel();
}
返回实例;
}
//警报通知
公共静态无效警报(通知){
获取()。_警报(通知);
}
}
这是控制器
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();
}
}
}
}
}
公共类MyWebSocket扩展了RootController{
公共静态类WebSocket扩展了WebSocketController{
公共静态void echo(长用户ID){
//保安
User=User.findById(userId);
EventModel eventcenter=EventModel.get();
//已连接套接字,请加入聊天室
EventStream eventStrean=eventcenter.getEventStream();
//在插座打开时循环
while(inbound.isOpen()){
//等待事件(入站套接字频道或聊天室消息中出现的内容)
e=等待(Promise.waitor)(
inbound.nextEvent(),
eventStrean.nextEvent()
));
//如果收到任何通知,请处理
for(EventModel.NotificationEvent事件:ClassOf(EventModel.NotificationEvent.class).match(e._2)){
如果(!event.getReceiver().equals(user))继续;
outbound.send(event.notification.toJson());
}
//案例:插座已关闭
for(WebSocketClose关闭:SocketClose.match(e._1)){
断开连接();
}
}
}
}
}
要发送给原始发件人,请删除或注释掉此行:
if(!event.getReceiver().equals(user)) continue;
需要注意的一个小问题