netty-socketio使用namespace


一、簡介

  netty-socketio中的namespace可以用於區別在相同連接地址下的不同用戶,當兩個不同的用戶打開同一個頁面的時候,可以使用namespace用來標記不同用戶。例如我們可以在用戶中心頁面動態的獲取用戶的消息數目。這里就可以使用到namespace。因為每個用戶的id都是不一樣的,我們可以使用id來標識每個用戶的namespace。

二、示例

  服務器端代碼:

package com.test.socket;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;

public class SocketServer2 {
    public static void main(String[] args) throws InterruptedException {

        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        final SocketIOServer server = new SocketIOServer(config); 
        server.start();
        String uid = "1111";
        String namespace = String.format("/%s_%s", "msg", uid);//構建命名空間
        SocketIONamespace chat1namespace = server.addNamespace(namespace); //設置命名空間

        for (int i = 0; i < 50; i++) { 
            Thread.sleep(2000); 
             chat1namespace.getBroadcastOperations().sendEvent("message", 1); //每次發送數字一
        }

        Thread.sleep(Integer.MAX_VALUE);

        server.stop();
    }

}

  客戶端message.html代碼:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6    <script src="./jquery-1.9.1.js" type="text/javascript"></script> 
 7   <script type="text/javascript" src="./socket.io/socket.io.js"></script>
 8 
 9     <style>
10         body { 
11             padding:20px;
12         }
13         #console {  
14             overflow: auto; 
15         }
16         .username-msg {color:orange;}
17         .connect-msg {color:green;}
18         .disconnect-msg {color:red;}
19         .send-msg {color:#888}
20     </style>
21 
22 </head>
23 
24 <body>
25     
26     <h1>Netty-socketio Demo Chat</h1>
27     
28     <br/>
29 
30     <div id="console" class="well">
31     </div>
32     消息總數:<div id="msgnum">0</di>    
33 </body>
34 
35   
36          
37 <script type="text/javascript">
38         var socket =  io.connect('http://localhost:9092/msg_1111');
39 
40         socket.on('connect', function() {
41             output('<span class="connect-msg">Client has connected to the server!</span>');
42         });
43 
44         socket.on('message', function(data) {//收到消息后,將消息總數加一
45             var num = $("#msgnum").html();
46             num = parseInt(num) + data;
47             $("#msgnum").html(num);
48         });
49         
50         socket.on('disconnect', function() {
51             output('<span class="disconnect-msg">The client has disconnected!</span>');
52         });
53         function sendDisconnect() {
54             socket.disconnect();
55         } 
56          
57         function output(message) {
58             var currentTime = "<span class='time'>" +  new Date() + "</span>";
59             var element = $("<div>" + currentTime + " " + message + "</div>");
60             $('#console').prepend(element);
61         }
62         
63     </script>
64 </html>

  啟動服務器,訪問該網頁,消息總數會每次加1。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM