NutzCN Logo
问答 redisService.set方法报空指针
发布于 2324天前 作者 小小小郑 2531 次浏览 复制 上一个帖子 下一个帖子
标签:
package com.game.brag.server;

import java.util.HashSet;
import java.util.Set;

import org.nutz.dao.Dao;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.plugins.mvc.websocket.handler.SimpleWsHandler;

import com.game.brag.bean.User;
@IocBean
public class MyHandler extends SimpleWsHandler {
	
	private Dao dao;
	// redis注入
    @Inject RedisService redisService;
    public MyHandler(String prefix, Dao dao) {
        super(prefix);
        this.dao = dao;
    }

    public void onMessage(String message) {
        try {
            NutMap msg = Json.fromJson(NutMap.class, message);
            String action = msg.getString("action");
            if (Strings.isBlank(action))
                return;
            String room = msg.getString("room");
            //获取当前房间的其他在线用户组,不包括自己,因为还没执行join
            Set<String> wsids = roomProvider.wsids(room);
            NutMap resp = new NutMap();
            switch (action) {
            case "join":
                join(room);
                String openId = msg.getString("openId");
                String nickName = msg.getString("nickName");
                String imgUrl = msg.getString("imgUrl");
                User user = new User();
                user.setOpenId(openId);
                user.setImgUrl(imgUrl);
                user.setNickName(nickName);
                //将用户放入redis缓存,服务器定时自动删除缓存
                redisService.set(openId, Json.toJson(user));
                //通知其他用户
                resp.setv("action", "join");
        		resp.setv("msg", redisService.get(session.getId()));
        		for(String wsid : wsids){
        			if(!wsid.equals(session.getId())){
        				endpoint.sendJson(wsid,resp);
        			}
        		}
                //向该用户发送其他用户信息
        		Set<User> users = new HashSet<User>();
        		for(String wsid : wsids){
        			users.add(Json.fromJson(User.class, redisService.get(wsid)/*来源参数*/));
        		}
        		resp.setv("action", "joinBack");
        		resp.setv("msg", users);
				endpoint.sendJson(session.getId(),resp);
                break;
            case "left":
                left(room);
                //通知其他用户
                resp.setv("action", "left");
        		resp.setv("msg", Json.fromJson(User.class, redisService.get(session.getId())));
        		for(String wsid : wsids){
        			if(!wsid.equals(session.getId())){
        				endpoint.sendJson(wsid,resp);
        			}
        		}
                break;
            case "send":
                 // 处理发送过来的消息
                break;
            default:
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3 回复

//将用户放入redis缓存,服务器定时自动删除缓存
redisService.set(openId, Json.toJson(user));
执行到这句就报空指针了,之前用过可以用,redis服务器都是ok的,启动的时候也没有报redis连接不上

已经解决了
MyHandler handler = new MyHandler(roomPrefix, dao,redisService);
因为是自己手动new的handler所以ioc不会帮你管理,@iocbean是无效的,在new的父类里把需要靠ioc管理的对象传过去就行了

添加回复
请先登陆
回到顶部