gpt4 book ai didi

java - 如何在 Jetty 9 中访问实例化的 WebSocket?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:49 27 4
gpt4 key购买 nike

这可能是显而易见的,但我对这个范例很陌生。我创建一个 Jetty 服务器并注册我的 websocket 类,如下所示:

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

websocket 接收消息正常。我还希望能够从服务器发送消息,而无需先从客户端接收消息。如何访问连接打开时创建的 MyEchoSocket 实例?或者,更一般地说,如何在 MyEchoSocket 中 onText 方法之外的套接字上发送消息?

最佳答案

这里以 super 简化的聊天室概念介绍了两种常见技术。

选项#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
}
}

然后只需使用中心位置与您选择的 Websocket 进行通信即可。

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

// or

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

选项 #2:使用 WebSocketCreator 手动创建 WebSocket

@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 进行通信。

关于java - 如何在 Jetty 9 中访问实例化的 WebSocket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37979541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com