原创nio socket mina+javascript+flash实现commet长连接网页聊天室

JS代码

//数据返回回调方法
 function cmtData(data){
  switch(data.event){
   case "init":
    $("#status").html("init...");
    var userlist = data.list;
    if(userlist){
     for(var i=0;i<userlist.length;i++){
      $("#userlist").append("<a id='user_"+userlist[i].sid+"' href='javascript:void();' onclick='toidclick(this);'>" + userlist[i].nickname + "<br><a>"); 
     }
    }
    break;
   case "quit":
    $("#ret_msg").append(data.nickname +"离开聊天室!<br>");
    $("#userlist #user_"+data.sid).remove();
    break;
   case "message":
    $("#ret_msg").append(data.nickname +"_say:"+ data.msg + "("+data.time+")<br>");
    break;
   case "join":
    $("#ret_msg").append(data.nickname +"加入聊天室!<br>");
    $("#userlist").append("<a id='user_"+data.sid+"' href='javascript:void();' onclick='toidclick(this);'>" + data.nickname + "<br><a>");
    break;
  }
 }
 //系统调用初始化
 function cmtInit(){
  connect();
 }
 //连接后调用
 function cmtConnected(){
  CMT.post('{"event":"login","nickname":"'+$("#nick").val()+'"}');
 }
 /////////////////////////////////
 function toidclick(e){
  $("#toid").val(e.id.substring(5));
 }
 function connect(){
  var nick = $("#nick").val();
  if(nick==''){
   alert('名称不能为空!');
  }else{
   CMT.connect();
  }
 }
 function logout(){
  CMT.post('{"event":"quit"}');
  $("#userlist").html('');
  $("#status").html("nostatus...");
 }
 function send(){
  if(CMT.isconnected()){
   var data = '{"event":"message","msg":"'+$("#message").val()+'","toid":"'+$("#toid").val()+'"}';
   CMT.post(data);
   $("#message").val('');
  }else{
   alert('请先点login按钮!');
  }
 }
 var CMT = {};
 (function(cmt,$){
  cmt.init = function(){
   var cmttaget = $("<div id=\"flash_content\" style=\"position: absolute;top: -1000px;left: -1000px\"></div>");
   $("body").append(cmttaget);
   //init swf
   var swf = new SWFObject("cmt.swf", "__cmt", "25", "25", "7", "#336699");
   swf.addParam("quality", "low");
   swf.addParam("wmode", "transparent");
   swf.addParam("salign", "t");
   swf.addParam("allowscriptaccess","always");
   swf.write("flash_content");

  }
  cmt.thisMovie = function(movieName){
   if (window.document[movieName]){
    return window.document[movieName];
   }
   if (navigator.appName.indexOf("Microsoft Internet")==-1){
    if (document.embeds && document.embeds[movieName])
     return document.embeds[movieName];
    }
   else{ // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    return document.getElementById(movieName);
   }
  }
  cmt.isconnected = function(){
   var isconnected = cmt.getCmt().isconnected();
   return isconnected;

  }
  cmt.getCmt = function(){
   return cmt.thisMovie("__cmt");
  }
  cmt.connect = function(){
   if(!cmt.isconnected())
    cmt.getCmt().connect('127.0.0.1',110);
   else
    alert("已与服务器连接!");
  }
  cmt.post = function(data){
   if(cmt.isconnected())
    cmt.getCmt().post(data);
   else
    alert("与服务器未连接!");
  }  
 })(CMT,jQuery)
 //////////////////////////
 $(function(){
   CMT.init();
  });

 此简易服务器消息处理代码

logger.info(">>>>>>>>>>>>>>>>>message");
		
ASObject message = context.getMessage();
mcManager = context.getMcManager();
Object toid = message.get("toid");
Result result = null;
		
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
CharacterMC mc = mcManager.get(context.getSession());
		
Map data = new HashMap();
data.put("event", "message");
data.put("sid", context.getSid());
data.put("msg", message.get("msg"));
data.put("nickname",mc.getNickname());
data.put("time", sdf.format(new Date()));
if(toid == null || "".equals(toid)){
	result = new BroadCastResult(data);
}else{
	result = new SubscribeResult(data,new Long[]{Long.parseLong((String)toid)},true);
}
context.addResult(result);

 欢印大家给我留言改进设计

你可能感兴趣的:(JavaScript,jquery,socket,Flash,Mina)