首页>代码>spring boot+mvc+mybatis+netty-sokey.io+html+js实现简单即时通讯聊天系统>/demo-spring-netty/src/main/java/com/xe/demo/Application.java
                
                package com.xe.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.xe.demo.common.pojo.AjaxResult;
import com.xe.demo.common.pojo.CacheMap;
import com.xe.demo.model.User;
import com.xe.demo.service.UserService;
@Controller
@SpringBootApplication
public class Application {
	@Value("${wss.server.host}")
	private String host;
	@Value("${wss.server.port}")
	private Integer port;
	@Bean
	public SocketIOServer socketIOServer() {
		Configuration config = new Configuration();
		config.setHostname(host);
		config.setPort(port);
		// 该处可以用来进行身份验证
		config.setAuthorizationListener(new AuthorizationListener() {
			@Override
			public boolean isAuthorized(HandshakeData data) {
				// http://localhost:8081?username=test&password=test
				// 例如果使用上面的链接进行connect,可以使用如下代码获取用户密码信息,本文不做身份验证
				// String username = data.getSingleUrlParam("username");
				// String password = data.getSingleUrlParam("password");
				return true;
			}
		});
		return new SocketIOServer(config);
	}
	@Bean
	public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
		return new SpringAnnotationScanner(socketServer);
	}
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	
	@Autowired
	private UserService userService;
	
	/**
	 * 访问首页
	 * @param map
	 * @param username
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/")
	public String index(Map<String, Object> map, String username){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if(!StringUtils.isEmpty(username)){
			User user = userService.queryUser(new User(null, username));
			map.put("user", user);
			if(null != user && 1 == user.getType()){//客服登录
				list = (List<Map<String, Object>>) CacheMap.getValue(user.getUid());
			}else{
				//获取客服列表
				list = userService.queryServiceList();
			}
		}else{
			//获取客服列表
			list = userService.queryServiceList();
		}
		map.put("list", list);
		return "chat";
	}
	
	/**
	 * 登录
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public AjaxResult login(String username, String password){
		User user = userService.queryUser(new User(null, username));
		if(null == user){
			return new AjaxResult(0, "用户不存在");
		}
		if(!password.equals(user.getPassword())){
			return new AjaxResult(0, "登录密码不正确");
		}
		return new AjaxResult(1, "登录成功");
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/getUserList")
	@ResponseBody
	public AjaxResult getUserList(String username){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if(!StringUtils.isEmpty(username)){
			User user = userService.queryUser(new User(null, username));
			if(null != user && 1 == user.getType()){//客服登录
				list = (List<Map<String, Object>>) CacheMap.getValue(user.getUid());
			}else{
				//获取客服列表
				list = userService.queryServiceList();
			}
		}else{
			//获取客服列表
			list = userService.queryServiceList();
		}
		return new AjaxResult(1, "查询成功", list);
	}
	
	/**
	 * 匿名咨询
	 */
	@RequestMapping("/anonyAsk")
	@ResponseBody
	public AjaxResult anonyAsk(){
		User user = new User();
		user.setUid(getUuid());
		user.setUsername("");
		user.setNickname("");
		user.setAvatar(getAvatar());
		user.setPassword("123456");
		user.setType(0);
		user.setOnline(0);
		userService.saveAnony(user);
		int id = user.getId();
		user.setUsername("anony" +id);
		user.setNickname("游客" + id);
		userService.update(user);
		return new AjaxResult(1, "创建成功", user);
	}
	
	/**
	 * 获取随机头像
	 * @return
	 */
	public static String getAvatar(){
		String[] avatars = new String[]{"2013.jpg", "2014.jpg", "2015.jpg", "2016.jpg", "2017.jpg", "2018.jpg", "2019.jpg", "2020.jpg", "2021.jpg", "2022.jpg", "2024.jpg"};
		Random rand = new Random();
		return avatars[rand.nextInt(10)];
	}
	
	/**
	 * 获取32为uuid
	 * @return
	 */
	public static String getUuid() {
		return UUID.randomUUID().toString().replace("-", "");
	}
}
最近下载更多
                
                xb12369     LV7
                2024年11月21日
            
            
        
                2509878298     LV5
                2024年2月29日
            
            
        
                百里守寡     LV5
                2023年8月29日
            
            
        
                你好啊呐     LV19
                2023年8月23日
            
            
        
                annazhang     LV29
                2023年2月17日
            
            
        
                pureshyness     LV6
                2022年9月29日
            
            
        
                骑着导弹让蜗牛追去     LV8
                2022年1月16日
            
            
        
                wanglinddad     LV55
                2021年12月30日
            
            
        
                j1an01     LV6
                2021年12月15日
            
            
        
                a1677596408     LV23
                2021年7月27日
            
            
        
                
                
                
最近浏览