如何访问实例化的WebSockets在码头9?(How do I access instantiat

2019-07-23 15:14发布

这可能是显而易见的,但我是新来这个范例。 我创建了一个Jetty服务器并注册我的WebSocket类,如下所示:

        Server server = new Server(8080);
        WebSocketHandler wsHandler = new WebSocketHandler()
        {
            @Override
            public void configure(WebSocketServletFactory factory)
            {
                factory.register(MyEchoSocket.class);
            }
        };
        server.setHandler(wsHandler);

网页套接字接收消息细。 我想也可以从服务器将邮件发送,而无需首先收到一则由客户端。 如何访问的连接打开时所创建的MyEchoSocket实例? 或者更一般地说,我怎么在插座上发送消息在MyEchoSocket的onText方法之外?

Answer 1:

两种常见的技术,在一个超级简单的聊天室的概念这里介绍。

选项#1:拥有的WebSocket其状态报告给中央位置

@WebSocket
public class ChatSocket {
    public Session session;

    @OnWebSocketConnect
    public void onConnect(Session session) {
        this.session = session;
        ChatRoom.getInstance().join(this);
    }

    @OnWebSocketMessage
    public void onText(String message) {
        ChatRoom.getInstance().writeAllMembers("Hello all");
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        ChatRoom.getInstance().part(this);
    }
}

public class ChatRoom {
    private static final ChatRoom INSTANCE = new ChatRoom();

    public static ChatRoom getInstance()
    {
        return INSTANCE;
    }

    private List<ChatSocket> members = new ArrayList<>();

    public void join(ChatSocket socket) 
    {
        members.add(socket);
    }

    public void part(ChatSocket socket) 
    {
        members.remove(socket);
    }

    public void writeAllMembers(String message) 
    {
        for(ChatSocket member: members)
        {
            member.session.getRemote().sendStringByFuture(message);
        }
    }

    public void writeSpecificMember(String memberName, String message) 
    {
        ChatSocket member = findMemberByName(memberName);
        member.session.getRemote().sendStringByFuture(message);
    }

    public ChatSocket findMemberByName(String memberName) 
    {
        // left as exercise to reader
    }
}

然后,只需使用中央位置谈谈你所选择的的WebSockets。

ChatRoom.getInstance().writeSpecificMember("alex", "Hello");

// or

ChatRoom.getInstance().writeAllMembers("Hello all");

选项2:有没有WebSocket的手动与WebSocketCreator创建

@WebSocket
public class ChatSocket {
    public ChatRoom chatroom;

    public ChatSocket(ChatRoom chatroom)
    {
        this.chatroom = chatroom;
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
        chatroom.join(this);
    }

    @OnWebSocketMessage
    public void onText(String message) {
        chatroom.writeAllMembers(message);
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        chatroom.part(this);
    }
}

public class ChatCreator implements WebSocketCreator
{
    private ChatRoom chatroom;

    public ChatCreator(ChatRoom chatroom)
    {
        this.chatroom = chatroom;
    }

    public Object createWebSocket(UpgradeRequest request, 
                                  UpgradeResponse response)
    {
        // We want to create the Chat Socket and associate
        // it with our chatroom implementation
        return new ChatSocket(chatroom);
    }
}

public class ChatHandler extends WebSocketHandler
{
    private ChatRoom chatroom = new ChatRoom();

    @Override
    public void configure(WebSocketServletFactory factory)
    {
        factory.setCreator(new ChatCreator(chatroom));
    }
}

在这一点上,你可以使用相同的技术上面跟你选择的的WebSockets。



文章来源: How do I access instantiated WebSockets in Jetty 9?