我开始使用的原生websocket,但要求使用nutz提供的ioc,又加上了nutz提供的插件,然后可以正常工作,但是我现在又感觉自己写的不靠谱,想使用nutz提供的websocket插件的所有功能,我就继承了AbstractWsEndpoint抽象类,但我发现我原来我的 @onOpen,@onMessage等注解方法都挂了...走了AbstractWsEndpoint抽象类里的onOpen,onClose等方法.. 然后我就不清楚使用nutzwebsocket插件应该是怎样个开发流程? 求指教
15 回复
我加了,但是就是没走,开始没继承AbstractWsEndpoint 这些方法走,但是继承后就不走这些了,走父类里的onOpen...
package com.mychat.socket;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
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.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.plugins.mvc.websocket.AbstractWsEndpoint;
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import com.mychat.dao.UserDao;
import com.mychat.msg.entity.ReceiveMessage;
import com.mychat.msg.entity.SendMessage;
@IocBean
@ServerEndpoint(value = "/chat", configurator=NutWsConfigurator.class)
public class ChatSocket extends AbstractWsEndpoint{
@Inject
private UserDao userDao;
public static Map<String,Session> onlineUsers = new HashMap<String,Session>();//在线用户列表
private String username; //当前会话的用户名
private int id; //当前用户id
public ChatSocket(){
System.out.println("create chatsocket...");
}
/**
* 连接打开时的回调
* @param session
*/
@OnOpen
public void open(Session session,EndpointConfig config){
super.onOpen(session, config);
String strs = session.getQueryString();
System.out.println(strs);
String username = strs.split("=")[1];
username = username.substring(0,username.length()-3);
int id = Integer.valueOf(strs.split("=")[2]);
try {
username = URLDecoder.decode(username,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.username = username;
this.id = id;
System.out.println(username+"上线了...");
onlineUsers.put(username, session);
System.out.println("当前在线:"+onlineUsers);
}
/**
* 连接关闭时的回调
* @param session
*/
@OnClose
public void close(Session session){
try {
session.close();
} catch (IOException e) {
System.out.println("我捕获到了异常...");
e.printStackTrace();
}
onlineUsers.remove(this.username);
userDao.hide(id);
System.out.println(username+"下线了...");
System.out.println("当前在线:"+onlineUsers);
}
/**
* 接受到消息时的回调
* @param session 当前sessino
* @param msg 消息内容
*/
@OnMessage
public void message(Session session,String msg){
//解析消息内容
ReceiveMessage message =Json.fromJson(ReceiveMessage.class,msg);
Map<String, String> to = message.getTo();
Map<String, String> mine = message.getMine();
String toName = to.get("name");
Session toSession = onlineUsers.get(toName);
//构建转发消息实体
SendMessage sendMessage = new SendMessage();
sendMessage.setUsername(mine.get("username"));
sendMessage.setAvatar(mine.get("avatar"));
sendMessage.setId(mine.get("id"));
sendMessage.setType("friend");
sendMessage.setContent(mine.get("content"));
// if(to.get("username").equals(username)){
// sendMessage.setMine(true);
// }else{
// sendMessage.setMine(false);
// }
sendMessage.setFromid(mine.get("id"));
sendMessage.setTimestamp(System.currentTimeMillis());
String strJson =Json.toJson(sendMessage);
// String strJson = sendMessage.toJson();
//发送消息,记得转成json
try {
toSession.getBasicRemote().sendText(strJson);
} catch (IOException e) {
e.printStackTrace();
}
}
}
就那几个类几个接口,我都看了好几遍了...一上午都在研究这个....就是不太懂每个类/接口代表什么. 尤其是开始的时候 Handler和Room,因为我才刚做开发,比较菜= =,平时也不怎么看大牛写的源码
简单来说是这样写
@IocBean
@ServerEndpoint(value = "/chat", configurator=NutWsConfigurator.class)
public class ChatSocket extends AbstractWsEndpoint{
@Inject Dao dao;
public WsHandler createHandler(Session session, EndpointConfig config) {
ChatWsHandler handler = new ChatWsHandler(roomPrefix, dao);
handler.setRoomProvider(roomProvider);
handler.setSession(session);
return handler;
}
}
public ChatWsHandler extends SimpleWsHandler {
private Dao dao;
public ChatWsHandler(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");
switch (action) {
case "join":
join(room);
break;
case "left":
left(room);
break;
case "send":
// 处理发送过来的消息
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加回复
请先登陆