Tomcat7中实现WebSocket 中 Endpoint的Session跟httpSession不是同一个东西,如何在WebSocket中访问HttpSocket呢?
这样可以获取HttpSocket中的对象,例如getAttribute()
首先要继承 ServerEndpointConfig,并实现 modifyHandshake方法,该方法有个
HandshakeRequest参数,代码如下:
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator
{
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request,
HandshakeResponse response)
{
HttpSession httpSession = (HttpSession)request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
这时可以在实现ServerEndPoint的类中作为configurator参数
package action;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.log4j.Logger;
import common.GetHttpSessionConfigurator;
import common.HTMLFilter;
@ServerEndpoint(value = "/action/websocket/chat",configurator=GetHttpSessionConfigurator.class)
public class ChatAction{
private final static Logger log = Logger.getLogger(ChatAction.class);
private static final Set<ChatAction> onlineUsers =
new CopyOnWriteArraySet<ChatAction>();
private String nickname;
private Session session;
private HttpSession httpSession;
@OnOpen
public void start(Session session, EndpointConfig config) {
this.session = session;
this.httpSession = (HttpSession) config.getUserProperties()
.get(HttpSession.class.getName());
this.nickname=(String) httpSession.getAttribute("loginOperatorId");
onlineUsers.add(this);
String message = String.format("* %s %s", nickname, " from websocket 上线了...");
broadcast(message);
}
@OnClose
public void end(Session session) {
onlineUsers.remove(this);
String message = String.format("* %s %s",
nickname, " from websocket 已经离开...");
broadcast(message);
}
@OnMessage
public void incoming(String message, EndpointConfig config) {
// Never trust the client
String filteredMessage = String.format("%s: %s",
nickname, HTMLFilter.filter(message.toString()));
broadcast(filteredMessage);
}
@OnError
public void onError(Throwable t) throws Throwable {
log.error("错误: " + t.toString(), t);
}
private static void broadcast(String msg) {
for (ChatAction client : onlineUsers) {
try {
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
log.debug("错误: 消息发送失败!", e);
onlineUsers.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, " from websocket 已经离开...");
broadcast(message);
}
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。