Node.js+Socket.io訊息中心實施之七 訊息中心的實現(5)

百聯達發表於2014-07-14

1. index.js  啟動類

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");


var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
handle["/index.html"] = requestHandlers.index;

server.start(router.route,handle);


2.index.html 測試頁面



 
    #messages { padding: 0px; list-style-type: none;}
    #messages li { padding: 2px 0px; border-bottom: 1px solid #ccc; }
 
 

></span></strong><br /> <strong><span style="font-size:18px;">&nbsp; <script src="/socket.io/socket.io.js">

 

       var socket;
      $(function() {
          socket = io.connect();
          socket.on('connect', function() {
     var userInfo='{"userId":"gengchong"}';
     socket.emit('bind',userInfo);
     $('#messages').append('
  • conntected
  • ');

              });

         socket.on('disconnect', function() {
                      $('#messages').append('

  • Disconnected
  • ');
                  });

           
              
           socket.on('message', function(data,fn) {
          var msg=JSON.parse(data);
          var uuIds=[];
          var userId="gengchong";
          for(var i=0;i
          {
           uuIds[i]=msg[i].uuId;
                      $('#messages').append('

  • '+msg[i].msgContent+'
  • ');
          }

          var result = {userId:userId,uuIds:uuIds};
          socket.emit('clear',JSON.stringify(result));
          fn(uuIds);
                  });

             
      

          
          });


    function sendMsgToUser()
    {
    var message='{"userId":"'+$('#userId').val()+'","msgType":"ORDER","msgContent":"'+$('#chatmsg').val()+'","msgTitle":"'+$('#chatmsg').val()+'","objectId":null,"property":"2","uuId":"ed0c3481-ca7b-42b3-9a25-656835a1a8ba'+$('#userId').val()+'"}';
    socket.emit('sendMsgToUser',message,function(result)
    {
     if(result)
     {
      $('#messages').append('

  • '+result+'
  • ');
     }
    }
    );

    }

    function sendTagMessage()
    {
    var message='{"userId":"'+$('#tag').val()+'","msgType":"ORDER","msgContent":"'+$('#tagmsg').val()+'","msgTitle":"'+$('#tagmsg').val()+'","objectId":null,"property":null,"uuId":"ed0c3481-ca7b-42b3-9a25-656835a1a8ba"}';
    socket.emit('sendMsgToRoomP2P',message,function(result)
    {
     if(result)
     {
      $('#messages').append('

  • '+result+'
  • ');
     }
    }
    );

    }

    function sendMsgToAll()
    {
    var message='{"userId":"ALL","msgType":"ORDER","msgContent":"'+$('#allmsg').val()+'","msgTitle":"'+$('#allmsg').val()+'","objectId":null,"property":null,"uuId":"ed0c3481-ca7b-42b3-9a25-656835a1a8ba"}';
    socket.emit('sendMsgToAll',message,function(result)
    {
      if(result)
     {
      $('#messages').append('

  • '+result+'
  • ');
     }
    }
    );

    }


    function sub()
    {
    var tags=[];
    tags[0]=$('#tag3').val();
    var userId=$('#userId2').val();

    var msg = {userId:userId,tags:tags};

    socket.emit('sub',JSON.stringify(msg),function(result)
    {
      if(result)
     {
      $('#messages').append('

  • '+result+'
  • ');
     }
    }
    );

    }


    function unsub()
    {
    var tags=[];
    tags[0]=$('#tag2').val();
    var userId=$('#userId3').val();

    var msg = {userId:userId,tags:tags};

    socket.emit('unsub',JSON.stringify(msg),function(result)
    {
      if(result)
     {
      $('#messages').append('

  • '+result+'
  • ');
     }
    }
    );

    }

     

     


     


       

       訊息內容: 接收人UserId:
       
       

       訊息內容: 標籤:
       

       

        訊息內容:
       

       

        訂閱話題:
        使用者UserId:
       

        

        解除訂閱:
        使用者UserId:
       

      來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-1218489/,如需轉載,請註明出處,否則將追究法律責任。

      相關文章