基于Websocket的在线聊天源码
立即下载
资源介绍:
本人不擅长前端,jsp页面可能不适合所有分辨率,但是核心功能都在后台代码上,希望大家可以弄懂源码,写出性能更强的聊天功能!
package com.chat.controller;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.chat.common.MyHibernateSessionFactory;
import com.chat.entity.ChatRecord;
import com.chat.entity.Evaluate;
import com.chat.entity.Relation;
import com.chat.entity.User;
public class UserDAOImpl implements UserDAO {
//用户登录
@Override
public List usersLogin(User u) {
Transaction transaction = null;
Session session = null;
try {
String hql = "from User where username=? and password=?";
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();//开始事务
Query query = session.createQuery(hql);
query.setParameter(0,u.getUsername());
query.setParameter(1, u.getPassword());
List list = query.list();
transaction.commit();
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}
public boolean changeLoginStatus(String uid,int status){
Transaction transaction = null;
Session session = null;
try {
String hql = "update User u set u.status=? where u.id=?";
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
Query query = session.createQuery(hql);
query.setParameter(0,status);
query.setParameter(1, uid);
int i = query.executeUpdate();
transaction.commit();
if(i==1){
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}
@Override
public boolean UsersRegister(User u) {
Transaction transaction = null;
Session session = null;
try {
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();//开始事务
session.save(u);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}
@Override
public boolean makeImpress(String content, String id) {
return false;
}
/*
* (non-Javadoc)
* 返回值:1代表添加成功,2代表已经是好友,0代表添加失败
* @see com.chat.controller.UserDAO#addFriend(java.lang.String, java.lang.String)
*/
@Override
public int addFriend(String fid,String mid) {
Transaction transaction = null;
Session session = null;
//首先判断是否是好友关系
int r = isFriendShip(mid, fid);
if(r==1){
Relation relation = new Relation();
relation.setAdded_id(fid);
relation.setAdder_id(mid);
relation.setStatus(1);
relation.setTime(new Date(System.currentTimeMillis()));
try {
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();//开始事务
session.save(relation);
transaction.commit();
return 1;//添加成功
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return 0;//添加失败
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}else if(r==2){//以前是好友关系,中间删除过,表中还有记录,只要修改状态为1即可
try {
String hql = "update Relation set status=1 where(adder_id=? and added_id=?) or (added_id=? and adder_id=?)";
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();//开始事务
Query query = session.createQuery(hql);
query.setParameter(0,fid);
query.setParameter(1,mid);
query.setParameter(2,fid);
query.setParameter(3,mid);
int i = query.executeUpdate();
transaction.commit();
return 1;//添加成功
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return 0;//添加失败
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}else{
return 2;//已经是好友关系
}
}
//判断是否是好友关系
public int isFriendShip(String mid,String fid){
Transaction transaction = null;
Session session = null;
try {
String hql = "from Relation where (adder_id=? and added_id=?) or (added_id=? and adder_id=?)";
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();//开始事务
Query query = session.createQuery(hql);
query.setParameter(0,mid);
query.setParameter(1,fid);
query.setParameter(2,mid);
query.setParameter(3,fid);
Relation relation = (Relation)query.uniqueResult();
transaction.commit();
if(relation==null){
return 1;//不是好友关系
}else if(relation.getStatus()==0){
return 2;//以前是好友关系,中间删除
}else{
return 3;//已经是好友关系
}
} catch (Exception e) {
e.printStackTrace();
return 0;//抛异常就显示时好友关系,后序添加操作就不会执行
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}
@Override
public boolean deleteFriend(String mid,String fid,int status) {
Transaction transaction = null;
Session session = null;
String hql= "update Relation set status=? where (adder_id=? and added_id=?) or (added_id=? and adder_id=?)";
try {
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
Query query = session.createQuery(hql);
query.setParameter(0, status);
query.setParameter(1,mid);
query.setParameter(2, fid);
query.setParameter(3,mid);
query.setParameter(4, fid);
int i = query.executeUpdate();
transaction.commit();
if(i==1){
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}
//查找联系人
@Override
public User searchFriend(String username) {
Transaction transaction = null;
Session session = null;
try {
String hql = "from User where username=?";
session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();//开始事务
Query query = session.createQuery(hql);
query.setParameter(0,username);
User user = (User)query.uniqueResult();
transaction.commit();
return user;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return null;
}finally{
MyHibernateSessionFactory.close(session);
MyHibernateSessionFactory.close(transaction);
}
}
@Override
public List selectAllOnLineFriend(String id,int status) {
Session session = null;
Transaction transaction = null;
String sql = null;
if(status==1){
//查询在线好友
sql = "select id,username,password,img,status from user where status=1 and id in("
+"select adder_id from relation where added_id="+id+" and status=1"
+" union "
+"select added_id from relation where adder_id="+id+" and status=1"
+ ")";
}
if(status==0){
//查询所有好友(包括离线�
资源文件列表:
onLineChat.zip 大约有134个文件