- <%=ios.getAttribute("account") %>
+ <%=ios.getAccount() %>
|
- <%=ios.getAttribute("channel") %>
+ <%=ios.getChannel() %>
|
- <%=(System.currentTimeMillis()-Long.valueOf(ios.getAttribute("loginTime").toString()))/1000 %>秒
+ <%=ios.getDeviceId() %>
|
- <%if(ios.getAttribute("heartbeat")!=null){ %>
- <%=StringUtil.transformDateTime(Long.valueOf(ios.getAttribute("heartbeat").toString())) %>
- <%} %>
+ <%=ios.getDeviceModel() %>
|
+
+ <%=(System.currentTimeMillis()-ios.getBindTime())/1000 %>秒
+ |
+
-
-
+
+
|
diff --git a/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java b/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java
index a56185b..11d5b1f 100644
--- a/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java
+++ b/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java
@@ -1,55 +1,74 @@
+
package com.farsunset.ichat.cim.handler;
+import java.net.InetAddress;
+import java.util.UUID;
import org.apache.log4j.Logger;
-import org.apache.mina.core.session.IoSession;
import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handle.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.Message;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
+import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.DefaultSessionManager;
import com.farsunset.ichat.common.util.ContextHolder;
+
/**
- * 绑定账号到服务端实现
+ * 账号绑定实现
*
* @author
- */
+ */
public class BindHandler implements CIMRequestHandler {
protected final Logger logger = Logger.getLogger(BindHandler.class);
-
- public ReplyBody process(IoSession newSession, SentBody message) {
-
+ public ReplyBody process(CIMSession newSession, SentBody message) {
ReplyBody reply = new ReplyBody();
DefaultSessionManager sessionManager= ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager"));
try {
String account = message.get("account");
- newSession.setAttribute("channel", message.get("channel"));
- newSession.setAttribute("deviceId", message.get("deviceId"));
- newSession.setAttribute("device", message.get("device"));
+ newSession.setAccount(account);
+ newSession.setDeviceId(message.get("deviceId"));
+ newSession.setId(UUID.randomUUID().toString());
+ newSession.setHost(InetAddress.getLocalHost().getHostAddress());
+ newSession.setChannel( message.get("channel"));
+ newSession.setDeviceModel(message.get("device"));
/**
* 由于客户端断线服务端可能会无法获知的情况,客户端重连时,需要关闭旧的连接
*/
- IoSession oldSession = sessionManager.getSession(account);
+ CIMSession oldSession = sessionManager.getSession(account);
if(oldSession!=null)
{
//如果是账号已经在另一台终端登录。则让另一个终端下线
- if(oldSession.getAttribute("deviceId")!=null&&!oldSession.getAttribute("deviceId").equals(newSession.getAttribute("deviceId")))
+ if((oldSession.getDeviceId()!=null&&!oldSession.getDeviceId().equals(newSession.getDeviceId())
+ ||!oldSession.equals(newSession)))
{
- oldSession.removeAttribute("account");
+
+
+ oldSession.removeAttribute(CIMConstant.SESSION_KEY);
Message msg = new Message();
msg.setType(CIMConstant.MessageType.TYPE_999);//强行下线消息类型
msg.setReceiver(account);
- oldSession.write(msg);
- oldSession.close(true);
+ if(!oldSession.isLocalhost())
+ {
+
+ /*
+ 判断当前session是否连接于本台服务器,如不是发往目标服务器处理
+ MessageDispatcher.execute(msg, oldSession.getHost());
+ */
+ }else
+ {
+ oldSession.write(msg);
+ oldSession.close(true);
+ oldSession = null;
+ }
oldSession = null;
}
@@ -57,23 +76,20 @@ public class BindHandler implements CIMRequestHandler {
if(oldSession==null)
{
//第一次设置心跳时间为登录时间
- newSession.setAttribute("heartbeat", System.currentTimeMillis());
- newSession.setAttribute("loginTime", System.currentTimeMillis());
+ newSession.setBindTime(System.currentTimeMillis());
+ newSession.setHeartbeat(System.currentTimeMillis());
sessionManager.addSession(account, newSession);
-
- //设置在线状态
- reply.setCode(CIMConstant.ReturnCode.CODE_200);
-
+
}
-
+ reply.setCode(CIMConstant.ReturnCode.CODE_200);
} catch (Exception e) {
reply.setCode(CIMConstant.ReturnCode.CODE_500);
e.printStackTrace();
}
- logger.debug("auth :account:" +message.get("account")+"-----------------------------" +reply.getCode());
+ logger.debug("bind :account:" +message.get("account")+"-----------------------------" +reply.getCode());
return reply;
}
diff --git a/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/LogoutHandler.java b/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/LogoutHandler.java
index 1978be0..a593c51 100644
--- a/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/LogoutHandler.java
+++ b/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/LogoutHandler.java
@@ -1,11 +1,11 @@
package com.farsunset.ichat.cim.handler;
-import org.apache.mina.core.session.IoSession;
-
+import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handle.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
+import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.DefaultSessionManager;
import com.farsunset.ichat.common.util.ContextHolder;
@@ -17,13 +17,13 @@ import com.farsunset.ichat.common.util.ContextHolder;
*/
public class LogoutHandler implements CIMRequestHandler {
- public ReplyBody process(IoSession ios, SentBody message) {
+ public ReplyBody process(CIMSession ios, SentBody message) {
DefaultSessionManager sessionManager = ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager"));
- String account =ios.getAttribute("account").toString();
- ios.removeAttribute("account");
+ String account =ios.getAttribute(CIMConstant.SESSION_KEY).toString();
+ ios.removeAttribute(CIMConstant.SESSION_KEY);
ios.close(true);
sessionManager.removeSession(account);
diff --git a/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/SessionClosedHandler.java b/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/SessionClosedHandler.java
index 3d220e4..ddae3d7 100644
--- a/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/SessionClosedHandler.java
+++ b/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/SessionClosedHandler.java
@@ -2,11 +2,12 @@
package com.farsunset.ichat.cim.handler;
import org.apache.log4j.Logger;
-import org.apache.mina.core.session.IoSession;
+import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handle.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
+import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.DefaultSessionManager;
import com.farsunset.ichat.common.util.ContextHolder;
@@ -19,17 +20,16 @@ public class SessionClosedHandler implements CIMRequestHandler {
protected final Logger logger = Logger.getLogger(SessionClosedHandler.class);
- public ReplyBody process(IoSession ios, SentBody message) {
+ public ReplyBody process(CIMSession ios, SentBody message) {
DefaultSessionManager sessionManager = ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager"));
- if(ios.getAttribute("account")==null)
+ if(ios.getAttribute(CIMConstant.SESSION_KEY)==null)
{
return null;
}
- String account = ios.getAttribute("account").toString();
-
+ String account = ios.getAttribute(CIMConstant.SESSION_KEY).toString();
sessionManager.removeSession(account);
return null;
diff --git a/ichat-server/src/main/java/com/farsunset/ichat/cim/push/DefaultMessagePusher.java b/ichat-server/src/main/java/com/farsunset/ichat/cim/push/DefaultMessagePusher.java
index 2a93c2f..6ac7e31 100644
--- a/ichat-server/src/main/java/com/farsunset/ichat/cim/push/DefaultMessagePusher.java
+++ b/ichat-server/src/main/java/com/farsunset/ichat/cim/push/DefaultMessagePusher.java
@@ -3,9 +3,9 @@ package com.farsunset.ichat.cim.push;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.mina.core.session.IoSession;
import com.farsunset.cim.nio.mutual.Message;
+import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.DefaultSessionManager;
/**
@@ -30,9 +30,15 @@ public class DefaultMessagePusher implements CIMMessagePusher {
* @param msg
*/
public void pushMessageToUser(Message msg) {
- IoSession session = sessionManager.getSession(msg.getReceiver());
+ CIMSession session = sessionManager.getSession(msg.getReceiver());
- //服务器集群时,可以在此 判断当前session是否连接于本台服务器,如果是,继续往下走,如果不是,将此消息发往当前session连接的服务器并 return
+ /*服务器集群时,可以在此 判断当前session是否连接于本台服务器,如果是,继续往下走,如果不是,将此消息发往当前session连接的服务器并 return
+ if(!session.isLocalhost()){//判断当前session是否连接于本台服务器,如不是
+
+ MessageDispatcher.execute(msg, session.getHost());
+ return;
+ }
+ */
if (session != null && session.isConnected()) {
diff --git a/ichat-server/src/main/java/com/farsunset/ichat/cim/session/ClusterSessionManager.java b/ichat-server/src/main/java/com/farsunset/ichat/cim/session/ClusterSessionManager.java
new file mode 100644
index 0000000..17e24a4
--- /dev/null
+++ b/ichat-server/src/main/java/com/farsunset/ichat/cim/session/ClusterSessionManager.java
@@ -0,0 +1,95 @@
+
+package com.farsunset.ichat.cim.session;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.mina.core.session.IoSession;
+
+import com.farsunset.cim.nio.constant.CIMConstant;
+import com.farsunset.cim.nio.session.CIMSession;
+import com.farsunset.cim.nio.session.SessionManager;
+
+/**
+ * 集群 session管理实现示例, 各位可以自行实现 AbstractSessionManager接口来实现自己的 session管理
+ *服务器集群时 须要将CIMSession 信息存入数据库或者nosql 等 第三方存储空间中,便于所有服务器都可以访问
+ * @author farsunset (3979434@qq.com)
+ */
+public class ClusterSessionManager implements SessionManager{
+
+
+ private static HashMap