- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在尝试使用 Spring WebSockets 创建聊天时遇到问题。
Spring 版本 - 4.3.1。
我创建了一个扩展 TextWebSocketHandler 的 ChatWebSocketHandler 类。以下是 websocket 的 XML 配置:
<websocket:handlers>
<websocket:mapping path="/websocket/chat" handler="chatWebSocketHandler" />
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor" />
</websocket:handshake-interceptors>
</websocket:handlers>
<bean name="chatWebSocketHandler" class="org.kolokolov.chat.controller.ChatWebSocketHandler" scope="prototype" />
如您所见,我定义了该 bean 的原型(prototype)范围。
这样做时,我期望 Spring 将为连接到聊天的任何新用户创建一个 ChatWebSocketHandler 的新实例。我需要它,因为这个 bean 是有状态的,因为在建立连接时,连接用户的 UserProfile 被写入它的字段。
这是部分代码
public class ChatWebSocketHandler extends TextWebSocketHandler {
private UserProfile user;
private static final Map<String, WebSocketSession> connections = new ConcurrentHashMap<>();
@Autowired
MessageService messageService;
public static Map<String, WebSocketSession> getConnections() {
return connections;
}
@Override
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
this.user = (UserProfile) webSocketSession.getAttributes().get("user");
connections.put(user.getNickname(), webSocketSession);
String message = String.format("%s %s", user.getNickname(), "has joined.");
broadcast(new Message(MessageType.MESSAGE, message, "[SERVER]").toJson());
broadcast(new Message(MessageType.USER_LIST, getLoggedUsersList()).toJson());
System.out.println(this + " : " + user.getNickname());
System.out.println(connections);
}
对于单个用户来说一切正常,直到我尝试连接第二个用户进行聊天。据我所知,没有为新用户创建 ChatWebSocketHandler 的新实例。我将第一行 System.out.println 添加到 afterConnectionEstablished 方法中以确保这一点。我得到了两个用户(开发人员和 Alex)的输出:
org.kolokolov.chat.controller.ChatWebSocketHandler@2c1b0d6d : Developer
org.kolokolov.chat.controller.ChatWebSocketHandler@2c1b0d6d : Alex
正如您所见,查看哈希码,两个用户使用相同的 ChatWebSocketHandler 实例。
为了避免这种情况,我应该考虑什么?
最佳答案
这是一个老问题,但仍然没有答案,所以解决它会很好。
对于具有以下代码的端点:
package com.my.company;
public class ChatWebSocketHandler extends TextWebSocketHandler {
//override needed methods
}
有一个类 org.springframework.web.socket.handler.PerConnectionWebSocketHandler 可以帮助您实现拥有一个连接/处理程序实例的目标。 XML 配置将类似于:
<bean id="chatPerConnectionHandler" class="org.springframework.web.socket.handler.PerConnectionWebSocketHandler">
<constructor-arg>
<value type="java.lang.Class">com.my.company.ChatWebSocketHandler</value>
</constructor-arg>
</bean>
<websocket:handlers>
<websocket:mapping path="/chat" handler="chatPerConnectionHandler" />
<!-- your config here -->
</websocket:handlers>
关于java - Spring 使用 websocket handler bean 作为单例,尽管它的原型(prototype)范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38419169/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!