在spring3.2.0+hibernate3.3.2+struts2 2.2.3中使用websocket,运行环境是tomcat8.0.36,获取HttpSession

直接看如何获取HttpSession对象。


首先必须得写一个继承javax.websocket.server.ServerEndpointConfig.Configurator类的类来获取HttpSession:

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

public class GetHttpSessionConfigurator extends Configurator{

	@Override
	public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
		// TODO Auto-generated method stub
		HttpSession httpSession=(HttpSession) request.getHttpSession();
        sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
	}
}

接着在websocket服务器类中:

注意由于使用了struts2,所以所有的请求都会被拦截,需要在strus.xml加上:

就可以放过该请求。

import java.sql.SQLException;

import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.hibernate.HibernateException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.chuangyejia.bean.User;
import com.chuangyejia.websockettoolkit.GetHttpSessionConfigurator;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

@ServerEndpoint(value = "/chat/server",configurator=GetHttpSessionConfigurator.class)
public class ChatServer  {
	

	@SuppressWarnings({ "resource", "unchecked", "rawtypes" })
	@OnMessage // 接受客户端消息
	public void onMessage(String msg, Session session) throws Exception {
		
		Gson gson = new Gson();
		JsonObject jo = new JsonObject();
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		HibernateTemplate hibernateTemplate = ac.getBean("hibernateTemplate", HibernateTemplate.class);
		final User user = new User();
		boolean rightToUser = (boolean)hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
				Object[] s = (Object[]) session.createQuery("select u.userId,u.userNickName,u.userPhoto from User u where u.userId = '402881fc56416d860156416e52330001'").list().get(0);
				if(s != null) {
					user.setUserId(s[0].toString());
					user.setUserNickName(s[1].toString());
					user.setUserPhoto(s[2].toString());
					return true;
				} else
					return false;
			}
			
		});
		
		if(rightToUser) {
		}
		jo.add("toUser", gson.toJsonTree(user));
		System.out.println("msg : " + msg.toString());
		
		session.getBasicRemote().sendText(jo.toString()); // 发送信息到客户端
		
		if(msg.equals("close")) {
			session.getBasicRemote().sendText("bye bye!"); // 发送信息到客户端
			session.close(); // 关闭连接点
		}
			
	}

	@OnOpen // 成功连接时执行此代码
	public void onOpen(EndpointConfig config) {
		HttpSession httpSession= (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
		User fromUser = (User)httpSession.getAttribute("user");
		System.out.println(fromUser.getUserNickName());
	}

	@OnClose // 连接关闭时执行
	public void onClose() {}
}

注意在OnOpen中执行的代码!就那里获取HttpSession对象!


看看前台的js代码部分:

 
  
 就这样。 
  

你可能感兴趣的:(struts2,tomcat,hibernate,spring,hibernate,struts2.0,spring,websocket,HttpSession)